Skip to content

Commit 38bca1f

Browse files
committed
Move completion to sub package
Signed-off-by: apostasie <[email protected]>
1 parent b8b5d70 commit 38bca1f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+308
-188
lines changed

cmd/nerdctl/apparmor_unload_linux.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121

2222
"github.com/spf13/cobra"
2323

24+
"github.com/containerd/nerdctl/v2/cmd/nerdctl/completion"
2425
"github.com/containerd/nerdctl/v2/pkg/cmd/apparmor"
2526
"github.com/containerd/nerdctl/v2/pkg/defaults"
2627
)
@@ -47,5 +48,5 @@ func apparmorUnloadAction(cmd *cobra.Command, args []string) error {
4748
}
4849

4950
func apparmorUnloadShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
50-
return shellCompleteApparmorProfiles(cmd)
51+
return completion.ShellCompleteApparmorProfiles(cmd)
5152
}

cmd/nerdctl/builder_build.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525

2626
"github.com/spf13/cobra"
2727

28+
"github.com/containerd/nerdctl/v2/cmd/nerdctl/completion"
2829
"github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers"
2930
"github.com/containerd/nerdctl/v2/pkg/api/types"
3031
"github.com/containerd/nerdctl/v2/pkg/buildkitutil"
@@ -72,7 +73,7 @@ If Dockerfile is not present and -f is not specified, it will look for Container
7273
// #region platform flags
7374
// platform is defined as StringSlice, not StringArray, to allow specifying "--platform=amd64,arm64"
7475
buildCommand.Flags().StringSlice("platform", []string{}, "Set target platform for build (e.g., \"amd64\", \"arm64\")")
75-
buildCommand.RegisterFlagCompletionFunc("platform", shellCompletePlatforms)
76+
buildCommand.RegisterFlagCompletionFunc("platform", completion.ShellCompletePlatforms)
7677
buildCommand.Flags().StringArray("build-context", []string{}, "Additional build contexts (e.g., name=path)")
7778
// #endregion
7879

cmd/nerdctl/completion.go renamed to cmd/nerdctl/completion/completion.go

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
limitations under the License.
1515
*/
1616

17-
package main
17+
package completion
1818

1919
import (
2020
"context"
@@ -25,12 +25,15 @@ import (
2525
containerd "github.com/containerd/containerd/v2/client"
2626

2727
"github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers"
28+
"github.com/containerd/nerdctl/v2/pkg/api/types"
2829
"github.com/containerd/nerdctl/v2/pkg/clientutil"
30+
"github.com/containerd/nerdctl/v2/pkg/cmd/volume"
31+
"github.com/containerd/nerdctl/v2/pkg/inspecttypes/native"
2932
"github.com/containerd/nerdctl/v2/pkg/labels"
3033
"github.com/containerd/nerdctl/v2/pkg/netutil"
3134
)
3235

33-
func shellCompleteImageNames(cmd *cobra.Command) ([]string, cobra.ShellCompDirective) {
36+
func ShellCompleteImageNames(cmd *cobra.Command) ([]string, cobra.ShellCompDirective) {
3437
globalOptions, err := helpers.ProcessRootCmdFlags(cmd)
3538
if err != nil {
3639
return nil, cobra.ShellCompDirectiveError
@@ -53,7 +56,7 @@ func shellCompleteImageNames(cmd *cobra.Command) ([]string, cobra.ShellCompDirec
5356
return candidates, cobra.ShellCompDirectiveNoFileComp
5457
}
5558

56-
func shellCompleteContainerNames(cmd *cobra.Command, filterFunc func(containerd.ProcessStatus) bool) ([]string, cobra.ShellCompDirective) {
59+
func ShellCompleteContainerNames(cmd *cobra.Command, filterFunc func(containerd.ProcessStatus) bool) ([]string, cobra.ShellCompDirective) {
5760
globalOptions, err := helpers.ProcessRootCmdFlags(cmd)
5861
if err != nil {
5962
return nil, cobra.ShellCompDirectiveError
@@ -101,8 +104,8 @@ func shellCompleteContainerNames(cmd *cobra.Command, filterFunc func(containerd.
101104
return candidates, cobra.ShellCompDirectiveNoFileComp
102105
}
103106

104-
// shellCompleteNetworkNames includes {"bridge","host","none"}
105-
func shellCompleteNetworkNames(cmd *cobra.Command, exclude []string) ([]string, cobra.ShellCompDirective) {
107+
// ShellCompleteNetworkNames includes {"bridge","host","none"}
108+
func ShellCompleteNetworkNames(cmd *cobra.Command, exclude []string) ([]string, cobra.ShellCompDirective) {
106109
globalOptions, err := helpers.ProcessRootCmdFlags(cmd)
107110
if err != nil {
108111
return nil, cobra.ShellCompDirectiveError
@@ -134,7 +137,7 @@ func shellCompleteNetworkNames(cmd *cobra.Command, exclude []string) ([]string,
134137
return candidates, cobra.ShellCompDirectiveNoFileComp
135138
}
136139

137-
func shellCompleteVolumeNames(cmd *cobra.Command) ([]string, cobra.ShellCompDirective) {
140+
func ShellCompleteVolumeNames(cmd *cobra.Command) ([]string, cobra.ShellCompDirective) {
138141
globalOptions, err := helpers.ProcessRootCmdFlags(cmd)
139142
if err != nil {
140143
return nil, cobra.ShellCompDirectiveError
@@ -150,7 +153,7 @@ func shellCompleteVolumeNames(cmd *cobra.Command) ([]string, cobra.ShellCompDire
150153
return candidates, cobra.ShellCompDirectiveNoFileComp
151154
}
152155

153-
func shellCompletePlatforms(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
156+
func ShellCompletePlatforms(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
154157
candidates := []string{
155158
"amd64",
156159
"arm64",
@@ -163,3 +166,12 @@ func shellCompletePlatforms(cmd *cobra.Command, args []string, toComplete string
163166
}
164167
return candidates, cobra.ShellCompDirectiveNoFileComp
165168
}
169+
170+
func getVolumes(cmd *cobra.Command, globalOptions types.GlobalCommandOptions) (map[string]native.Volume, error) {
171+
volumeSize, err := cmd.Flags().GetBool("size")
172+
if err != nil {
173+
// The `nerdctl volume rm` does not have the flag `size`, so set it to false as the default value.
174+
volumeSize = false
175+
}
176+
return volume.Volumes(globalOptions.Namespace, globalOptions.DataRoot, globalOptions.Address, volumeSize, nil)
177+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
Copyright The containerd Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package completion
18+
19+
import "github.com/spf13/cobra"
20+
21+
func ShellCompleteCgroupManagerNames(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
22+
return nil, cobra.ShellCompDirectiveNoFileComp
23+
}

cmd/nerdctl/completion_linux.go renamed to cmd/nerdctl/completion/completion_linux.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,17 @@
1414
limitations under the License.
1515
*/
1616

17-
package main
17+
package completion
1818

1919
import (
2020
"github.com/spf13/cobra"
2121

2222
"github.com/containerd/nerdctl/v2/pkg/apparmorutil"
23+
ncdefaults "github.com/containerd/nerdctl/v2/pkg/defaults"
24+
"github.com/containerd/nerdctl/v2/pkg/rootlessutil"
2325
)
2426

25-
func shellCompleteApparmorProfiles(cmd *cobra.Command) ([]string, cobra.ShellCompDirective) {
27+
func ShellCompleteApparmorProfiles(cmd *cobra.Command) ([]string, cobra.ShellCompDirective) {
2628
profiles, err := apparmorutil.Profiles()
2729
if err != nil {
2830
return nil, cobra.ShellCompDirectiveError
@@ -33,3 +35,14 @@ func shellCompleteApparmorProfiles(cmd *cobra.Command) ([]string, cobra.ShellCom
3335
}
3436
return names, cobra.ShellCompDirectiveNoFileComp
3537
}
38+
39+
func ShellCompleteCgroupManagerNames(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
40+
candidates := []string{"cgroupfs"}
41+
if ncdefaults.IsSystemdAvailable() {
42+
candidates = append(candidates, "systemd")
43+
}
44+
if rootlessutil.IsRootless() {
45+
candidates = append(candidates, "none")
46+
}
47+
return candidates, cobra.ShellCompDirectiveNoFileComp
48+
}

cmd/nerdctl/completion_linux_test.go renamed to cmd/nerdctl/completion/completion_linux_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
limitations under the License.
1515
*/
1616

17-
package main
17+
package completion
1818

1919
import (
2020
"testing"
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
Copyright The containerd Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package completion
18+
19+
import (
20+
"testing"
21+
22+
"github.com/containerd/nerdctl/v2/pkg/testutil"
23+
)
24+
25+
func TestMain(m *testing.M) {
26+
testutil.M(m)
27+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
//go:build unix
2+
3+
/*
4+
Copyright The containerd Authors.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
18+
19+
package completion
20+
21+
import (
22+
"github.com/spf13/cobra"
23+
24+
"github.com/containerd/log"
25+
26+
"github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers"
27+
"github.com/containerd/nerdctl/v2/pkg/clientutil"
28+
"github.com/containerd/nerdctl/v2/pkg/infoutil"
29+
"github.com/containerd/nerdctl/v2/pkg/rootlessutil"
30+
)
31+
32+
func ShellCompleteNetworkDrivers(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
33+
candidates := []string{"bridge", "macvlan", "ipvlan"}
34+
return candidates, cobra.ShellCompDirectiveNoFileComp
35+
}
36+
37+
func ShellCompleteIPAMDrivers(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
38+
return []string{"default", "host-local", "dhcp"}, cobra.ShellCompDirectiveNoFileComp
39+
}
40+
41+
func ShellCompleteNamespaceNames(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
42+
globalOptions, err := helpers.ProcessRootCmdFlags(cmd)
43+
if err != nil {
44+
return nil, cobra.ShellCompDirectiveError
45+
}
46+
if rootlessutil.IsRootlessParent() {
47+
_ = rootlessutil.ParentMain(globalOptions.HostGatewayIP)
48+
return nil, cobra.ShellCompDirectiveNoFileComp
49+
}
50+
51+
client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), globalOptions.Namespace, globalOptions.Address)
52+
if err != nil {
53+
return nil, cobra.ShellCompDirectiveError
54+
}
55+
defer cancel()
56+
nsService := client.NamespaceService()
57+
nsList, err := nsService.List(ctx)
58+
if err != nil {
59+
log.L.Warn(err)
60+
return nil, cobra.ShellCompDirectiveError
61+
}
62+
var candidates []string
63+
candidates = append(candidates, nsList...)
64+
return candidates, cobra.ShellCompDirectiveNoFileComp
65+
}
66+
67+
func ShellCompleteSnapshotterNames(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
68+
globalOptions, err := helpers.ProcessRootCmdFlags(cmd)
69+
if err != nil {
70+
return nil, cobra.ShellCompDirectiveError
71+
}
72+
if rootlessutil.IsRootlessParent() {
73+
_ = rootlessutil.ParentMain(globalOptions.HostGatewayIP)
74+
return nil, cobra.ShellCompDirectiveNoFileComp
75+
}
76+
client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), globalOptions.Namespace, globalOptions.Address)
77+
if err != nil {
78+
return nil, cobra.ShellCompDirectiveError
79+
}
80+
defer cancel()
81+
snapshotterPlugins, err := infoutil.GetSnapshotterNames(ctx, client.IntrospectionService())
82+
if err != nil {
83+
return nil, cobra.ShellCompDirectiveError
84+
}
85+
var candidates []string
86+
candidates = append(candidates, snapshotterPlugins...)
87+
return candidates, cobra.ShellCompDirectiveNoFileComp
88+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
Copyright The containerd Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package completion
18+
19+
import "github.com/spf13/cobra"
20+
21+
func ShellCompleteNamespaceNames(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
22+
return nil, cobra.ShellCompDirectiveNoFileComp
23+
}
24+
25+
func ShellCompleteSnapshotterNames(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
26+
return nil, cobra.ShellCompDirectiveNoFileComp
27+
}
28+
29+
func ShellCompleteCgroupManagerNames(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
30+
return nil, cobra.ShellCompDirectiveNoFileComp
31+
}
32+
33+
func ShellCompleteNetworkDrivers(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
34+
candidates := []string{"nat"}
35+
return candidates, cobra.ShellCompDirectiveNoFileComp
36+
}
37+
38+
func ShellCompleteIPAMDrivers(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
39+
return []string{"default"}, cobra.ShellCompDirectiveNoFileComp
40+
}

cmd/nerdctl/container_attach.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121

2222
containerd "github.com/containerd/containerd/v2/client"
2323

24+
"github.com/containerd/nerdctl/v2/cmd/nerdctl/completion"
2425
"github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers"
2526
"github.com/containerd/nerdctl/v2/pkg/api/types"
2627
"github.com/containerd/nerdctl/v2/pkg/clientutil"
@@ -95,5 +96,5 @@ func attachShellComplete(cmd *cobra.Command, args []string, toComplete string) (
9596
statusFilterFn := func(st containerd.ProcessStatus) bool {
9697
return st == containerd.Running
9798
}
98-
return shellCompleteContainerNames(cmd, statusFilterFn)
99+
return completion.ShellCompleteContainerNames(cmd, statusFilterFn)
99100
}

0 commit comments

Comments
 (0)