Skip to content

Commit f7e033d

Browse files
committed
Move IsExactArgs and FindIPv6 to helpers
Signed-off-by: apostasie <[email protected]>
1 parent d2741ba commit f7e033d

14 files changed

+68
-45
lines changed

cmd/nerdctl/container_commit.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func newCommitCommand() *cobra.Command {
3030
var commitCommand = &cobra.Command{
3131
Use: "commit [flags] CONTAINER REPOSITORY[:TAG]",
3232
Short: "Create a new image from a container's changes",
33-
Args: IsExactArgs(2),
33+
Args: helpers.IsExactArgs(2),
3434
RunE: commitAction,
3535
ValidArgsFunction: commitShellComplete,
3636
SilenceUsage: true,

cmd/nerdctl/container_cp_linux.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ Using 'nerdctl cp' with untrusted or malicious containers is unsupported and may
4444
nerdctl cp [flags] SRC_PATH|- CONTAINER:DEST_PATH`
4545
var cpCommand = &cobra.Command{
4646
Use: usage,
47-
Args: IsExactArgs(2),
47+
Args: helpers.IsExactArgs(2),
4848
Short: shortHelp,
4949
Long: longHelp,
5050
RunE: cpAction,

cmd/nerdctl/container_logs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ The following containers are supported:
4040
`
4141
var logsCommand = &cobra.Command{
4242
Use: "logs [flags] CONTAINER",
43-
Args: IsExactArgs(1),
43+
Args: helpers.IsExactArgs(1),
4444
Short: shortUsage,
4545
Long: longUsage,
4646
RunE: logsAction,

cmd/nerdctl/container_rename.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import (
2929
func newRenameCommand() *cobra.Command {
3030
var renameCommand = &cobra.Command{
3131
Use: "rename [flags] CONTAINER NEW_NAME",
32-
Args: IsExactArgs(2),
32+
Args: helpers.IsExactArgs(2),
3333
Short: "rename a container",
3434
RunE: renameAction,
3535
ValidArgsFunction: renameShellComplete,

cmd/nerdctl/container_run_network_linux_test.go

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

3232
"github.com/containerd/errdefs"
3333

34+
"github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers"
3435
"github.com/containerd/nerdctl/v2/pkg/rootlessutil"
3536
"github.com/containerd/nerdctl/v2/pkg/testutil"
3637
"github.com/containerd/nerdctl/v2/pkg/testutil/nettestutil"
@@ -632,7 +633,7 @@ func TestRunContainerWithStaticIP6(t *testing.T) {
632633
return
633634
}
634635
cmd.AssertOutWithFunc(func(stdout string) error {
635-
ip := findIPv6(stdout)
636+
ip := helpers.FindIPv6(stdout)
636637
if !subnet.Contains(ip) {
637638
return fmt.Errorf("expected subnet %s include ip %s", subnet, ip)
638639
}

cmd/nerdctl/helpers/cobra.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,21 @@ func UnknownSubcommandAction(cmd *cobra.Command, args []string) error {
4141
}
4242
return errors.New(msg)
4343
}
44+
45+
// IsExactArgs returns an error if there is not the exact number of args
46+
func IsExactArgs(number int) cobra.PositionalArgs {
47+
return func(cmd *cobra.Command, args []string) error {
48+
if len(args) == number {
49+
return nil
50+
}
51+
return fmt.Errorf(
52+
"%q requires exactly %d %s.\nSee '%s --help'.\n\nUsage: %s\n\n%s",
53+
cmd.CommandPath(),
54+
number,
55+
"argument(s)",
56+
cmd.CommandPath(),
57+
cmd.UseLine(),
58+
cmd.Short,
59+
)
60+
}
61+
}

cmd/nerdctl/helpers/testing.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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 helpers
18+
19+
import (
20+
"net"
21+
"strings"
22+
)
23+
24+
func FindIPv6(output string) net.IP {
25+
var ipv6 string
26+
lines := strings.Split(output, "\n")
27+
for _, line := range lines {
28+
if strings.Contains(line, "inet6") {
29+
fields := strings.Fields(line)
30+
if len(fields) > 1 {
31+
ipv6 = strings.Split(fields[1], "/")[0]
32+
break
33+
}
34+
}
35+
}
36+
return net.ParseIP(ipv6)
37+
}

cmd/nerdctl/image_history.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func newHistoryCommand() *cobra.Command {
4747
var historyCommand = &cobra.Command{
4848
Use: "history [flags] IMAGE",
4949
Short: "Show the history of an image",
50-
Args: IsExactArgs(1),
50+
Args: helpers.IsExactArgs(1),
5151
RunE: historyAction,
5252
ValidArgsFunction: historyShellComplete,
5353
SilenceUsage: true,

cmd/nerdctl/image_pull.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func newPullCommand() *cobra.Command {
3232
var pullCommand = &cobra.Command{
3333
Use: "pull [flags] NAME[:TAG]",
3434
Short: "Pull an image from a registry. Optionally specify \"ipfs://\" or \"ipns://\" scheme to pull image from IPFS.",
35-
Args: IsExactArgs(1),
35+
Args: helpers.IsExactArgs(1),
3636
RunE: pullAction,
3737
SilenceUsage: true,
3838
SilenceErrors: true,

cmd/nerdctl/image_push.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func newPushCommand() *cobra.Command {
3434
var pushCommand = &cobra.Command{
3535
Use: "push [flags] NAME[:TAG]",
3636
Short: "Push an image or a repository to a registry. Optionally specify \"ipfs://\" or \"ipns://\" scheme to push image to IPFS.",
37-
Args: IsExactArgs(1),
37+
Args: helpers.IsExactArgs(1),
3838
RunE: pushAction,
3939
ValidArgsFunction: pushShellComplete,
4040
SilenceUsage: true,

0 commit comments

Comments
 (0)