|
| 1 | +package commands |
| 2 | + |
| 3 | +import ( |
| 4 | + "io" |
| 5 | + |
| 6 | + "github.com/ethpandaops/xcli/pkg/config" |
| 7 | + "github.com/ethpandaops/xcli/pkg/constants" |
| 8 | + "github.com/ethpandaops/xcli/pkg/orchestrator" |
| 9 | + "github.com/sirupsen/logrus" |
| 10 | + "github.com/spf13/cobra" |
| 11 | +) |
| 12 | + |
| 13 | +// completeServices returns a ValidArgsFunction that completes service names. |
| 14 | +// It loads the config and creates an orchestrator to get the dynamic service list. |
| 15 | +func completeServices(configPath string) func(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective) { |
| 16 | + return func(_ *cobra.Command, args []string, _ string) ([]string, cobra.ShellCompDirective) { |
| 17 | + // Don't complete if we already have an argument (for single-arg commands) |
| 18 | + if len(args) > 0 { |
| 19 | + return nil, cobra.ShellCompDirectiveNoFileComp |
| 20 | + } |
| 21 | + |
| 22 | + // Create a silent logger for completion (no output) |
| 23 | + log := logrus.New() |
| 24 | + log.SetOutput(io.Discard) |
| 25 | + |
| 26 | + // Try to load config - fail gracefully |
| 27 | + labCfg, cfgPath, err := config.LoadLabConfig(configPath) |
| 28 | + if err != nil { |
| 29 | + return nil, cobra.ShellCompDirectiveNoFileComp |
| 30 | + } |
| 31 | + |
| 32 | + // Try to create orchestrator - fail gracefully |
| 33 | + orch, err := orchestrator.NewOrchestrator(log, labCfg, cfgPath) |
| 34 | + if err != nil { |
| 35 | + return nil, cobra.ShellCompDirectiveNoFileComp |
| 36 | + } |
| 37 | + |
| 38 | + return orch.GetValidServices(), cobra.ShellCompDirectiveNoFileComp |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +// completeModes returns a ValidArgsFunction that completes mode values. |
| 43 | +func completeModes() func(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective) { |
| 44 | + return func(_ *cobra.Command, args []string, _ string) ([]string, cobra.ShellCompDirective) { |
| 45 | + if len(args) > 0 { |
| 46 | + return nil, cobra.ShellCompDirectiveNoFileComp |
| 47 | + } |
| 48 | + |
| 49 | + return []string{constants.ModeLocal, constants.ModeHybrid}, cobra.ShellCompDirectiveNoFileComp |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +// completeRebuildProjects returns a ValidArgsFunction that completes rebuild project names. |
| 54 | +func completeRebuildProjects() func(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective) { |
| 55 | + return func(_ *cobra.Command, args []string, _ string) ([]string, cobra.ShellCompDirective) { |
| 56 | + if len(args) > 0 { |
| 57 | + return nil, cobra.ShellCompDirectiveNoFileComp |
| 58 | + } |
| 59 | + |
| 60 | + return []string{ |
| 61 | + "xatu-cbt", |
| 62 | + "all", |
| 63 | + "cbt", |
| 64 | + "cbt-api", |
| 65 | + "lab-backend", |
| 66 | + "lab-frontend", |
| 67 | + "prometheus", |
| 68 | + "grafana", |
| 69 | + }, cobra.ShellCompDirectiveNoFileComp |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +// completeReleasableProjects returns a ValidArgsFunction that completes releasable project names. |
| 74 | +// Supports multiple arguments since release accepts multiple projects. |
| 75 | +func completeReleasableProjects() func(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective) { |
| 76 | + return func(_ *cobra.Command, args []string, _ string) ([]string, cobra.ShellCompDirective) { |
| 77 | + // Filter out already-provided arguments |
| 78 | + alreadyUsed := make(map[string]bool, len(args)) |
| 79 | + for _, arg := range args { |
| 80 | + alreadyUsed[arg] = true |
| 81 | + } |
| 82 | + |
| 83 | + completions := make([]string, 0, len(constants.ReleasableProjects)) |
| 84 | + for _, project := range constants.ReleasableProjects { |
| 85 | + if !alreadyUsed[project] { |
| 86 | + completions = append(completions, project) |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + return completions, cobra.ShellCompDirectiveNoFileComp |
| 91 | + } |
| 92 | +} |
0 commit comments