Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 115 additions & 3 deletions completions/zsh/_crio
Original file line number Diff line number Diff line change
@@ -1,13 +1,125 @@
_cli_zsh_autocomplete() {

local -a cmds
cmds=('complete:Generate bash, fish or zsh completions.' 'completion:Generate bash, fish or zsh completions.' 'man:Generate the man page documentation.' 'markdown:Generate the markdown documentation.' 'md:Generate the markdown documentation.' 'config:Outputs a commented version of the configuration file that could be used
cmds=(
'complete:Generate bash, fish or zsh completions.'
'completion:Generate bash, fish or zsh completions.'
'man:Generate the man page documentation.'
'markdown:Generate the markdown documentation.'
'md:Generate the markdown documentation.'
'config:Outputs a commented version of the configuration file that could be used
by CRI-O. This allows you to save you current configuration setup and then load
it later with **--config**. Global options will modify the output.' 'version:display detailed version information' 'wipe:wipe CRI-O's container and image storage' 'help:Shows a list of commands or help for one command' 'h:Shows a list of commands or help for one command')
it later with **--config**. Global options will modify the output.'
'version:display detailed version information'
"wipe:wipe CRI-O's container and image storage"
'help:Shows a list of commands or help for one command'
'h:Shows a list of commands or help for one command'
)
_describe 'commands' cmds

local -a opts
opts=('--absent-mount-sources-to-reject' '--additional-devices' '--address' '--allowed-devices' '--apparmor-profile' '--big-files-temporary-dir' '--bind-mount-prefix' '--blockio-config-file' '--cgroup-manager' '--clean-shutdown-file' '--cni-config-dir' '--cni-default-network' '--cni-plugin-dir' '--config' '--config-dir' '--conmon' '--conmon-cgroup' '--conmon-env' '--container-attach-socket-dir' '--container-exits-dir' '--ctr-stop-timeout' '--decryption-keys-path' '--default-capabilities' '--default-env' '--default-mounts-file' '--default-runtime' '--default-sysctls' '--default-transport' '--default-ulimits' '--device-ownership-from-security-context' '--drop-infra-ctr' '--enable-metrics' '--enable-profile-unix-socket' '--enable-tracing' '--gid-mappings' '--global-auth-file' '--grpc-max-recv-msg-size' '--grpc-max-send-msg-size' '--hooks-dir' '--image-volumes' '--infra-ctr-cpuset' '--insecure-registry' '--internal-wipe' '--irqbalance-config-file' '--listen' '--log' '--log-dir' '--log-filter' '--log-format' '--log-journald' '--log-level' '--log-size-max' '--metrics-cert' '--metrics-collectors' '--metrics-key' '--metrics-port' '--metrics-socket' '--minimum-mappable-gid' '--minimum-mappable-uid' '--namespaces-dir' '--no-pivot' '--pause-command' '--pause-image' '--pause-image-auth-file' '--pids-limit' '--pinns-path' '--profile' '--profile-cpu' '--profile-mem' '--profile-port' '--rdt-config-file' '--read-only' '--registries-conf' '--registries-conf-dir' '--registry' '--root' '--runroot' '--runtimes' '--seccomp-profile' '--seccomp-use-default-when-empty' '--selinux' '--separate-pull-cgroup' '--signature-policy' '--stats-collection-period' '--storage-driver' '--storage-opt' '--stream-address' '--stream-enable-tls' '--stream-idle-timeout' '--stream-port' '--stream-tls-ca' '--stream-tls-cert' '--stream-tls-key' '--tracing-endpoint' '--tracing-sampling-rate-per-million' '--uid-mappings' '--version-file' '--version-file-persist' '--help' '--version')
opts=(
'--absent-mount-sources-to-reject'
'--additional-devices'
'--address'
'--allowed-devices'
'--apparmor-profile'
'--big-files-temporary-dir'
'--bind-mount-prefix'
'--blockio-config-file'
'--cgroup-manager'
'--clean-shutdown-file'
'--cni-config-dir'
'--cni-default-network'
'--cni-plugin-dir'
'--config'
'--config-dir'
'--conmon'
'--conmon-cgroup'
'--conmon-env'
'--container-attach-socket-dir'
'--container-exits-dir'
'--ctr-stop-timeout'
'--decryption-keys-path'
'--default-capabilities'
'--default-env'
'--default-mounts-file'
'--default-runtime'
'--default-sysctls'
'--default-transport'
'--default-ulimits'
'--device-ownership-from-security-context'
'--drop-infra-ctr'
'--enable-metrics'
'--enable-profile-unix-socket'
'--enable-tracing'
'--gid-mappings'
'--global-auth-file'
'--grpc-max-recv-msg-size'
'--grpc-max-send-msg-size'
'--hooks-dir'
'--image-volumes'
'--infra-ctr-cpuset'
'--insecure-registry'
'--internal-wipe'
'--irqbalance-config-file'
'--listen'
'--log'
'--log-dir'
'--log-filter'
'--log-format'
'--log-journald'
'--log-level'
'--log-size-max'
'--metrics-cert'
'--metrics-collectors'
'--metrics-key'
'--metrics-port'
'--metrics-socket'
'--minimum-mappable-gid'
'--minimum-mappable-uid'
'--namespaces-dir'
'--no-pivot'
'--pause-command'
'--pause-image'
'--pause-image-auth-file'
'--pids-limit'
'--pinns-path'
'--profile'
'--profile-cpu'
'--profile-mem'
'--profile-port'
'--rdt-config-file'
'--read-only'
'--registries-conf'
'--registries-conf-dir'
'--registry'
'--root'
'--runroot'
'--runtimes'
'--seccomp-profile'
'--seccomp-use-default-when-empty'
'--selinux'
'--separate-pull-cgroup'
'--signature-policy'
'--stats-collection-period'
'--storage-driver'
'--storage-opt'
'--stream-address'
'--stream-enable-tls'
'--stream-idle-timeout'
'--stream-port'
'--stream-tls-ca'
'--stream-tls-cert'
'--stream-tls-key'
'--tracing-endpoint'
'--tracing-sampling-rate-per-million'
'--uid-mappings'
'--version-file'
'--version-file-persist'
'--help'
'--version'
)
_describe 'global options' opts

return
Expand Down
24 changes: 22 additions & 2 deletions completions/zsh/_crio-status
Original file line number Diff line number Diff line change
@@ -1,11 +1,31 @@
_cli_zsh_autocomplete() {

local -a cmds
cmds=('complete:Generate bash, fish or zsh completions.' 'completion:Generate bash, fish or zsh completions.' 'man:Generate the man page documentation.' 'markdown:Generate the markdown documentation.' 'md:Generate the markdown documentation.' 'config:Show the configuration of CRI-O as TOML string.' 'c:Show the configuration of CRI-O as TOML string.' 'containers:Display detailed information about the provided container ID.' 'container:Display detailed information about the provided container ID.' 'cs:Display detailed information about the provided container ID.' 's:Display detailed information about the provided container ID.' 'info:Retrieve generic information about CRI-O, like the cgroup and storage driver.' 'i:Retrieve generic information about CRI-O, like the cgroup and storage driver.' 'help:Shows a list of commands or help for one command' 'h:Shows a list of commands or help for one command')
cmds=(
'complete:Generate bash, fish or zsh completions.'
'completion:Generate bash, fish or zsh completions.'
'man:Generate the man page documentation.'
'markdown:Generate the markdown documentation.'
'md:Generate the markdown documentation.'
'config:Show the configuration of CRI-O as TOML string.'
'c:Show the configuration of CRI-O as TOML string.'
'containers:Display detailed information about the provided container ID.'
'container:Display detailed information about the provided container ID.'
'cs:Display detailed information about the provided container ID.'
's:Display detailed information about the provided container ID.'
'info:Retrieve generic information about CRI-O, like the cgroup and storage driver.'
'i:Retrieve generic information about CRI-O, like the cgroup and storage driver.'
'help:Shows a list of commands or help for one command'
'h:Shows a list of commands or help for one command'
)
_describe 'commands' cmds

local -a opts
opts=('--socket' '--help' '--version')
opts=(
'--socket'
'--help'
'--version'
)
_describe 'global options' opts

return
Expand Down
23 changes: 17 additions & 6 deletions internal/criocli/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,15 @@ func bashCompletion(c *cli.Context) error {
const zshCompletionTemplate = `_cli_zsh_autocomplete() {

local -a cmds
cmds=('%s')
cmds=(
%s
)
_describe 'commands' cmds

local -a opts
opts=('%s')
opts=(
%s
)
_describe 'global options' opts

return
Expand All @@ -94,24 +98,31 @@ func zshCompletion(c *cli.Context) error {
continue
}
for _, name := range command.Names() {
subcommands = append(subcommands, name+":"+command.Usage)
subcommands = append(subcommands, zshQuoteCmd(name, command.Usage))
}
}

opts := []string{}
for _, flag := range c.App.Flags {
// only includes full flag name.
opts = append(opts, "--"+flag.Names()[0])
opts = append(opts, "'--"+flag.Names()[0]+"'")
}

fmt.Fprintln(c.App.Writer,
fmt.Sprintf(zshCompletionTemplate,
strings.Join(subcommands, "' '"),
strings.Join(opts, "' '"),
strings.Join(subcommands, "\n "),
strings.Join(opts, "\n "),
c.App.Name))
return nil
}

func zshQuoteCmd(name, usage string) string {
if !strings.ContainsRune(usage, '\'') {
return "'" + name + ":" + usage + "'"
}
return "\"" + name + ":" + strings.ReplaceAll(usage, "$", "\\$") + "\""
}

func fishCompletion(c *cli.Context) error {
completion, err := c.App.ToFishCompletion()
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions internal/criocli/completion_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package criocli

// ZshQuoteCmd export zshQuoteCmd for testing.
func ZshQuoteCmd(name, usage string) string {
return zshQuoteCmd(name, usage)
}
32 changes: 32 additions & 0 deletions internal/criocli/criocli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,35 @@ var _ = t.Describe("CLI", func() {
Expect(slice.Value()[0]).To(Equal("value1"))
})
})

// The actual test suite for zsh completion quoting.
var _ = t.Describe("completion generation", func() {
DescribeTable("should quote and escape strings correctly", func(name, usage, expected string) {
// When
result := criocli.ZshQuoteCmd(name, usage)

// Then
Expect(result).To(Equal(expected))
},
Entry(
"should use single quotes by default",
"foo", "description of foo",
"'foo:description of foo'",
),
Entry(
"should use double quotes for strings containing single quotes",
"bar", "bar's description",
"\"bar:bar's description\"",
),
Entry(
"should not escape $'s within single quotes",
"foobar", "foobar handles $FOOBAR",
"'foobar:foobar handles $FOOBAR'",
),
Entry(
"should escape $'s within doube quotes",
"barfoo", "barfoo's usage $needs $escaped $dollars",
"\"barfoo:barfoo's usage \\$needs \\$escaped \\$dollars\"",
),
)
})