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

     1  // Copyright (c) 2022 IoTeX
     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  
    11  	"github.com/pkg/errors"
    12  	"github.com/spf13/cobra"
    13  
    14  	"github.com/iotexproject/iotex-core/ioctl"
    15  	"github.com/iotexproject/iotex-core/ioctl/config"
    16  )
    17  
    18  // Multi-language support
    19  var (
    20  	_shorts = map[config.Language]string{
    21  		config.English: "Update password for IoTeX account",
    22  		config.Chinese: "为IoTeX账户更新密码",
    23  	}
    24  	_uses = map[config.Language]string{
    25  		config.English: "update [ALIAS|ADDRESS]",
    26  		config.Chinese: "update [别名|地址]",
    27  	}
    28  	_flagUsages = map[config.Language]string{
    29  		config.English: `set version type, "stable" or "unstable"`,
    30  		config.Chinese: `设置版本类型, "稳定版" 或 "非稳定版"`,
    31  	}
    32  	_invalidVersionType = map[config.Language]string{
    33  		config.English: "invalid version-type flag:%s",
    34  		config.Chinese: "无效版本状态:%s",
    35  	}
    36  	_resultSuccess = map[config.Language]string{
    37  		config.English: "ioctl is up-to-date now.",
    38  		config.Chinese: "ioctl 现已更新完毕。",
    39  	}
    40  	_resultFail = map[config.Language]string{
    41  		config.English: "failed to update ioctl",
    42  		config.Chinese: "ioctl 更新失败",
    43  	}
    44  	_resultInfo = map[config.Language]string{
    45  		config.English: "Downloading the latest %s version ...\n",
    46  		config.Chinese: "正在下载最新的 %s 版本 ...\n",
    47  	}
    48  	_infoWarn = map[config.Language]string{
    49  		config.English: "Type 'YES' to continue, quit for anything else.",
    50  		config.Chinese: "输入 'YES' 以继续, 否则退出",
    51  	}
    52  	_infoQuit = map[config.Language]string{
    53  		config.English: "quit",
    54  		config.Chinese: "退出",
    55  	}
    56  )
    57  
    58  // NewUpdateCmd represents the update command
    59  func NewUpdateCmd(c ioctl.Client) *cobra.Command {
    60  	var versionType string
    61  
    62  	use, _ := c.SelectTranslation(_uses)
    63  	short, _ := c.SelectTranslation(_shorts)
    64  	flagUsage, _ := c.SelectTranslation(_flagUsages)
    65  	success, _ := c.SelectTranslation(_resultSuccess)
    66  	fail, _ := c.SelectTranslation(_resultFail)
    67  	info, _ := c.SelectTranslation(_resultInfo)
    68  	invalidVersionType, _ := c.SelectTranslation(_invalidVersionType)
    69  	_infoWarn, _ := c.SelectTranslation(_infoWarn)
    70  	_infoQuit, _ := c.SelectTranslation(_infoQuit)
    71  
    72  	uc := &cobra.Command{
    73  		Use:   use,
    74  		Short: short,
    75  		Args:  cobra.ExactArgs(0),
    76  		RunE: func(cmd *cobra.Command, args []string) error {
    77  			cmd.SilenceUsage = true
    78  			var cmdString string
    79  			switch versionType {
    80  			case "stable":
    81  				cmdString = "curl --silent https://raw.githubusercontent.com/iotexproject/" + "iotex-core/master/install-cli.sh | sh"
    82  			case "unstable":
    83  				cmdString = "curl --silent https://raw.githubusercontent.com/iotexproject/" + "iotex-core/master/install-cli.sh | sh -s \"unstable\""
    84  			default:
    85  				return errors.New(fmt.Sprintf(invalidVersionType, versionType))
    86  			}
    87  
    88  			confirmed, err := c.AskToConfirm(_infoWarn)
    89  			if err != nil {
    90  				return errors.Wrap(err, "failed to ask confirm")
    91  			}
    92  			if !confirmed {
    93  				cmd.Println(_infoQuit)
    94  				return nil
    95  			}
    96  			cmd.Printf(info, versionType)
    97  
    98  			if err = c.Execute(cmdString); err != nil {
    99  				return errors.Wrap(err, fail)
   100  			}
   101  			cmd.Println(success)
   102  			return nil
   103  		},
   104  	}
   105  
   106  	uc.Flags().StringVarP(&versionType, "version-type", "t", "stable", flagUsage)
   107  	return uc
   108  }