|
| 1 | +package version |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "runtime/debug" |
| 8 | + "strings" |
| 9 | + |
| 10 | + _ "embed" |
| 11 | + |
| 12 | + "github.com/spf13/cobra" |
| 13 | +) |
| 14 | + |
| 15 | +const ( |
| 16 | + FlagFormat = "format" |
| 17 | + FlagFormatShortHand = "f" |
| 18 | + FlagFormatOCMv1 = "legacyjson" |
| 19 | + FlagFormatGoBuildInfo = "gobuildinfo" |
| 20 | + FlagFormatGoBuildInfoJSON = "gobuildinfojson" |
| 21 | +) |
| 22 | + |
| 23 | +// BuildVersion is an external variable that can be set at build time to override the version. |
| 24 | +// It is set to "n/a" by default, indicating that no version has been specified. |
| 25 | +// The variable can be adjusted at build time with |
| 26 | +// |
| 27 | +// -ldflags "-X ocm.software/open-component-model/cli/cmd/version.BuildVersion=1.2.3" |
| 28 | +// |
| 29 | +// The build version accepted is interpreted differently depending on the format: |
| 30 | +// - For `ocmv1`, it is expected to be a semantic version (e.g., "1.2.3") and will be split |
| 31 | +// for a json like output |
| 32 | +// - For `gobuildinfo`, it can be any version string, including a full semantic version. |
| 33 | +// If set, it will override the detected module build version from the Go build info. |
| 34 | +var BuildVersion = "n/a" |
| 35 | + |
| 36 | +func New() *cobra.Command { |
| 37 | + cmd := &cobra.Command{ |
| 38 | + Use: "version", |
| 39 | + Short: "Retrieve the build version of the OCM CLI", |
| 40 | + Long: fmt.Sprintf(`The version command retrieves the build version of the OCM CLI. |
| 41 | +
|
| 42 | +The build version can be formatted in different ways depending on the specified %[1]s flag. |
| 43 | +The default format is %[2]q, which outputs the version in a format compatible with OCM v1 specifications, |
| 44 | +with slight modifications: |
| 45 | +
|
| 46 | +- "gitTreeState" is removed in favor of "meta" field, which contains the git tree state. |
| 47 | +- "buildDate" and "gitCommit" are derived from the input version string, and are parsed according to the go module version specification. |
| 48 | +
|
| 49 | +When the format is set to %[3]q, it outputs the Go build information as a string. The format is standardized |
| 50 | +and unified across all golang applications. |
| 51 | +
|
| 52 | +When the format is set to %[4]q, it outputs the Go build information in JSON format. |
| 53 | +This is equivalent to %[3]q, but in a structured JSON format. |
| 54 | +
|
| 55 | +The build info by default is drawn from the go module build information, which is set at build time of the CLI. |
| 56 | +When officially built, it is possibly overwritten with the released version of the OCM CLI.`, FlagFormat, FlagFormatOCMv1, FlagFormatGoBuildInfo, FlagFormatGoBuildInfoJSON), |
| 57 | + Example: fmt.Sprintf(`ocm version --format %s`, FlagFormatOCMv1), |
| 58 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 59 | + format, err := cmd.Flags().GetString(FlagFormat) |
| 60 | + if err != nil { |
| 61 | + return err |
| 62 | + } |
| 63 | + ver, ok := debug.ReadBuildInfo() |
| 64 | + if !ok { |
| 65 | + return fmt.Errorf("no build info available") |
| 66 | + } |
| 67 | + if BuildVersion != "n/a" { |
| 68 | + // Override the version if specified |
| 69 | + ver.Main.Version = BuildVersion |
| 70 | + } |
| 71 | + switch format { |
| 72 | + case FlagFormatOCMv1: |
| 73 | + ver, err := GetLegacyFormat(ver) |
| 74 | + if err != nil { |
| 75 | + return err |
| 76 | + } |
| 77 | + return json.NewEncoder(cmd.OutOrStdout()).Encode(ver) |
| 78 | + case FlagFormatGoBuildInfo: |
| 79 | + str := ver.String() |
| 80 | + _, err = io.Copy(cmd.OutOrStdout(), strings.NewReader(str)) |
| 81 | + return err |
| 82 | + case FlagFormatGoBuildInfoJSON: |
| 83 | + return json.NewEncoder(cmd.OutOrStdout()).Encode(ver) |
| 84 | + default: |
| 85 | + return cmd.Help() |
| 86 | + } |
| 87 | + }, |
| 88 | + DisableAutoGenTag: true, |
| 89 | + SilenceUsage: true, |
| 90 | + } |
| 91 | + |
| 92 | + cmd.Flags().StringP(FlagFormat, FlagFormatShortHand, FlagFormatOCMv1, "format of the generated documentation") |
| 93 | + return cmd |
| 94 | +} |
0 commit comments