Skip to content

Commit 2edf4ae

Browse files
Merge pull request #27122 from nothiaki/feat-sysctl-completion
feat(completions): sysctl completion
2 parents d58e496 + f0f05e2 commit 2edf4ae

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

cmd/podman/common/completion.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1954,3 +1954,35 @@ func AutocompleteSSH(cmd *cobra.Command, args []string, toComplete string) ([]st
19541954
func AutocompleteHealthOnFailure(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
19551955
return define.SupportedHealthCheckOnFailureActions, cobra.ShellCompDirectiveNoFileComp
19561956
}
1957+
1958+
// AutocompleteSysctl - autocomplete list all sysctl names
1959+
func AutocompleteSysctl(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
1960+
var completions []string
1961+
sysPath := "/proc/sys"
1962+
1963+
err := filepath.Walk(sysPath, func(path string, info os.FileInfo, err error) error {
1964+
if err != nil {
1965+
return err
1966+
}
1967+
1968+
if !info.IsDir() {
1969+
rel, err := filepath.Rel(sysPath, path)
1970+
if err != nil {
1971+
return err
1972+
}
1973+
sysctlName := strings.ReplaceAll(rel, string(os.PathSeparator), ".")
1974+
1975+
if strings.HasPrefix(sysctlName, toComplete) {
1976+
completions = append(completions, sysctlName)
1977+
}
1978+
}
1979+
1980+
return nil
1981+
})
1982+
1983+
if err != nil {
1984+
return nil, cobra.ShellCompDirectiveError
1985+
}
1986+
1987+
return completions, cobra.ShellCompDirectiveNoFileComp
1988+
}

cmd/podman/common/create.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -733,8 +733,8 @@ func DefineCreateFlags(cmd *cobra.Command, cf *entities.ContainerCreateOptions,
733733
sysctlFlagName, []string{},
734734
"Sysctl options",
735735
)
736-
//TODO: Add function for sysctl completion.
737-
_ = cmd.RegisterFlagCompletionFunc(sysctlFlagName, completion.AutocompleteNone)
736+
737+
_ = cmd.RegisterFlagCompletionFunc(sysctlFlagName, AutocompleteSysctl)
738738

739739
securityOptFlagName := "security-opt"
740740
createFlags.StringArrayVar(

test/system/600-completion.bats

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,3 +410,15 @@ function _check_no_suggestions() {
410410
# cleanup container
411411
run_podman rm $ctrname
412412
}
413+
414+
# bats test_tags=ci:parallel
415+
@test "podman run --sysctl completion for sysctl" {
416+
skip_if_remote "sysctl option not working via remote"
417+
418+
run_completion run --sysctl net.
419+
420+
assert "$output" =~ "^net\." \
421+
"Only suggestions with 'net.' should be present for podman run --sysctl net."
422+
423+
_check_completion_end NoFileComp
424+
}

0 commit comments

Comments
 (0)