cmd/podman: completion for --sysctl in create/run

Signed-off-by: Celso Henrique Souza Silva <celsohenrique367@gmail.com>
This commit is contained in:
nothiaki
2025-09-19 16:05:23 -03:00
committed by Celso Henrique Souza Silva
parent 1671029517
commit f0f05e22c6
3 changed files with 46 additions and 2 deletions

View File

@ -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
}

View File

@ -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(

View File

@ -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
}