Skip to content

Commit

Permalink
chore: use slices instead map
Browse files Browse the repository at this point in the history
  • Loading branch information
giautm committed Sep 29, 2024
1 parent e339995 commit 3fcf7d0
Showing 1 changed file with 21 additions and 49 deletions.
70 changes: 21 additions & 49 deletions prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,10 +331,10 @@ func (i *InitActionCmd) handleAtlasConfig(ctx context.Context, configs []string,
if len(envs) == 0 {
return nil
}
return i.chooseEnv(envs, config)
return i.chooseEnv(envs)
}

func (i *InitActionCmd) getEnvs(ctx context.Context, path string, cr RepoExplorer) (envs map[string]gen.Env, err error) {
func (i *InitActionCmd) getEnvs(ctx context.Context, path string, cr RepoExplorer) (envs []gen.Env, err error) {
content, err := cr.ReadContent(ctx, path)
if err != nil {
return nil, err
Expand All @@ -343,7 +343,7 @@ func (i *InitActionCmd) getEnvs(ctx context.Context, path string, cr RepoExplore
if len(diags) > 0 {
return nil, fmt.Errorf("failed to parse %s: %s", path, diags.Error())
}
envs = make(map[string]gen.Env)
envs = []gen.Env{}
for _, b := range file.Body.(*hclsyntax.Body).Blocks {
if b.Type == "env" {
var (
Expand All @@ -353,10 +353,7 @@ func (i *InitActionCmd) getEnvs(ctx context.Context, path string, cr RepoExplore
hasSchemaSrc bool
hasRepoName bool
)
if len(b.Labels) == 0 {
//TODO: fix, it may conflict with other envs names
name = UnnamedEnv
} else {
if len(b.Labels) != 0 {
name = b.Labels[0]
}
_, hasDevURL = b.Body.Attributes["dev"]
Expand All @@ -371,14 +368,14 @@ func (i *InitActionCmd) getEnvs(ctx context.Context, path string, cr RepoExplore
}
}
}
envs[name] = gen.Env{
envs = append(envs, gen.Env{
Name: name,
HasDevURL: hasDevURL,
HasURL: hasURL,
HasSchemaSrc: hasSchemaSrc,
HasRepoName: hasRepoName,
Path: path,
}
})
}
}
return envs, nil
Expand All @@ -400,59 +397,34 @@ func (i *InitActionCmd) chooseConfig(configs []string) (string, error) {
return config, err
}

func (i *InitActionCmd) chooseEnv(envs map[string]gen.Env, config string) error {
envNames := make([]string, 0, len(envs))
hasUnnamedEnv := false
for k := range envs {
envNames = append(envNames, k)
if k == UnnamedEnv {
hasUnnamedEnv = true
func (i *InitActionCmd) chooseEnv(envs []gen.Env) error {
choose := 0
if len(envs) > 1 {
prompt := promptui.Select{
Label: "Choose an environment",
HideHelp: true,
Items: envs,
Stdin: i.stdin,
Templates: &promptui.SelectTemplates{
Selected: fmt.Sprintf(`{{ "%s" | green }} {{ "Config env: " | faint }} {{ .Name | "Other" }}`, promptui.IconGood),
},
}
}
if len(envNames) == 1 && hasUnnamedEnv {
name, err := i.promptForEnvName()
var err error
choose, _, err = prompt.Run()
if err != nil {
return err
}
var env gen.Env
for _, v := range envs {
env = v
}
env.Name = name
i.env = env
return nil
}
if len(envNames) == 1 {
i.env = envs[envNames[0]]
return nil
}
prompt := promptui.Select{
Label: "Choose an environment",
HideHelp: true,
Items: envNames,
Stdin: i.stdin,
Templates: &promptui.SelectTemplates{
Selected: fmt.Sprintf(`{{ "%s" | green }} {{ "Config env: " | faint }} {{ . }}`, promptui.IconGood),
},
}
_, ans, err := prompt.Run()
if err != nil {
return err
}
if ans == UnnamedEnv {
env := envs[choose]
if env.Name == "" {
name, err := i.promptForEnvName()
if err != nil {
return err
}
var env gen.Env
for _, v := range envs {
env = v
}
env.Name = name
i.env = env
return nil
}
i.env = envs[ans]
return nil
}

Expand Down

0 comments on commit 3fcf7d0

Please sign in to comment.