Skip to content

Commit 0742349

Browse files
Merge pull request #27132 from NotSoFancyName/interface-completion
cmd: add autocomplete for network create --interface-name flag
2 parents 277530e + ebde5d1 commit 0742349

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

cmd/podman/common/completion.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bufio"
55
"fmt"
66
"io/fs"
7+
"net"
78
"os"
89
"path"
910
"path/filepath"
@@ -1880,6 +1881,20 @@ func AutocompleteNetworkFilters(cmd *cobra.Command, args []string, toComplete st
18801881
return completeKeyValues(toComplete, kv)
18811882
}
18821883

1884+
// AutocompleteNetworkInterfaceNames - Autocomplete network create --interface-name options.
1885+
func AutocompleteNetworkInterfaceNames(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
1886+
interfaces, err := net.Interfaces()
1887+
if err != nil {
1888+
cobra.CompErrorln(err.Error())
1889+
return nil, cobra.ShellCompDirectiveDefault
1890+
}
1891+
interfaceNames := make([]string, 0, len(interfaces))
1892+
for _, iface := range interfaces {
1893+
interfaceNames = append(interfaceNames, iface.Name)
1894+
}
1895+
return interfaceNames, cobra.ShellCompDirectiveNoFileComp
1896+
}
1897+
18831898
// AutocompleteVolumeFilters - Autocomplete volume ls --filter options.
18841899
func AutocompleteVolumeFilters(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
18851900
local := func(_ string) ([]string, cobra.ShellCompDirective) {

cmd/podman/networks/create.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func networkCreateFlags(cmd *cobra.Command) {
8585

8686
interfaceFlagName := "interface-name"
8787
flags.StringVar(&networkCreateOptions.InterfaceName, interfaceFlagName, "", "interface name which is used by the driver")
88-
_ = cmd.RegisterFlagCompletionFunc(interfaceFlagName, completion.AutocompleteNone)
88+
_ = cmd.RegisterFlagCompletionFunc(interfaceFlagName, common.AutocompleteNetworkInterfaceNames)
8989

9090
flags.BoolVar(&networkCreateOptions.DisableDNS, "disable-dns", false, "disable dns plugin")
9191

test/system/600-completion.bats

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,3 +422,11 @@ function _check_no_suggestions() {
422422

423423
_check_completion_end NoFileComp
424424
}
425+
426+
@test "podman network create --interface-name" {
427+
run_completion network create --interface-name l
428+
429+
assert "$output" =~ '.*lo.*' "Loopback interface should be present by default"
430+
431+
_check_completion_end NoFileComp
432+
}

0 commit comments

Comments
 (0)