Skip to content
This repository was archived by the owner on Nov 27, 2023. It is now read-only.

Commit d887bf7

Browse files
gtardifndeloof
authored andcommitted
Introduce compose version command
Signed-off-by: Guillaume Tardif <[email protected]> Signed-off-by: Nicolas De Loof <[email protected]>
1 parent 1dc0c4c commit d887bf7

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

cli/cmd/compose/compose.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ func Command(contextType string) *cobra.Command {
165165
eventsCommand(&opts),
166166
portCommand(&opts),
167167
imagesCommand(&opts),
168+
versionCommand(),
168169
)
169170

170171
if contextType == store.LocalContextType || contextType == store.DefaultContextType {

cli/cmd/compose/version.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
Copyright 2020 Docker Compose CLI 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 compose
18+
19+
import (
20+
"fmt"
21+
"strings"
22+
23+
"github.com/spf13/cobra"
24+
25+
"github.com/docker/compose-cli/cli/formatter"
26+
"github.com/docker/compose-cli/internal"
27+
)
28+
29+
type versionOptions struct {
30+
format string
31+
short bool
32+
}
33+
34+
func versionCommand() *cobra.Command {
35+
opts := versionOptions{}
36+
cmd := &cobra.Command{
37+
Use: "version",
38+
Short: "Show the Docker Compose version information",
39+
Args: cobra.MaximumNArgs(0),
40+
RunE: func(cmd *cobra.Command, _ []string) error {
41+
runVersion(opts)
42+
return nil
43+
},
44+
}
45+
// define flags for backward compatibility with com.docker.cli
46+
flags := cmd.Flags()
47+
flags.StringVarP(&opts.format, "format", "f", "", "Format the output. Values: [pretty | json]. (Default: pretty)")
48+
flags.BoolVar(&opts.short, "short", false, "Shows only Compose's version number.")
49+
50+
return cmd
51+
}
52+
53+
func runVersion(opts versionOptions) {
54+
displayedVersion := strings.TrimPrefix(internal.Version, "v")
55+
if opts.short {
56+
fmt.Println(displayedVersion)
57+
return
58+
}
59+
if opts.format == formatter.JSON {
60+
fmt.Printf(`{"version":"%s"}\n`, displayedVersion)
61+
return
62+
}
63+
fmt.Printf(`Docker Compose version %s`, displayedVersion)
64+
}

0 commit comments

Comments
 (0)