-
Notifications
You must be signed in to change notification settings - Fork 6
/
update.go
102 lines (91 loc) 路 2.99 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package cmd
import (
"fmt"
"time"
"github.com/criteo/command-launcher/internal/config"
"github.com/criteo/command-launcher/internal/console"
"github.com/criteo/command-launcher/internal/context"
"github.com/criteo/command-launcher/internal/repository"
"github.com/criteo/command-launcher/internal/updater"
"github.com/criteo/command-launcher/internal/user"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
type UpdateFlags struct {
Package bool
Self bool
Timeout time.Duration
}
var (
updateFlags = UpdateFlags{}
)
func AddUpdateCmd(rootCmd *cobra.Command, appCtx context.LauncherContext, localRepo repository.PackageRepository) {
appName := appCtx.AppName()
updateCmd := &cobra.Command{
Use: "update",
Short: fmt.Sprintf("Update %s, or its commands", appName),
Long: fmt.Sprintf(`
Check the update of %s and its commands.
`, appName),
Example: fmt.Sprintf(`
%s update --package
%s update --self
`, appName, appName),
RunE: func(cmd *cobra.Command, args []string) error {
u, err := user.GetUser()
if err != nil {
log.Errorln(err)
}
if updateFlags.Self {
console.Highlight("checking available %s version ...", appCtx.AppName())
selfUpdater := updater.SelfUpdater{
BinaryName: appCtx.AppName(),
LatestVersionUrl: viper.GetString(config.SELF_UPDATE_LATEST_VERSION_URL_KEY),
SelfUpdateRootUrl: viper.GetString(config.SELF_UPDATE_BASE_URL_KEY),
User: u,
CurrentVersion: appCtx.AppVersion(),
Timeout: updateFlags.Timeout,
}
selfUpdater.CheckUpdateAsync()
err := selfUpdater.Update()
if err != nil {
console.Error(err.Error())
} else {
console.Success("%s is up-to-date", appCtx.AppName())
}
}
if updateFlags.Package {
console.Highlight("checking available package updates ...")
enableCI := viper.GetBool(config.CI_ENABLED_KEY)
packageLockFile := viper.GetString(config.PACKAGE_LOCK_FILE_KEY)
if enableCI {
fmt.Printf("CI mode enabled, load package lock file: %s\n", packageLockFile)
}
cmdUpdater := updater.CmdUpdater{
LocalRepo: localRepo,
CmdRepositoryBaseUrl: viper.GetString(config.COMMAND_REPOSITORY_BASE_URL_KEY),
User: u,
Timeout: updateFlags.Timeout,
EnableCI: enableCI,
PackageLockFile: packageLockFile,
}
cmdUpdater.CheckUpdateAsync()
err := cmdUpdater.Update()
if err != nil {
console.Error(err.Error())
} else {
console.Success("packages are up-to-date")
}
}
if !updateFlags.Package && !updateFlags.Self {
cmd.Help()
}
return nil
},
}
updateCmd.Flags().BoolVarP(&updateFlags.Package, "package", "p", false, "Update packages and commands")
updateCmd.Flags().BoolVarP(&updateFlags.Self, "self", "s", false, "Self update")
updateCmd.Flags().DurationVarP(&updateFlags.Timeout, "timeout", "t", 10*time.Second, "Timeout for update operations")
rootCmd.AddCommand(updateCmd)
}