From 52bd7a111bfe3c6761bd25c90574726b5b7a8372 Mon Sep 17 00:00:00 2001 From: Richard87 Date: Thu, 26 Sep 2024 09:57:13 +0200 Subject: [PATCH] Add Jobs autocompletion --- cmd/logsPipelineJob.go | 1 + cmd/restartPipelineJob.go | 1 + pkg/utils/completion/alias.go | 3 ++- pkg/utils/completion/deployments.go | 5 ++-- pkg/utils/completion/job.go | 42 +++++++++++++++++++++++++++++ pkg/utils/completion/secrets.go | 3 ++- pkg/utils/completion/variable.go | 4 +-- 7 files changed, 53 insertions(+), 6 deletions(-) create mode 100644 pkg/utils/completion/job.go diff --git a/cmd/logsPipelineJob.go b/cmd/logsPipelineJob.go index 78302a8..fbdfba8 100644 --- a/cmd/logsPipelineJob.go +++ b/cmd/logsPipelineJob.go @@ -185,5 +185,6 @@ func init() { logsJobCmd.Flags().StringP(flagnames.Job, "j", "", "The job to get logs for") _ = logsJobCmd.RegisterFlagCompletionFunc(flagnames.Application, completion.ApplicationCompletion) + _ = logsJobCmd.RegisterFlagCompletionFunc(flagnames.Job, completion.JobCompletion) setContextSpecificPersistentFlags(logsJobCmd) } diff --git a/cmd/restartPipelineJob.go b/cmd/restartPipelineJob.go index 468f7f9..5b6eb11 100644 --- a/cmd/restartPipelineJob.go +++ b/cmd/restartPipelineJob.go @@ -71,5 +71,6 @@ func init() { restartPipelineJobCmd.Flags().StringP(flagnames.Job, "j", "", "The job to restart") _ = restartPipelineJobCmd.RegisterFlagCompletionFunc(flagnames.Application, completion.ApplicationCompletion) + _ = restartPipelineJobCmd.RegisterFlagCompletionFunc(flagnames.Job, completion.JobCompletion) setContextSpecificPersistentFlags(restartPipelineJobCmd) } diff --git a/pkg/utils/completion/alias.go b/pkg/utils/completion/alias.go index f4c22c8..8fe268d 100644 --- a/pkg/utils/completion/alias.go +++ b/pkg/utils/completion/alias.go @@ -7,6 +7,7 @@ import ( "github.com/equinor/radix-cli/pkg/client" "github.com/equinor/radix-cli/pkg/config" "github.com/equinor/radix-cli/pkg/flagnames" + "github.com/equinor/radix-common/utils/pointers" "github.com/spf13/cobra" "k8s.io/utils/strings/slices" ) @@ -40,7 +41,7 @@ func AliasCompletion(cmd *cobra.Command, _ []string, toComplete string) ([]strin if component.Name != nil && *component.Name == componentName { for _, externalDns := range component.ExternalDNS { if externalDns.FQDN != nil { - names = append(names, *externalDns.FQDN) + names = append(names, pointers.Val(externalDns.FQDN)) } } } diff --git a/pkg/utils/completion/deployments.go b/pkg/utils/completion/deployments.go index 9c5c425..5343266 100644 --- a/pkg/utils/completion/deployments.go +++ b/pkg/utils/completion/deployments.go @@ -10,6 +10,7 @@ import ( "github.com/equinor/radix-cli/pkg/client" "github.com/equinor/radix-cli/pkg/config" "github.com/equinor/radix-cli/pkg/flagnames" + "github.com/equinor/radix-common/utils/pointers" "github.com/equinor/radix-common/utils/slice" "github.com/spf13/cobra" "k8s.io/utils/strings/slices" @@ -54,7 +55,7 @@ func getEnvironmentDeployments(appName, envName string, apiClient *apiclient.Rad } return slice.Map(resp.Payload.Deployments, func(item *models.DeploymentSummary) string { - return *item.Name + return pointers.Val(item.Name) }) } @@ -66,6 +67,6 @@ func getAllDeployments(appName string, apiClient *apiclient.Radixapi) []string { } return slice.Map(resp.Payload, func(item *models.DeploymentSummary) string { - return *item.Name + return pointers.Val(item.Name) }) } diff --git a/pkg/utils/completion/job.go b/pkg/utils/completion/job.go new file mode 100644 index 0000000..cbd6f73 --- /dev/null +++ b/pkg/utils/completion/job.go @@ -0,0 +1,42 @@ +package completion + +import ( + "strings" + + "github.com/equinor/radix-cli/generated-client/client/application" + "github.com/equinor/radix-cli/generated-client/models" + "github.com/equinor/radix-cli/pkg/client" + "github.com/equinor/radix-cli/pkg/config" + "github.com/equinor/radix-cli/pkg/flagnames" + "github.com/equinor/radix-common/utils/slice" + "github.com/spf13/cobra" + "k8s.io/utils/strings/slices" +) + +func JobCompletion(cmd *cobra.Command, _ []string, toComplete string) ([]string, cobra.ShellCompDirective) { + + appName, err := config.GetAppNameFromConfigOrFromParameter(cmd, flagnames.Application) + if err != nil || appName == "" { + return nil, cobra.ShellCompDirectiveNoFileComp + } + apiClient, err := client.GetForCommand(cmd) + if err != nil { + return nil, cobra.ShellCompDirectiveNoFileComp + } + + params := application.NewGetApplicationParams().WithAppName(appName) + resp, err := apiClient.Application.GetApplication(params, nil) + if err != nil || resp.Payload == nil { + return nil, cobra.ShellCompDirectiveNoFileComp + } + + names := slice.Map(resp.Payload.Jobs, func(component *models.JobSummary) string { + return component.Name + }) + + filteredNames := slices.Filter(nil, names, func(appName string) bool { + return strings.HasPrefix(appName, toComplete) + }) + + return filteredNames, cobra.ShellCompDirectiveNoFileComp +} diff --git a/pkg/utils/completion/secrets.go b/pkg/utils/completion/secrets.go index 0a6dbf4..ec6b3ae 100644 --- a/pkg/utils/completion/secrets.go +++ b/pkg/utils/completion/secrets.go @@ -8,6 +8,7 @@ import ( "github.com/equinor/radix-cli/pkg/client" "github.com/equinor/radix-cli/pkg/config" "github.com/equinor/radix-cli/pkg/flagnames" + "github.com/equinor/radix-common/utils/pointers" "github.com/equinor/radix-common/utils/slice" "github.com/spf13/cobra" "k8s.io/utils/strings/slices" @@ -38,7 +39,7 @@ func SecretCompletion(cmd *cobra.Command, _ []string, toComplete string) ([]stri } names := slice.Map(resp.Payload.Secrets, func(item *models.Secret) string { - return *item.Name + return pointers.Val(item.Name) }) filteredNames := slices.Filter(nil, names, func(name string) bool { diff --git a/pkg/utils/completion/variable.go b/pkg/utils/completion/variable.go index 77441ee..c3faf9e 100644 --- a/pkg/utils/completion/variable.go +++ b/pkg/utils/completion/variable.go @@ -37,8 +37,8 @@ func VariableCompletion(cmd *cobra.Command, _ []string, toComplete string) ([]st var names []string for _, comp := range resp.Payload.ActiveDeployment.Components { - if *comp.Name == componentName { - for variable, _ := range comp.Variables { + if comp.Name != nil && *comp.Name == componentName { + for variable := range comp.Variables { names = append(names, variable) } }