forked from twpayne/chezmoi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.go
63 lines (53 loc) · 1.25 KB
/
update.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package cmd
import (
"fmt"
"strings"
"github.com/spf13/cobra"
vfs "github.com/twpayne/go-vfs"
)
type updateCmdConfig struct {
apply bool
}
var updateCmd = &cobra.Command{
Use: "update",
Args: cobra.NoArgs,
Short: "Pull changes from the source VCS and apply any changes",
RunE: makeRunE(config.runUpdateCmd),
}
func init() {
rootCmd.AddCommand(updateCmd)
persistentFlags := updateCmd.PersistentFlags()
persistentFlags.BoolVarP(&config.update.apply, "apply", "a", true, "apply after pulling")
}
func (c *Config) runUpdateCmd(fs vfs.FS, args []string) error {
vcsInfo, err := c.getVCSInfo()
if err != nil {
return err
}
var pullArgs []string
if c.SourceVCS.Pull != nil {
switch v := c.SourceVCS.Pull.(type) {
case string:
pullArgs = strings.Split(v, " ")
case []string:
pullArgs = v
default:
return fmt.Errorf("sourceVCS.pull: cannot parse value")
}
} else {
pullArgs = vcsInfo.pullArgs
}
if pullArgs == nil {
return fmt.Errorf("%s: pull not supported", c.SourceVCS.Command)
}
if err := c.run(c.SourceDir, c.SourceVCS.Command, pullArgs...); err != nil {
return err
}
if c.update.apply {
mutator := c.getDefaultMutator(fs)
if err := c.applyArgs(fs, nil, mutator); err != nil {
return err
}
}
return nil
}