diff --git a/cmd/podman/common/completion.go b/cmd/podman/common/completion.go index e600b2ed60..ad2f6e5437 100644 --- a/cmd/podman/common/completion.go +++ b/cmd/podman/common/completion.go @@ -1954,3 +1954,35 @@ func AutocompleteSSH(cmd *cobra.Command, args []string, toComplete string) ([]st func AutocompleteHealthOnFailure(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { return define.SupportedHealthCheckOnFailureActions, cobra.ShellCompDirectiveNoFileComp } + +// AutocompleteSysctl - autocomplete list all sysctl names +func AutocompleteSysctl(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + var completions []string + sysPath := "/proc/sys" + + err := filepath.Walk(sysPath, func(path string, info os.FileInfo, err error) error { + if err != nil { + return err + } + + if !info.IsDir() { + rel, err := filepath.Rel(sysPath, path) + if err != nil { + return err + } + sysctlName := strings.ReplaceAll(rel, string(os.PathSeparator), ".") + + if strings.HasPrefix(sysctlName, toComplete) { + completions = append(completions, sysctlName) + } + } + + return nil + }) + + if err != nil { + return nil, cobra.ShellCompDirectiveError + } + + return completions, cobra.ShellCompDirectiveNoFileComp +} diff --git a/cmd/podman/common/create.go b/cmd/podman/common/create.go index ef8939a55e..cb55f74d4a 100644 --- a/cmd/podman/common/create.go +++ b/cmd/podman/common/create.go @@ -733,8 +733,8 @@ func DefineCreateFlags(cmd *cobra.Command, cf *entities.ContainerCreateOptions, sysctlFlagName, []string{}, "Sysctl options", ) - //TODO: Add function for sysctl completion. - _ = cmd.RegisterFlagCompletionFunc(sysctlFlagName, completion.AutocompleteNone) + + _ = cmd.RegisterFlagCompletionFunc(sysctlFlagName, AutocompleteSysctl) securityOptFlagName := "security-opt" createFlags.StringArrayVar( diff --git a/test/system/600-completion.bats b/test/system/600-completion.bats index 3773cf4056..53a46fbe61 100644 --- a/test/system/600-completion.bats +++ b/test/system/600-completion.bats @@ -410,3 +410,15 @@ function _check_no_suggestions() { # cleanup container run_podman rm $ctrname } + +# bats test_tags=ci:parallel +@test "podman run --sysctl completion for sysctl" { + skip_if_remote "sysctl option not working via remote" + + run_completion run --sysctl net. + + assert "$output" =~ "^net\." \ + "Only suggestions with 'net.' should be present for podman run --sysctl net." + + _check_completion_end NoFileComp +}