|
| 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