From 96f6e2e11a6e3701d8cca5e56b101adf2db5388f Mon Sep 17 00:00:00 2001 From: Allen Zhong Date: Tue, 13 Apr 2021 14:53:50 +0800 Subject: [PATCH] tiup: update `mirror` command (#1302) --- cmd/mirror.go | 54 ++++++++++++++++++++++++++++++++++++++++----------- cmd/root.go | 18 +++++++++++++---- 2 files changed, 57 insertions(+), 15 deletions(-) diff --git a/cmd/mirror.go b/cmd/mirror.go index c5c31f65ad..5bbad0692f 100644 --- a/cmd/mirror.go +++ b/cmd/mirror.go @@ -141,18 +141,28 @@ func newMirrorSignCmd() *cobra.Command { // the `mirror set` sub command func newMirrorSetCmd() *cobra.Command { - root := "" + var ( + root string + reset bool + ) cmd := &cobra.Command{ Use: "set ", - Short: "set mirror address", - Long: "set mirror address, will replace the root certificate", + Short: "Set mirror address", + Long: `Set mirror address, the address could be an URL or a path to the repository +directory. Relative paths will not be expanded, so absolute paths are recommended. +The root manifest in $TIUP_HOME will be replaced with the one in given repository automatically.`, RunE: func(cmd *cobra.Command, args []string) error { - if len(args) != 1 { + if !reset && len(args) != 1 { return cmd.Help() } - addr := args[0] - profile := environment.GlobalEnv().Profile() + var addr string + if reset { + addr = repository.DefaultMirror + } else { + addr = args[0] + } + profile := localdata.InitProfile() if err := profile.ResetMirror(addr, root); err != nil { log.Errorf("Failed to set mirror: %s\n", err.Error()) return err @@ -162,6 +172,8 @@ func newMirrorSetCmd() *cobra.Command { }, } cmd.Flags().StringVarP(&root, "root", "r", root, "Specify the path of `root.json`") + cmd.Flags().BoolVar(&reset, "reset", false, "Reset mirror to use the default address.") + return cmd } @@ -655,8 +667,9 @@ func newMirrorInitCmd() *cobra.Command { cmd := &cobra.Command{ Use: "init ", Short: "Initialize an empty repository", - Long: `Initialize an empty TiUP repository at given path. If path is not specified, the -current working directory (".") will be used.`, + Long: `Initialize an empty TiUP repository at given path. +The specified path must be an empty directory. +If the path does not exist, a new directory will be created.`, RunE: func(cmd *cobra.Command, args []string) error { if len(args) != 1 { return cmd.Help() @@ -666,6 +679,7 @@ current working directory (".") will be used.`, // create the target path if not exist if utils.IsNotExist(repoPath) { var err error + log.Infof("Target path \"%s\" does not exist, creating new directory...", repoPath) if err = os.Mkdir(repoPath, 0755); err != nil { return err } @@ -686,13 +700,21 @@ current working directory (".") will be used.`, }, } - cmd.Flags().StringVarP(&keyDir, "key-dir", "k", "", "Path to write the private key file") + cmd.Flags().StringVarP(&keyDir, "key-dir", "k", "", "Path to write the private key files") return cmd } func initRepo(path, keyDir string) error { - return v1manifest.Init(path, keyDir, time.Now().UTC()) + log.Infof("Initializing empty new repository at \"%s\", private keys will be stored in \"%s\"...", path, keyDir) + err := v1manifest.Init(path, keyDir, time.Now().UTC()) + if err != nil { + log.Errorf("Initializing new repository failed.") + return err + } + log.Infof("New repository initialized at \"%s\", private keys are stored in \"%s\".", path, keyDir) + log.Infof("Use `tiup mirror set` command to set and use the new repository.") + return nil } // the `mirror merge` sub command @@ -809,7 +831,17 @@ func newMirrorCloneCmd() *cobra.Command { return spec.TiDBComponentVersion(comp, "") } - return repository.CloneMirror(repo, components, versionMapper, args[0], args[1:], options) + // format input versions + versionList := make([]string, 0) + for _, ver := range args[1:] { + v, err := utils.FmtVer(ver) + if err != nil { + return err + } + versionList = append(versionList, v) + } + + return repository.CloneMirror(repo, components, versionMapper, args[0], versionList, options) }, } diff --git a/cmd/root.go b/cmd/root.go index 0aceb73e6d..e518a0dffd 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -58,11 +58,21 @@ the latest stable version will be downloaded from the repository.`, if printVersion && len(args) == 0 { return nil } - e, err := environment.InitEnv(repoOpts) - if err != nil { - return err + switch cmd.Name() { + case "init", + "set": + if cmd.HasParent() && cmd.Parent().Name() == "mirror" { + // skip environment init + break + } + fallthrough + default: + e, err := environment.InitEnv(repoOpts) + if err != nil { + return err + } + environment.SetGlobalEnv(e) } - environment.SetGlobalEnv(e) return nil }, RunE: func(cmd *cobra.Command, args []string) error {