Skip to content

Commit 3e1b10d

Browse files
Add nerdctl compose version command
Made it exactly like the Docker compose version. For the actual version, I just used the nerdctl version Co-authored-by: Akihiro Suda <[email protected]> Signed-off-by: Yarden Shoham <[email protected]>
1 parent 1a3ec3f commit 3e1b10d

File tree

4 files changed

+119
-0
lines changed

4 files changed

+119
-0
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,7 @@ It does not necessarily mean that the corresponding features are missing in cont
338338
- [:whale: nerdctl compose config](#whale-nerdctl-compose-config)
339339
- [:whale: nerdctl compose kill](#whale-nerdctl-compose-kill)
340340
- [:whale: nerdctl compose run](#whale-nerdctl-compose-run)
341+
- [:whale: nerdctl compose version](#whale-nerdctl-compose-version)
341342
- [IPFS management](#ipfs-management)
342343
- [:nerd_face: nerdctl ipfs registry up](#nerd_face-nerdctl-ipfs-registry-up)
343344
- [:nerd_face: nerdctl ipfs registry down](#nerd_face-nerdctl-ipfs-registry-down)
@@ -1453,6 +1454,15 @@ Unimplemented `docker-compose run` (V1) flags: `--use-aliases`, `--no-TTY`
14531454

14541455
Unimplemented `docker compose run` (V2) flags: `--use-aliases`, `--no-TTY`, `--tty`
14551456

1457+
### :whale: nerdctl compose version
1458+
Show the Compose version information (which is the nerdctl version)
1459+
1460+
Usage: `nerdctl compose version`
1461+
1462+
Flags:
1463+
- :whale: `-f, --format`: Format the output. Values: [pretty | json] (default "pretty")
1464+
- :whale: `--short`: Shows only Compose's version number
1465+
14561466
## IPFS management
14571467

14581468
P2P image distribution (IPFS) is completely optional. Your host is NOT connected to any P2P network, unless you opt in to [install and run IPFS daemon](https://docs.ipfs.io/install/).

cmd/nerdctl/compose.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ func newComposeCommand() *cobra.Command {
6060
newComposePsCommand(),
6161
newComposeKillCommand(),
6262
newComposeRunCommand(),
63+
newComposeVersionCommand(),
6364
)
6465

6566
return composeCommand

cmd/nerdctl/compose_version.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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 main
18+
19+
import (
20+
"fmt"
21+
"strings"
22+
23+
"github.com/containerd/nerdctl/pkg/version"
24+
"github.com/spf13/cobra"
25+
)
26+
27+
func newComposeVersionCommand() *cobra.Command {
28+
var composeVersionCommand = &cobra.Command{
29+
Use: "version",
30+
Short: "Show the Compose version information",
31+
Args: cobra.NoArgs,
32+
RunE: composeVersionAction,
33+
SilenceUsage: true,
34+
SilenceErrors: true,
35+
}
36+
composeVersionCommand.Flags().StringP("format", "f", "pretty", "Format the output. Values: [pretty | json]")
37+
composeVersionCommand.RegisterFlagCompletionFunc("format", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
38+
return []string{"json", "pretty"}, cobra.ShellCompDirectiveNoFileComp
39+
})
40+
composeVersionCommand.Flags().Bool("short", false, "Shows only Compose's version number")
41+
return composeVersionCommand
42+
}
43+
44+
func composeVersionAction(cmd *cobra.Command, args []string) error {
45+
short, err := cmd.Flags().GetBool("short")
46+
if err != nil {
47+
return err
48+
}
49+
if short {
50+
fmt.Fprintln(cmd.OutOrStdout(), strings.TrimPrefix(version.Version, "v"))
51+
return nil
52+
}
53+
54+
format, err := cmd.Flags().GetString("format")
55+
if err != nil {
56+
return err
57+
}
58+
switch format {
59+
case "pretty":
60+
fmt.Fprintln(cmd.OutOrStdout(), "nerdctl Compose version "+version.Version)
61+
case "json":
62+
fmt.Fprintf(cmd.OutOrStdout(), "{\"version\":\"%v\"}\n", version.Version)
63+
default:
64+
return fmt.Errorf("format can be either pretty or json, not %v", format)
65+
}
66+
67+
return nil
68+
}

cmd/nerdctl/compose_version_test.go

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 main
18+
19+
import (
20+
"testing"
21+
22+
"github.com/containerd/nerdctl/pkg/testutil"
23+
)
24+
25+
func TestComposeVersion(t *testing.T) {
26+
base := testutil.NewBase(t)
27+
base.ComposeBinary = "" // test with docker compose version, not docker-compose version
28+
base.ComposeCmd("version").AssertOutContains("Compose version ")
29+
}
30+
31+
func TestComposeVersionShort(t *testing.T) {
32+
base := testutil.NewBase(t)
33+
base.ComposeCmd("version", "--short").AssertOK()
34+
}
35+
36+
func TestComposeVersionJson(t *testing.T) {
37+
base := testutil.NewBase(t)
38+
base.ComposeBinary = "" // test with docker compose version, not docker-compose version
39+
base.ComposeCmd("version", "--format", "json").AssertOutContains("{\"version\":\"")
40+
}

0 commit comments

Comments
 (0)