github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/cmd/update/update.go (about)

     1  // Copyright (c) 2022 IoTeX Foundation
     2  // This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
     3  // or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
     4  // This source code is governed by Apache License 2.0 that can be found in the LICENSE file.
     5  
     6  package update
     7  
     8  import (
     9  	"fmt"
    10  	"os/exec"
    11  
    12  	"github.com/spf13/cobra"
    13  
    14  	"github.com/iotexproject/iotex-core/ioctl/config"
    15  	"github.com/iotexproject/iotex-core/ioctl/output"
    16  )
    17  
    18  // Multi-language support
    19  var (
    20  	_updateCmdUses = map[config.Language]string{
    21  		config.English: "update [-t version-type]",
    22  		config.Chinese: "update [-t 版本类型]",
    23  	}
    24  	_updateCmdShorts = map[config.Language]string{
    25  		config.English: "Update ioctl with latest version",
    26  		config.Chinese: "使用最新版本更新ioctl",
    27  	}
    28  	_flagVersionTypeUsages = map[config.Language]string{
    29  		config.English: `set version type, "stable" or "unstable"`,
    30  		config.Chinese: `设置版本类型, "稳定版" 或 "非稳定版"`,
    31  	}
    32  )
    33  
    34  var (
    35  	_versionType string
    36  )
    37  
    38  // UpdateCmd represents the update command
    39  var UpdateCmd = &cobra.Command{
    40  	Use:   config.TranslateInLang(_updateCmdUses, config.UILanguage),
    41  	Short: config.TranslateInLang(_updateCmdShorts, config.UILanguage),
    42  	Args:  cobra.ExactArgs(0),
    43  	RunE: func(cmd *cobra.Command, args []string) error {
    44  		cmd.SilenceUsage = true
    45  		err := update()
    46  		return err
    47  	},
    48  }
    49  
    50  func init() {
    51  	UpdateCmd.Flags().StringVarP(&_versionType, "version-type", "t", "stable",
    52  		config.TranslateInLang(_flagVersionTypeUsages, config.UILanguage))
    53  }
    54  
    55  func update() error {
    56  	var cmdString string
    57  	switch _versionType {
    58  	default:
    59  		return output.NewError(output.FlagError, "invalid version-type flag: "+_versionType, nil)
    60  	case "stable":
    61  		cmdString = "curl --silent https://raw.githubusercontent.com/iotexproject/" +
    62  			"iotex-core/master/install-cli.sh | sh"
    63  	case "unstable":
    64  		cmdString = "curl --silent https://raw.githubusercontent.com/iotexproject/" +
    65  			"iotex-core/master/install-cli.sh | sh -s \"unstable\""
    66  
    67  	}
    68  	cmd := exec.Command("bash", "-c", cmdString)
    69  	output.PrintResult(fmt.Sprintf("Downloading the latest %s version ...\n", _versionType))
    70  	err := cmd.Run()
    71  	if err != nil {
    72  		return output.NewError(output.UpdateError, "failed to update ioctl", err)
    73  	}
    74  	output.PrintResult("ioctl is up-to-date now.")
    75  	return nil
    76  }