|
| 1 | +package emulator |
| 2 | + |
| 3 | +import ( |
| 4 | + "sort" |
| 5 | + |
| 6 | + "github.com/spf13/cobra" |
| 7 | + "github.com/spf13/viper" |
| 8 | + |
| 9 | + e "github.com/cloudposse/atmos/internal/exec" |
| 10 | + cfg "github.com/cloudposse/atmos/pkg/config" |
| 11 | + "github.com/cloudposse/atmos/pkg/flags" |
| 12 | + l "github.com/cloudposse/atmos/pkg/list" |
| 13 | + "github.com/cloudposse/atmos/pkg/perf" |
| 14 | + "github.com/cloudposse/atmos/pkg/schema" |
| 15 | +) |
| 16 | + |
| 17 | +// globalInfoForCompletion builds a minimal ConfigAndStacksInfo from global flags |
| 18 | +// so completion honors config-selection flags. |
| 19 | +func globalInfoForCompletion(cmd *cobra.Command) schema.ConfigAndStacksInfo { |
| 20 | + globalFlags := flags.ParseGlobalFlags(cmd, viper.GetViper()) |
| 21 | + return schema.ConfigAndStacksInfo{ |
| 22 | + AtmosBasePath: globalFlags.BasePath, |
| 23 | + AtmosConfigFilesFromArg: globalFlags.Config, |
| 24 | + AtmosConfigDirsFromArg: globalFlags.ConfigPath, |
| 25 | + ProfilesFromArg: globalFlags.Profile, |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +// stackFlagCompletion provides completion values for the --stack flag. |
| 30 | +func stackFlagCompletion(cmd *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) { |
| 31 | + defer perf.Track(nil, "emulator.stackFlagCompletion")() |
| 32 | + |
| 33 | + atmosConfig, err := cfg.InitCliConfig(globalInfoForCompletion(cmd), true) |
| 34 | + if err != nil { |
| 35 | + return nil, cobra.ShellCompDirectiveNoFileComp |
| 36 | + } |
| 37 | + stacks, err := stackNamesForCompletion(&atmosConfig) |
| 38 | + if err != nil { |
| 39 | + return nil, cobra.ShellCompDirectiveNoFileComp |
| 40 | + } |
| 41 | + return stacks, cobra.ShellCompDirectiveNoFileComp |
| 42 | +} |
| 43 | + |
| 44 | +func stackNamesForCompletion(atmosConfig *schema.AtmosConfiguration) ([]string, error) { |
| 45 | + stacksMap, _, err := e.FindStacksMap(atmosConfig, false) |
| 46 | + if err != nil { |
| 47 | + return nil, err |
| 48 | + } |
| 49 | + |
| 50 | + names := make(map[string]struct{}, len(stacksMap)) |
| 51 | + for stackFileName, stackSection := range stacksMap { |
| 52 | + stackMap, ok := stackSection.(map[string]any) |
| 53 | + if !ok { |
| 54 | + continue |
| 55 | + } |
| 56 | + name, ok := stackNameForCompletion(atmosConfig, stackFileName, stackMap) |
| 57 | + if ok { |
| 58 | + names[name] = struct{}{} |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + stacks := make([]string, 0, len(names)) |
| 63 | + for name := range names { |
| 64 | + stacks = append(stacks, name) |
| 65 | + } |
| 66 | + sort.Strings(stacks) |
| 67 | + return stacks, nil |
| 68 | +} |
| 69 | + |
| 70 | +func stackNameForCompletion(atmosConfig *schema.AtmosConfiguration, stackFileName string, stackMap map[string]any) (string, bool) { |
| 71 | + if name, ok := stackMap["name"].(string); ok && name != "" { |
| 72 | + return name, true |
| 73 | + } |
| 74 | + |
| 75 | + if atmosConfig.Stacks.NameTemplate != "" { |
| 76 | + name, err := e.ProcessTmpl(atmosConfig, "emulator-stack-completion-name-template", atmosConfig.Stacks.NameTemplate, stackMap, atmosConfig.Templates.Settings.IgnoreMissingTemplateValues) |
| 77 | + if err == nil && name != "" { |
| 78 | + return name, true |
| 79 | + } |
| 80 | + return stackNameFromComponentTemplates(atmosConfig, stackMap) |
| 81 | + } |
| 82 | + |
| 83 | + if pattern := e.GetStackNamePattern(atmosConfig); pattern != "" { |
| 84 | + vars, _ := stackMap[cfg.VarsSectionName].(map[string]any) |
| 85 | + context := cfg.GetContextFromVars(vars) |
| 86 | + name, err := cfg.GetContextPrefix(stackFileName, context, pattern, stackFileName) |
| 87 | + if err == nil && name != "" { |
| 88 | + return name, true |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + return stackFileName, stackFileName != "" |
| 93 | +} |
| 94 | + |
| 95 | +func stackNameFromComponentTemplates(atmosConfig *schema.AtmosConfiguration, stackMap map[string]any) (string, bool) { |
| 96 | + componentsSection, ok := stackMap[cfg.ComponentsSectionName].(map[string]any) |
| 97 | + if !ok { |
| 98 | + return "", false |
| 99 | + } |
| 100 | + for _, componentsByType := range componentsSection { |
| 101 | + componentMap, ok := componentsByType.(map[string]any) |
| 102 | + if !ok { |
| 103 | + continue |
| 104 | + } |
| 105 | + for _, componentSection := range componentMap { |
| 106 | + componentData, ok := componentSection.(map[string]any) |
| 107 | + if !ok { |
| 108 | + continue |
| 109 | + } |
| 110 | + name, err := e.ProcessTmpl(atmosConfig, "emulator-stack-completion-component-name-template", atmosConfig.Stacks.NameTemplate, componentData, atmosConfig.Templates.Settings.IgnoreMissingTemplateValues) |
| 111 | + if err == nil && name != "" { |
| 112 | + return name, true |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + return "", false |
| 117 | +} |
| 118 | + |
| 119 | +// componentArgCompletion provides completion values for the component argument, |
| 120 | +// restricted to emulator components in the selected stack. |
| 121 | +func componentArgCompletion(cmd *cobra.Command, args []string, _ string) ([]string, cobra.ShellCompDirective) { |
| 122 | + defer perf.Track(nil, "emulator.componentArgCompletion")() |
| 123 | + |
| 124 | + if len(args) > 0 { |
| 125 | + return nil, cobra.ShellCompDirectiveNoFileComp |
| 126 | + } |
| 127 | + stack := "" |
| 128 | + if stackFlag := cmd.Flag("stack"); stackFlag != nil { |
| 129 | + stack = stackFlag.Value.String() |
| 130 | + } |
| 131 | + atmosConfig, err := cfg.InitCliConfig(globalInfoForCompletion(cmd), true) |
| 132 | + if err != nil { |
| 133 | + return nil, cobra.ShellCompDirectiveNoFileComp |
| 134 | + } |
| 135 | + stacksMap, err := e.ExecuteDescribeStacksWithAuthDisabled(&atmosConfig, stack, nil, []string{cfg.EmulatorComponentType}, nil, false, false, false, false, nil, nil, true) |
| 136 | + if err != nil { |
| 137 | + return nil, cobra.ShellCompDirectiveNoFileComp |
| 138 | + } |
| 139 | + components, err := l.FilterAndListComponents(stack, stacksMap) |
| 140 | + if err != nil { |
| 141 | + return nil, cobra.ShellCompDirectiveNoFileComp |
| 142 | + } |
| 143 | + return components, cobra.ShellCompDirectiveNoFileComp |
| 144 | +} |
| 145 | + |
| 146 | +// RegisterEmulatorCompletions registers completion functions for the emulator |
| 147 | +// command. Every subcommand takes a component argument. |
| 148 | +func RegisterEmulatorCompletions(cmd *cobra.Command) { |
| 149 | + defer perf.Track(nil, "emulator.RegisterEmulatorCompletions")() |
| 150 | + |
| 151 | + for _, subCmd := range cmd.Commands() { |
| 152 | + subCmd.ValidArgsFunction = componentArgCompletion |
| 153 | + } |
| 154 | +} |
0 commit comments