From 5815fc6d33ab5c5429ffe9d148fa23e1e09ae7f0 Mon Sep 17 00:00:00 2001 From: nexustar Date: Fri, 6 May 2022 17:16:22 +0800 Subject: [PATCH] playground: make playground tag has higher priority than tiup --- cmd/root.go | 12 ++++++++++-- components/playground/main.go | 21 +++++++++------------ 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index e9422ced8a..abd7edfd52 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -142,17 +142,25 @@ the latest stable version will be downloaded from the repository.`, return nil case "--binpath": if len(args) < 2 { - return fmt.Errorf("flag needs an argument: %s", args[0]) + return fmt.Errorf("flag %s needs an argument", args[0]) } binPath = args[1] args = args[2:] case "--tag", "-T": if len(args) < 2 { - return fmt.Errorf("flag needs an argument: %s", args[0]) + return fmt.Errorf("flag %s needs an argument", args[0]) } tag = args[1] args = args[2:] } + + // component may use tag from environment variable. as workaround, make tiup set the same tag + for i := 0; i < len(args)-1; i++ { + if args[i] == "--tag" || args[i] == "-T" { + tag = args[i+1] + } + } + if len(args) < 1 { return cmd.Help() } diff --git a/components/playground/main.go b/components/playground/main.go index bdade14803..80305937d7 100644 --- a/components/playground/main.go +++ b/components/playground/main.go @@ -164,24 +164,21 @@ Examples: if tiupHome == "" { tiupHome, _ = getAbsolutePath(filepath.Join("~", localdata.ProfileDirName)) } - if tiupDataDir == "" { - if tag == "" { - dataDir = filepath.Join(tiupHome, localdata.DataParentDir, utils.Base62Tag()) - } else { - dataDir = filepath.Join(tiupHome, localdata.DataParentDir, tag) - } - if dataDir == "" { - return errors.Errorf("cannot read environment variable %s nor %s", localdata.EnvNameInstanceDataDir, localdata.EnvNameHome) - } + switch { + case tag != "": + dataDir = filepath.Join(tiupHome, localdata.DataParentDir, tag) err := os.MkdirAll(dataDir, os.ModePerm) if err != nil { return err } - } else { + case tiupDataDir != "": dataDir = tiupDataDir + tag = dataDir[strings.LastIndex(dataDir, "/")+1:] + default: + tag = utils.Base62Tag() + dataDir = filepath.Join(tiupHome, localdata.DataParentDir, tag) } - instanceName := dataDir[strings.LastIndex(dataDir, "/")+1:] - fmt.Printf("\033]0;TiUP Playground: %s\a", instanceName) + fmt.Printf("\033]0;TiUP Playground: %s\a", tag) return nil }, RunE: func(cmd *cobra.Command, args []string) error {