Skip to content

Commit

Permalink
shell completion for the network flag
Browse files Browse the repository at this point in the history
Complete all the options e.g. `container:`,`ns:`,`host`, etc...

Signed-off-by: Paul Holzinger <[email protected]>
  • Loading branch information
Paul Holzinger committed Dec 12, 2020
1 parent dd95478 commit f7bd0a4
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 15 deletions.
58 changes: 45 additions & 13 deletions cmd/podman/common/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,10 @@ func completeKeyValues(toComplete string, k keyValueCompletion) ([]string, cobra
return suggestions, directive
}

func getBoolCompletion(_ string) ([]string, cobra.ShellCompDirective) {
return []string{"true", "false"}, cobra.ShellCompDirectiveNoFileComp
}

/* Autocomplete Functions for cobra ValidArgsFunction */

// AutocompleteContainers - Autocomplete all container names.
Expand Down Expand Up @@ -797,6 +801,39 @@ func AutocompleteVolumeFlag(cmd *cobra.Command, args []string, toComplete string
return volumes, directive
}

// AutocompleteNetworkFlag - Autocomplete network flag options.
func AutocompleteNetworkFlag(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
kv := keyValueCompletion{
"container:": func(s string) ([]string, cobra.ShellCompDirective) { return getContainers(cmd, s, completeDefault) },
"ns:": func(_ string) ([]string, cobra.ShellCompDirective) {
return nil, cobra.ShellCompDirectiveDefault
},
"bridge": nil,
"none": nil,
"host": nil,
"private": nil,
"slirp4netns:": func(s string) ([]string, cobra.ShellCompDirective) {
skv := keyValueCompletion{
"allow_host_loopback=": getBoolCompletion,
"cidr=": nil,
"enable_ipv6=": getBoolCompletion,
"outbound_addr=": nil,
"outbound_addr6=": nil,
"port_handler=": func(_ string) ([]string, cobra.ShellCompDirective) {
return []string{"rootlesskit", "slirp4netns"}, cobra.ShellCompDirectiveNoFileComp
},
}
return completeKeyValues(s, skv)
},
}

networks, _ := getNetworks(cmd, toComplete)
suggestions, dir := completeKeyValues(toComplete, kv)
// add slirp4netns here it does not work correct if we add it to the kv map
suggestions = append(suggestions, "slirp4netns")
return append(networks, suggestions...), dir
}

// AutocompleteJSONFormat - Autocomplete format flag option.
// -> "json"
func AutocompleteJSONFormat(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
Expand Down Expand Up @@ -974,17 +1011,14 @@ func AutocompletePodPsFilters(cmd *cobra.Command, args []string, toComplete stri

// AutocompleteImageFilters - Autocomplete image ls --filter options.
func AutocompleteImageFilters(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
getBool := func(_ string) ([]string, cobra.ShellCompDirective) {
return []string{"true", "false"}, cobra.ShellCompDirectiveNoFileComp
}
getImg := func(s string) ([]string, cobra.ShellCompDirective) { return getImages(cmd, s) }
kv := keyValueCompletion{
"before=": getImg,
"since=": getImg,
"label=": nil,
"reference=": nil,
"dangling=": getBool,
"readonly=": getBool,
"dangling=": getBoolCompletion,
"readonly=": getBoolCompletion,
}
return completeKeyValues(toComplete, kv)
}
Expand All @@ -1004,14 +1038,12 @@ func AutocompleteVolumeFilters(cmd *cobra.Command, args []string, toComplete str
return []string{"local"}, cobra.ShellCompDirectiveNoFileComp
}
kv := keyValueCompletion{
"name=": func(s string) ([]string, cobra.ShellCompDirective) { return getVolumes(cmd, s) },
"driver=": local,
"scope=": local,
"label=": nil,
"opt=": nil,
"dangling=": func(_ string) ([]string, cobra.ShellCompDirective) {
return []string{"true", "false"}, cobra.ShellCompDirectiveNoFileComp
},
"name=": func(s string) ([]string, cobra.ShellCompDirective) { return getVolumes(cmd, s) },
"driver=": local,
"scope=": local,
"label=": nil,
"opt=": nil,
"dangling=": getBoolCompletion,
}
return completeKeyValues(toComplete, kv)
}
2 changes: 1 addition & 1 deletion cmd/podman/common/netflags.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func DefineNetFlags(cmd *cobra.Command) {
networkFlagName, containerConfig.NetNS(),
"Connect a container to a network",
)
_ = cmd.RegisterFlagCompletionFunc(networkFlagName, AutocompleteNetworks)
_ = cmd.RegisterFlagCompletionFunc(networkFlagName, AutocompleteNetworkFlag)

networkAliasFlagName := "network-alias"
netFlags.StringSlice(
Expand Down
2 changes: 1 addition & 1 deletion cmd/podman/play/kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func init() {

networkFlagName := "network"
flags.StringVar(&kubeOptions.Network, networkFlagName, "", "Connect pod to CNI network(s)")
_ = kubeCmd.RegisterFlagCompletionFunc(networkFlagName, common.AutocompleteNetworks)
_ = kubeCmd.RegisterFlagCompletionFunc(networkFlagName, common.AutocompleteNetworkFlag)

logDriverFlagName := "log-driver"
flags.StringVar(&kubeOptions.LogDriver, logDriverFlagName, "", "Logging driver for the container")
Expand Down

0 comments on commit f7bd0a4

Please sign in to comment.