|
| 1 | +package subcmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "flag" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "os/exec" |
| 9 | + |
| 10 | + "github.com/vim-volt/volt/config" |
| 11 | + "github.com/vim-volt/volt/lockjson" |
| 12 | + "github.com/vim-volt/volt/logger" |
| 13 | + "github.com/vim-volt/volt/pathutil" |
| 14 | + "github.com/vim-volt/volt/subcmd/builder" |
| 15 | +) |
| 16 | + |
| 17 | +func init() { |
| 18 | + cmdMap["edit"] = &editCmd{} |
| 19 | +} |
| 20 | + |
| 21 | +type editCmd struct { |
| 22 | + helped bool |
| 23 | + editor string |
| 24 | +} |
| 25 | + |
| 26 | +func (cmd *editCmd) ProhibitRootExecution(args []string) bool { return true } |
| 27 | + |
| 28 | +func (cmd *editCmd) FlagSet() *flag.FlagSet { |
| 29 | + fs := flag.NewFlagSet(os.Args[0], flag.ContinueOnError) |
| 30 | + fs.SetOutput(os.Stdout) |
| 31 | + fs.Usage = func() { |
| 32 | + fmt.Print(` |
| 33 | +Usage |
| 34 | + volt edit [-help] [-e|--editor {editor}] {repository} [{repository2} ...] |
| 35 | +
|
| 36 | +Quick example |
| 37 | + $ volt edit tyru/caw.vim # will open the plugconf file for tyru/caw.vim for editing |
| 38 | +
|
| 39 | +Description |
| 40 | + Open the plugconf file(s) of one or more {repository} for editing. |
| 41 | +
|
| 42 | + If the -e option was given, use the given editor for editing those files (unless it cannot be found) |
| 43 | +
|
| 44 | + It also calls "volt build" afterwards if modifications were made to the plugconf file(s).` + "\n\n") |
| 45 | + //fmt.Println("Options") |
| 46 | + //fs.PrintDefaults() |
| 47 | + fmt.Println() |
| 48 | + cmd.helped = true |
| 49 | + } |
| 50 | + fs.StringVar(&cmd.editor, "editor", "", "Use the given editor for editing the plugconf files") |
| 51 | + fs.StringVar(&cmd.editor, "e", "", "Use the given editor for editing the plugconf files") |
| 52 | + return fs |
| 53 | +} |
| 54 | + |
| 55 | +func (cmd *editCmd) Run(args []string) *Error { |
| 56 | + reposPathList, err := cmd.parseArgs(args) |
| 57 | + if err == ErrShowedHelp { |
| 58 | + return nil |
| 59 | + } |
| 60 | + if err != nil { |
| 61 | + return &Error{Code: 10, Msg: "Failed to parse args: " + err.Error()} |
| 62 | + } |
| 63 | + |
| 64 | + hasChanges, err := cmd.doEdit(reposPathList) |
| 65 | + if err != nil { |
| 66 | + return &Error{Code: 15, Msg: "Failed to edit plugconf file: " + err.Error()} |
| 67 | + } |
| 68 | + |
| 69 | + // Build opt dir |
| 70 | + if hasChanges { |
| 71 | + err = builder.Build(false) |
| 72 | + if err != nil { |
| 73 | + return &Error{Code: 12, Msg: "Could not build " + pathutil.VimVoltDir() + ": " + err.Error()} |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + return nil |
| 78 | +} |
| 79 | + |
| 80 | +func (cmd *editCmd) doEdit(reposPathList []pathutil.ReposPath) (bool, error) { |
| 81 | + // Read lock.json |
| 82 | + lockJSON, err := lockjson.Read() |
| 83 | + if err != nil { |
| 84 | + return false, err |
| 85 | + } |
| 86 | + |
| 87 | + // Read config.toml |
| 88 | + cfg, err := config.Read() |
| 89 | + if err != nil { |
| 90 | + return false, errors.New("could not read config.toml: " + err.Error()) |
| 91 | + } |
| 92 | + |
| 93 | + editor, err := cmd.identifyEditor(cfg) |
| 94 | + if err != nil || editor == "" { |
| 95 | + return false, &Error{Code: 30, Msg: "No usable editor found"} |
| 96 | + } |
| 97 | + |
| 98 | + changeWasMade := false |
| 99 | + for _, reposPath := range reposPathList { |
| 100 | + |
| 101 | + // Edit plugconf file |
| 102 | + plugconfPath := reposPath.Plugconf() |
| 103 | + |
| 104 | + // Install a new template if none exists |
| 105 | + if !pathutil.Exists(plugconfPath) { |
| 106 | + getCmd := new(getCmd) |
| 107 | + logger.Debugf("Installing new plugconf for '%s'.", reposPath) |
| 108 | + getCmd.downloadPlugconf(reposPath) |
| 109 | + } |
| 110 | + |
| 111 | + // Remember modification time before opening the editor |
| 112 | + info, err := os.Stat(plugconfPath) |
| 113 | + if err != nil { |
| 114 | + return false, err |
| 115 | + } |
| 116 | + mTimeBefore := info.ModTime() |
| 117 | + |
| 118 | + // Call the editor with the plugconf file |
| 119 | + editorCmd := exec.Command(editor, plugconfPath) |
| 120 | + editorCmd.Stdin = os.Stdin |
| 121 | + editorCmd.Stdout = os.Stdout |
| 122 | + if err = editorCmd.Run(); err != nil { |
| 123 | + logger.Error("Error calling editor for '%s': %s", reposPath, err.Error) |
| 124 | + continue |
| 125 | + } |
| 126 | + |
| 127 | + // Get modification time after closing the editor |
| 128 | + info, err = os.Stat(plugconfPath) |
| 129 | + if err != nil { |
| 130 | + return false, err |
| 131 | + } |
| 132 | + mTimeAfter := info.ModTime() |
| 133 | + |
| 134 | + // A change was made if the modification time was updated |
| 135 | + changeWasMade = changeWasMade || mTimeAfter.After(mTimeBefore) |
| 136 | + |
| 137 | + // Remove repository from lock.json |
| 138 | + err = lockJSON.Repos.RemoveAllReposPath(reposPath) |
| 139 | + err2 := lockJSON.Profiles.RemoveAllReposPath(reposPath) |
| 140 | + if err == nil || err2 == nil { |
| 141 | + // ignore? |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + // Write to lock.json |
| 146 | + if err = lockJSON.Write(); err != nil { |
| 147 | + return changeWasMade, err |
| 148 | + } |
| 149 | + return changeWasMade, nil |
| 150 | +} |
| 151 | + |
| 152 | +func (cmd *editCmd) parseArgs(args []string) (pathutil.ReposPathList, error) { |
| 153 | + fs := cmd.FlagSet() |
| 154 | + fs.Parse(args) |
| 155 | + if cmd.helped { |
| 156 | + return nil, ErrShowedHelp |
| 157 | + } |
| 158 | + |
| 159 | + if len(fs.Args()) == 0 { |
| 160 | + fs.Usage() |
| 161 | + return nil, errors.New("repository was not given") |
| 162 | + } |
| 163 | + |
| 164 | + // Normalize repos path |
| 165 | + reposPathList := make(pathutil.ReposPathList, 0, len(fs.Args())) |
| 166 | + for _, arg := range fs.Args() { |
| 167 | + reposPath, err := pathutil.NormalizeRepos(arg) |
| 168 | + if err != nil { |
| 169 | + return nil, err |
| 170 | + } |
| 171 | + reposPathList = append(reposPathList, reposPath) |
| 172 | + } |
| 173 | + |
| 174 | + return reposPathList, nil |
| 175 | +} |
| 176 | + |
| 177 | +func (cmd *editCmd) identifyEditor(cfg *config.Config) (string, error) { |
| 178 | + editors := make([]string, 0, 6) |
| 179 | + |
| 180 | + // if an editor is specified as commandline argument, consider it |
| 181 | + // as alternative |
| 182 | + if cmd.editor != "" { |
| 183 | + editors = append(editors, cmd.editor) |
| 184 | + } |
| 185 | + |
| 186 | + // if an editor is configured in the config.toml, consider it as |
| 187 | + // alternative |
| 188 | + if cfg.Edit.Editor != "" { |
| 189 | + editors = append(editors, cfg.Edit.Editor) |
| 190 | + } |
| 191 | + |
| 192 | + vimExecutable, err := pathutil.VimExecutable() |
| 193 | + if err != nil { |
| 194 | + logger.Debug("No vim executable found in $PATH") |
| 195 | + } else { |
| 196 | + editors = append(editors, vimExecutable) |
| 197 | + } |
| 198 | + |
| 199 | + // specifiy a fixed list of other alternatives |
| 200 | + editors = append(editors, "$VISUAL", "sensible-editor", "$EDITOR") |
| 201 | + |
| 202 | + for _, editor := range editors { |
| 203 | + // resolve content of environment variables |
| 204 | + var editorName string |
| 205 | + if editor[0] == '$' { |
| 206 | + editorName = os.Getenv(editor[1:]) |
| 207 | + } else { |
| 208 | + editorName = editor |
| 209 | + } |
| 210 | + |
| 211 | + path, err := exec.LookPath(editorName) |
| 212 | + if err != nil { |
| 213 | + logger.Debug(editorName + " not found in $PATH") |
| 214 | + } else if path != "" { |
| 215 | + logger.Debug("Using " + path + " as editor") |
| 216 | + return editorName, nil |
| 217 | + } |
| 218 | + } |
| 219 | + |
| 220 | + return "", errors.New("No usable editor found") |
| 221 | +} |
0 commit comments