/
update.go
77 lines (67 loc) · 2.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
70
71
72
73
74
75
76
77
// Copyright (c) 2019 IoTeX Foundation
// This is an alpha (internal) release and is not suitable for production. This source code is provided 'as is' and no
// warranties are given as to title or non-infringement, merchantability or fitness for purpose and, to the extent
// permitted by law, all liability for your use of the code is disclaimed. This source code is governed by Apache
// License 2.0 that can be found in the LICENSE file.
package update
import (
"fmt"
"os/exec"
"github.com/spf13/cobra"
"github.com/iotexproject/iotex-core/ioctl/config"
"github.com/iotexproject/iotex-core/ioctl/output"
)
// Multi-language support
var (
updateCmdUses = map[config.Language]string{
config.English: "update [-t version-type]",
config.Chinese: "update [-t 版本类型]",
}
updateCmdShorts = map[config.Language]string{
config.English: "Update ioctl with latest version",
config.Chinese: "使用最新版本更新ioctl",
}
flagVersionTypeUsages = map[config.Language]string{
config.English: `set version type, "stable" or "unstable"`,
config.Chinese: `设置版本类型, "稳定版" 或 "非稳定版"`,
}
)
var (
versionType string
)
// UpdateCmd represents the update command
var UpdateCmd = &cobra.Command{
Use: config.TranslateInLang(updateCmdUses, config.UILanguage),
Short: config.TranslateInLang(updateCmdShorts, config.UILanguage),
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
err := update()
return err
},
}
func init() {
UpdateCmd.Flags().StringVarP(&versionType, "version-type", "t", "stable",
config.TranslateInLang(flagVersionTypeUsages, config.UILanguage))
}
func update() error {
var cmdString string
switch versionType {
default:
return output.NewError(output.FlagError, "invalid version-type flag: "+versionType, nil)
case "stable":
cmdString = "curl --silent https://raw.githubusercontent.com/iotexproject/" +
"iotex-core/master/install-cli.sh | sh"
case "unstable":
cmdString = "curl --silent https://raw.githubusercontent.com/iotexproject/" +
"iotex-core/master/install-cli.sh | sh -s \"unstable\""
}
cmd := exec.Command("bash", "-c", cmdString)
output.PrintResult(fmt.Sprintf("Downloading the latest %s version ...\n", versionType))
err := cmd.Run()
if err != nil {
return output.NewError(output.UpdateError, "failed to update ioctl", err)
}
output.PrintResult("ioctl is up-to-date now.")
return nil
}