Skip to content

Commit

Permalink
tiup: update mirror command (#1302)
Browse files Browse the repository at this point in the history
  • Loading branch information
AstroProfundis committed Apr 25, 2021
1 parent 77c17c6 commit 96f6e2e
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 15 deletions.
54 changes: 43 additions & 11 deletions cmd/mirror.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 <mirror-addr>",
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
Expand All @@ -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
}

Expand Down Expand Up @@ -655,8 +667,9 @@ func newMirrorInitCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "init <path>",
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()
Expand All @@ -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
}
Expand All @@ -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
Expand Down Expand Up @@ -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)
},
}

Expand Down
18 changes: 14 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 96f6e2e

Please sign in to comment.