Skip to content

Commit

Permalink
Add Jobs autocompletion
Browse files Browse the repository at this point in the history
  • Loading branch information
Richard87 committed Sep 26, 2024
1 parent d79e04b commit 52bd7a1
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 6 deletions.
1 change: 1 addition & 0 deletions cmd/logsPipelineJob.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
1 change: 1 addition & 0 deletions cmd/restartPipelineJob.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
3 changes: 2 additions & 1 deletion pkg/utils/completion/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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))
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/utils/completion/deployments.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
})
}

Expand All @@ -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)
})
}
42 changes: 42 additions & 0 deletions pkg/utils/completion/job.go
Original file line number Diff line number Diff line change
@@ -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
}
3 changes: 2 additions & 1 deletion pkg/utils/completion/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions pkg/utils/completion/variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down

0 comments on commit 52bd7a1

Please sign in to comment.