/
update.go
69 lines (59 loc) · 1.4 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
64
65
66
67
68
69
package cmd
import (
"fmt"
"strings"
"github.com/spf13/cobra"
)
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",
Long: mustGetLongHelp("update"),
Example: getExample("update"),
PreRunE: config.ensureNoError,
RunE: 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(cmd *cobra.Command, args []string) error {
vcs, err := c.getVCS()
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 = vcs.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 {
persistentState, err := c.getPersistentState(nil)
if err != nil {
return err
}
defer persistentState.Close()
if err := c.applyArgs(nil, persistentState); err != nil {
return err
}
}
return nil
}