Skip to content

Commit

Permalink
Remove "volt profile use" command (fix #163)
Browse files Browse the repository at this point in the history
  • Loading branch information
tyru committed Jan 2, 2018
1 parent c9f83bd commit 3d3c27f
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 158 deletions.
7 changes: 0 additions & 7 deletions CMDREF.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,10 +259,6 @@ Command
profile rm [-current | {name}] {repository} [{repository2} ...]
Remove one or more repositories from profile {name}.
profile use [-current | {name}] vimrc [true | false]
profile use [-current | {name}] gvimrc [true | false]
Set vimrc / gvimrc flag to true or false.
Quick example
$ volt profile list # default profile is "default"
* default
Expand All @@ -284,9 +280,6 @@ Quick example
$ volt profile rm foo tyru/caw.vim # disable loading tyru/caw.vim on "foo" profile
$ volt profile destroy foo # will delete profile "foo"
$ volt profile use -current vimrc false # Disable installing vimrc on current profile on "volt build"
$ volt profile use default gvimrc true # Enable installing gvimrc on profile default on "volt build"
```

# volt rm
Expand Down
4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,6 @@ Command
profile rm {name} {repository} [{repository2} ...]
Remove one or more repositories to profile
profile use [-current | {name}] vimrc [true | false]
profile use [-current | {name}] gvimrc [true | false]
Set vimrc / gvimrc flag to true or false.
build [-full]
Build ~/.vim/pack/volt/ directory
Expand Down
16 changes: 7 additions & 9 deletions cmd/builder/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (

type BaseBuilder struct{}

func (builder *BaseBuilder) installVimrcAndGvimrc(profileName, vimrcPath, gvimrcPath string, useVimrc, useGvimrc bool) error {
func (builder *BaseBuilder) installVimrcAndGvimrc(profileName, vimrcPath, gvimrcPath string) error {
// Save old vimrc file as {vimrc}.bak
vimrcInfo, err := os.Stat(vimrcPath)
if err != nil && !os.IsNotExist(err) {
Expand All @@ -39,7 +39,6 @@ func (builder *BaseBuilder) installVimrcAndGvimrc(profileName, vimrcPath, gvimrc
profileName,
pathutil.ProfileVimrc,
vimrcPath,
useVimrc,
)
if err != nil {
return err
Expand All @@ -50,7 +49,6 @@ func (builder *BaseBuilder) installVimrcAndGvimrc(profileName, vimrcPath, gvimrc
profileName,
pathutil.ProfileGvimrc,
gvimrcPath,
useGvimrc,
)
if err != nil {
// Restore old vimrc
Expand All @@ -70,7 +68,7 @@ func (builder *BaseBuilder) installVimrcAndGvimrc(profileName, vimrcPath, gvimrc
return nil
}

func (builder *BaseBuilder) installRCFile(profileName, srcRCFileName, dst string, install bool) error {
func (builder *BaseBuilder) installRCFile(profileName, srcRCFileName, dst string) error {
src := filepath.Join(pathutil.RCDir(profileName), srcRCFileName)

// Return error if destination file does not have magic comment
Expand All @@ -90,8 +88,8 @@ func (builder *BaseBuilder) installRCFile(profileName, srcRCFileName, dst string
return errors.New("failed to remove " + dst)
}

// Skip if use_vimrc/use_gvimrc is false or rc file does not exist
if !install || !pathutil.Exists(src) {
// Skip if rc file does not exist
if !pathutil.Exists(src) {
return nil
}

Expand Down Expand Up @@ -165,17 +163,17 @@ type actionReposResult struct {
files buildinfo.FileMap
}

func (builder *BaseBuilder) getCurrentProfileAndReposList(lockJSON *lockjson.LockJSON) (*lockjson.Profile, lockjson.ReposList, error) {
func (builder *BaseBuilder) getCurrentReposList(lockJSON *lockjson.LockJSON) (lockjson.ReposList, error) {
// Find current profile
profile, err := lockJSON.Profiles.FindByName(lockJSON.CurrentProfileName)
if err != nil {
// this must not be occurred because lockjson.Read()
// validates that the matching profile exists
return nil, nil, err
return nil, err
}

reposList, err := lockJSON.GetReposListByProfile(profile)
return profile, reposList, err
return reposList, err
}

func (builder *BaseBuilder) helptags(reposPath, vimExePath string) error {
Expand Down
4 changes: 2 additions & 2 deletions cmd/builder/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (builder *copyBuilder) Build(buildInfo *buildinfo.BuildInfo, buildReposMap
}

// Get current profile's repos list
profile, reposList, err := builder.getCurrentProfileAndReposList(lockJSON)
reposList, err := builder.getCurrentReposList(lockJSON)
if err != nil {
return err
}
Expand All @@ -48,7 +48,7 @@ func (builder *copyBuilder) Build(buildInfo *buildinfo.BuildInfo, buildReposMap
vimrcPath := filepath.Join(vimDir, pathutil.Vimrc)
gvimrcPath := filepath.Join(vimDir, pathutil.Gvimrc)
err = builder.installVimrcAndGvimrc(
lockJSON.CurrentProfileName, vimrcPath, gvimrcPath, profile.UseVimrc, profile.UseGvimrc,
lockJSON.CurrentProfileName, vimrcPath, gvimrcPath,
)
if err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions cmd/builder/symlink.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (builder *symlinkBuilder) Build(buildInfo *buildinfo.BuildInfo, buildReposM
if err != nil {
return errors.New("could not read lock.json: " + err.Error())
}
profile, reposList, err := builder.getCurrentProfileAndReposList(lockJSON)
reposList, err := builder.getCurrentReposList(lockJSON)
if err != nil {
return err
}
Expand All @@ -45,7 +45,7 @@ func (builder *symlinkBuilder) Build(buildInfo *buildinfo.BuildInfo, buildReposM
vimrcPath := filepath.Join(vimDir, pathutil.Vimrc)
gvimrcPath := filepath.Join(vimDir, pathutil.Gvimrc)
err = builder.installVimrcAndGvimrc(
lockJSON.CurrentProfileName, vimrcPath, gvimrcPath, profile.UseVimrc, profile.UseGvimrc,
lockJSON.CurrentProfileName, vimrcPath, gvimrcPath,
)
if err != nil {
return err
Expand Down
4 changes: 0 additions & 4 deletions cmd/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,6 @@ Command
profile rm {name} {repository} [{repository2} ...]
Remove one or more repositories to profile
profile use [-current | {name}] vimrc [true | false]
profile use [-current | {name}] gvimrc [true | false]
Set vimrc / gvimrc flag to true or false.
build [-full]
Build ~/.vim/pack/volt/ directory
Expand Down
106 changes: 1 addition & 105 deletions cmd/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"flag"
"fmt"
"os"
"strconv"

"github.com/vim-volt/volt/lockjson"
"github.com/vim-volt/volt/logger"
Expand Down Expand Up @@ -57,10 +56,6 @@ Command
profile rm [-current | {name}] {repository} [{repository2} ...]
Remove one or more repositories from profile {name}.
profile use [-current | {name}] vimrc [true | false]
profile use [-current | {name}] gvimrc [true | false]
Set vimrc / gvimrc flag to true or false.
Quick example
$ volt profile list # default profile is "default"
* default
Expand All @@ -81,10 +76,7 @@ Quick example
$ volt disable tyru/caw.vim # disable loading tyru/caw.vim on current profile
$ volt profile rm foo tyru/caw.vim # disable loading tyru/caw.vim on "foo" profile
$ volt profile destroy foo # will delete profile "foo"
$ volt profile use -current vimrc false # Disable installing vimrc on current profile on "volt build"
$ volt profile use default gvimrc true # Enable installing gvimrc on profile default on "volt build"` + "\n\n")
$ volt profile destroy foo # will delete profile "foo"` + "\n\n")
cmd.helped = true
}
return fs
Expand Down Expand Up @@ -119,8 +111,6 @@ func (cmd *profileCmd) Run(args []string) int {
err = cmd.doAdd(args[1:])
case "rm":
err = cmd.doRm(args[1:])
case "use":
err = cmd.doUse(args[1:])
default:
logger.Error("unknown subcommand: " + subCmd)
return 11
Expand Down Expand Up @@ -252,8 +242,6 @@ func (cmd *profileCmd) doShow(args []string) error {
}

fmt.Println("name:", profile.Name)
fmt.Println("use vimrc:", profile.UseVimrc)
fmt.Println("use gvimrc:", profile.UseGvimrc)
fmt.Println("repos path:")
for _, reposPath := range profile.ReposPath {
hash, err := getReposHEAD(reposPath)
Expand Down Expand Up @@ -316,8 +304,6 @@ func (cmd *profileCmd) doNew(args []string) error {
lockJSON.Profiles = append(lockJSON.Profiles, lockjson.Profile{
Name: profileName,
ReposPath: make([]string, 0),
UseVimrc: true,
UseGvimrc: true,
})

// Write to lock.json
Expand Down Expand Up @@ -581,93 +567,3 @@ func (*profileCmd) transactProfile(lockJSON *lockjson.LockJSON, profileName stri
}
return lockJSON, nil
}

func (cmd *profileCmd) doUse(args []string) error {
// Validate arguments
if len(args) != 3 {
cmd.FlagSet().Usage()
logger.Error("'volt profile use' receives profile name, rc name, value.")
return nil
}
if args[1] != "vimrc" && args[1] != "gvimrc" {
cmd.FlagSet().Usage()
logger.Error("volt profile use: Please specify \"vimrc\" or \"gvimrc\" to the 2nd argument")
return nil
}
if args[2] != "true" && args[2] != "false" {
cmd.FlagSet().Usage()
logger.Error("volt profile use: Please specify \"true\" or \"false\" to the 3rd argument")
return nil
}

// Read lock.json
lockJSON, err := lockjson.Read()
if err != nil {
return errors.New("failed to read lock.json: " + err.Error())
}

// Convert arguments
var profileName string
var rcName string
var value bool
if args[0] == "-current" {
profileName = lockJSON.CurrentProfileName
} else {
profileName = args[0]
}
rcName = args[1]
if args[2] == "true" {
value = true
} else {
value = false
}

// Look up specified profile
profile, err := lockJSON.Profiles.FindByName(profileName)
if err != nil {
return err
}

// Begin transaction
err = transaction.Create()
if err != nil {
return err
}
defer transaction.Remove()

// Set use_vimrc / use_gvimrc flag
changed := false
if rcName == "vimrc" {
if profile.UseVimrc != value {
logger.Infof("Set vimrc flag of profile '%s' to '%s'", profileName, strconv.FormatBool(value))
profile.UseVimrc = value
changed = true
} else {
logger.Warnf("vimrc flag of profile '%s' is already '%s'", profileName, strconv.FormatBool(value))
}
} else {
if profile.UseGvimrc != value {
logger.Infof("Set gvimrc flag of profile '%s' to '%s'", profileName, strconv.FormatBool(value))
profile.UseGvimrc = value
changed = true
} else {
logger.Warnf("gvimrc flag of profile '%s' is already '%s'", profileName, strconv.FormatBool(value))
}
}

if changed {
// Write to lock.json
err = lockJSON.Write()
if err != nil {
return err
}
}

// Build ~/.vim/pack/volt dir
err = (&buildCmd{}).doBuild(false)
if err != nil {
return errors.New("could not build " + pathutil.VimVoltDir() + ": " + err.Error())
}

return nil
}
28 changes: 7 additions & 21 deletions cmd/profile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,11 @@ func TestVoltProfileSet(t *testing.T) {

// Checks:
// (a) Output has profile name
// (b) Output has "use vimrc"
// (c) Output has "use gvimrc"
// (d) Output has "repos path"
// (b) Output has "repos path"
//
// * Run `volt profile show <profile>` (`<profile>` is existing profile) (A, B, a, b, c, d)
// * Run `volt profile show -current` (A, B, a, b, c, d)
// * Run `volt profile show <profile>` (`<profile>` is non-existing profile) (!A, !B, !a, !b, !c, !d)
// * Run `volt profile show <profile>` (`<profile>` is existing profile) (A, B, a, b)
// * Run `volt profile show -current` (A, B, a, b)
// * Run `volt profile show <profile>` (`<profile>` is non-existing profile) (!A, !B, !a, !b)
func TestVoltProfileShow(t *testing.T) {
t.Run("Run `volt profile show <profile>` (`<profile>` is existing profile)", func(t *testing.T) {
// =============== setup =============== //
Expand All @@ -140,17 +138,11 @@ func TestVoltProfileShow(t *testing.T) {
// (A, B)
testutil.SuccessExit(t, out, err)

// (a, b, c, d)
// (a, b)
outstr := string(out)
if !strings.Contains(outstr, "name: default\n") {
t.Errorf("Expected 'name: default' line, but got: %s", outstr)
}
if !strings.Contains(outstr, "use vimrc: true\n") {
t.Errorf("Expected 'use vimrc: true' line, but got: %s", outstr)
}
if !strings.Contains(outstr, "use gvimrc: true\n") {
t.Errorf("Expected 'use gvimrc: true' line, but got: %s", outstr)
}
if !strings.Contains(outstr, "repos path:\n") {
t.Errorf("Expected 'repos path:' line, but got: %s", outstr)
}
Expand All @@ -167,17 +159,11 @@ func TestVoltProfileShow(t *testing.T) {
// (A, B)
testutil.SuccessExit(t, out, err)

// (a, b, c, d)
// (a, b)
outstr := string(out)
if !strings.Contains(outstr, "name: default\n") {
t.Errorf("Expected 'name: default' line, but got: %s", outstr)
}
if !strings.Contains(outstr, "use vimrc: true\n") {
t.Errorf("Expected 'use vimrc: true' line, but got: %s", outstr)
}
if !strings.Contains(outstr, "use gvimrc: true\n") {
t.Errorf("Expected 'use gvimrc: true' line, but got: %s", outstr)
}
if !strings.Contains(outstr, "repos path:\n") {
t.Errorf("Expected 'repos path:' line, but got: %s", outstr)
}
Expand All @@ -194,7 +180,7 @@ func TestVoltProfileShow(t *testing.T) {
// (!A, !B)
testutil.FailExit(t, out, err)

// (!a, !b, !c, !d)
// (!a, !b)
outstr := string(out)
expected := "[ERROR] profile 'bar' does not exist"
if strings.Trim(outstr, " \t\r\n") != expected {
Expand Down
4 changes: 0 additions & 4 deletions lockjson/lockjson.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ type profReposPath []string
type Profile struct {
Name string `json:"name"`
ReposPath profReposPath `json:"repos_path"`
UseVimrc bool `json:"use_vimrc"`
UseGvimrc bool `json:"use_gvimrc"`
}

const lockJSONVersion = 2
Expand All @@ -60,8 +58,6 @@ func initialLockJSON() *LockJSON {
Profile{
Name: "default",
ReposPath: make([]string, 0),
UseVimrc: true,
UseGvimrc: true,
},
},
}
Expand Down

0 comments on commit 3d3c27f

Please sign in to comment.