mirror of
https://github.com/containers/podman.git
synced 2025-10-10 07:45:08 +08:00
Merge pull request #27132 from NotSoFancyName/interface-completion
cmd: add autocomplete for network create --interface-name flag
This commit is contained in:
@ -4,6 +4,7 @@ import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"net"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
@ -1880,6 +1881,20 @@ func AutocompleteNetworkFilters(cmd *cobra.Command, args []string, toComplete st
|
||||
return completeKeyValues(toComplete, kv)
|
||||
}
|
||||
|
||||
// AutocompleteNetworkInterfaceNames - Autocomplete network create --interface-name options.
|
||||
func AutocompleteNetworkInterfaceNames(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
||||
interfaces, err := net.Interfaces()
|
||||
if err != nil {
|
||||
cobra.CompErrorln(err.Error())
|
||||
return nil, cobra.ShellCompDirectiveDefault
|
||||
}
|
||||
interfaceNames := make([]string, 0, len(interfaces))
|
||||
for _, iface := range interfaces {
|
||||
interfaceNames = append(interfaceNames, iface.Name)
|
||||
}
|
||||
return interfaceNames, cobra.ShellCompDirectiveNoFileComp
|
||||
}
|
||||
|
||||
// AutocompleteVolumeFilters - Autocomplete volume ls --filter options.
|
||||
func AutocompleteVolumeFilters(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
||||
local := func(_ string) ([]string, cobra.ShellCompDirective) {
|
||||
|
@ -85,7 +85,7 @@ func networkCreateFlags(cmd *cobra.Command) {
|
||||
|
||||
interfaceFlagName := "interface-name"
|
||||
flags.StringVar(&networkCreateOptions.InterfaceName, interfaceFlagName, "", "interface name which is used by the driver")
|
||||
_ = cmd.RegisterFlagCompletionFunc(interfaceFlagName, completion.AutocompleteNone)
|
||||
_ = cmd.RegisterFlagCompletionFunc(interfaceFlagName, common.AutocompleteNetworkInterfaceNames)
|
||||
|
||||
flags.BoolVar(&networkCreateOptions.DisableDNS, "disable-dns", false, "disable dns plugin")
|
||||
|
||||
|
@ -422,3 +422,11 @@ function _check_no_suggestions() {
|
||||
|
||||
_check_completion_end NoFileComp
|
||||
}
|
||||
|
||||
@test "podman network create --interface-name" {
|
||||
run_completion network create --interface-name l
|
||||
|
||||
assert "$output" =~ '.*lo.*' "Loopback interface should be present by default"
|
||||
|
||||
_check_completion_end NoFileComp
|
||||
}
|
||||
|
Reference in New Issue
Block a user