Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve bash completion #1437

Merged
merged 10 commits into from
Jun 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 54 additions & 5 deletions cmd/argo/commands/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,69 @@ import (
const (
bashCompletionFunc = `
__argo_get_workflow() {
local argo_out
if argo_out=$(argo list --output name 2>/dev/null); then
local status="$1"
local -a argo_out
if argo_out=($(argo list --status="$status" --output name 2>/dev/null)); then
COMPREPLY+=( $( compgen -W "${argo_out[*]}" -- "$cur" ) )
fi
}

__argo_get_logs() {
# Determine if were completing a workflow or not.
local workflow=0
for comp_word in "${COMP_WORDS[@]}"; do
if [[ $comp_word =~ ^(-w|--workflow)$ ]]; then
workflow=1
break
fi
done

# If completing a workflow, call normal function.
if [[ $workflow -eq 1 ]]; then
__argo_get_workflow && return $?
fi

# Otherwise, complete the list of pods
local -a kubectl_out
if kubectl_out=($(kubectl get pods --no-headers --label-columns=workflows.argoproj.io/workflow 2>/dev/null | awk '{if ($6!="") print $1}' 2>/dev/null)); then
COMPREPLY+=( $( compgen -W "${kubectl_out[*]}" -- "$cur" ) )
fi
}

__argo_list_files() {
COMPREPLY+=( $( compgen -f -o plusdirs -X '!*.@(yaml|yml|json)' -- "$cur" ) )
}

__argo_custom_func() {
case ${last_command} in
argo_delete | argo_get | argo_logs |\
argo_resubmit | argo_resume | argo_retry | argo_suspend |\
argo_terminate | argo_wait | argo_watch)
argo_delete | argo_get | argo_resubmit)
__argo_get_workflow
return
;;
argo_suspend | argo_terminate | argo_wait | argo_watch)
__argo_get_workflow "Running,Pending"
return
;;
argo_resume)
__argo_get_workflow "Running"
return
;;
argo_retry)
__argo_get_workflow "Failed"
return
;;
argo_logs)
__argo_get_logs
return
;;
argo_submit)
__argo_list_files
return
;;
argo_lint)
__argo_list_files
return
;;
*)
;;
esac
Expand Down
24 changes: 13 additions & 11 deletions cmd/argo/commands/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ import (
)

type listFlags struct {
allNamespaces bool // --all-namespaces
status string // --status
completed bool // --completed
running bool // --running
output string // --output
since string // --since
chunkSize int64 // --chunk-size
allNamespaces bool // --all-namespaces
status []string // --status
completed bool // --completed
running bool // --running
output string // --output
since string // --since
chunkSize int64 // --chunk-size
}

func NewListCommand() *cobra.Command {
Expand All @@ -49,9 +49,11 @@ func NewListCommand() *cobra.Command {
}
listOpts := metav1.ListOptions{}
labelSelector := labels.NewSelector()
if listArgs.status != "" {
req, _ := labels.NewRequirement(common.LabelKeyPhase, selection.In, strings.Split(listArgs.status, ","))
labelSelector = labelSelector.Add(*req)
if len(listArgs.status) != 0 {
req, _ := labels.NewRequirement(common.LabelKeyPhase, selection.In, listArgs.status)
if req != nil {
labelSelector = labelSelector.Add(*req)
}
}
if listArgs.completed {
req, _ := labels.NewRequirement(common.LabelKeyCompleted, selection.Equals, []string{"true"})
Expand Down Expand Up @@ -111,7 +113,7 @@ func NewListCommand() *cobra.Command {
},
}
command.Flags().BoolVar(&listArgs.allNamespaces, "all-namespaces", false, "Show workflows from all namespaces")
command.Flags().StringVar(&listArgs.status, "status", "", "Filter by status (comma separated)")
command.Flags().StringSliceVar(&listArgs.status, "status", []string{}, "Filter by status (comma separated)")
command.Flags().BoolVar(&listArgs.completed, "completed", false, "Show only completed workflows")
command.Flags().BoolVar(&listArgs.running, "running", false, "Show only running workflows")
command.Flags().StringVarP(&listArgs.output, "output", "o", "", "Output format. One of: wide|name")
Expand Down
8 changes: 7 additions & 1 deletion cmd/argo/commands/submit.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,20 @@ func NewSubmitCommand() *cobra.Command {
command.Flags().StringVar(&submitOpts.GenerateName, "generate-name", "", "override metadata.generateName")
command.Flags().StringVar(&submitOpts.Entrypoint, "entrypoint", "", "override entrypoint")
command.Flags().StringArrayVarP(&submitOpts.Parameters, "parameter", "p", []string{}, "pass an input parameter")
command.Flags().StringVarP(&submitOpts.ParameterFile, "parameter-file", "f", "", "pass a file containing all input parameters")
command.Flags().StringVar(&submitOpts.ServiceAccount, "serviceaccount", "", "run all pods in the workflow using specified serviceaccount")
command.Flags().StringVar(&submitOpts.InstanceID, "instanceid", "", "submit with a specific controller's instance id label")
command.Flags().StringVarP(&cliSubmitOpts.output, "output", "o", "", "Output format. One of: name|json|yaml|wide")
command.Flags().BoolVarP(&cliSubmitOpts.wait, "wait", "w", false, "wait for the workflow to complete")
command.Flags().BoolVar(&cliSubmitOpts.watch, "watch", false, "watch the workflow until it completes")
command.Flags().BoolVar(&cliSubmitOpts.strict, "strict", true, "perform strict workflow validation")
command.Flags().Int32Var(&priority, "priority", 0, "workflow priority")
command.Flags().StringVarP(&submitOpts.ParameterFile, "parameter-file", "f", "", "pass a file containing all input parameters")
// Only complete files with appropriate extension.
err := command.Flags().SetAnnotation("parameter-file", cobra.BashCompFilenameExt, []string{"json", "yaml", "yml"})
if err != nil {
log.Fatal(err)
}

return command
}

Expand Down