From 98026fbd433da5a81b5793db9d69f6b6bbddb895 Mon Sep 17 00:00:00 2001 From: Nicolas-Peiffer <102670102+Nicolas-Peiffer@users.noreply.github.com> Date: Thu, 8 Aug 2024 17:31:41 +0200 Subject: [PATCH] Fix gitignore and add version CLI command --- .gitignore | 1 - cmd/k8s-kms-plugin/cmd/version.go | 129 ++++++++++++++++++++++++++++++ 2 files changed, 129 insertions(+), 1 deletion(-) create mode 100644 cmd/k8s-kms-plugin/cmd/version.go diff --git a/.gitignore b/.gitignore index 000b8d2..51150c1 100644 --- a/.gitignore +++ b/.gitignore @@ -8,7 +8,6 @@ build/ generated/ deployed.json .socket -k8s-kms-plugin # dev pkg/crypto11 diff --git a/cmd/k8s-kms-plugin/cmd/version.go b/cmd/k8s-kms-plugin/cmd/version.go new file mode 100644 index 0000000..19a5c97 --- /dev/null +++ b/cmd/k8s-kms-plugin/cmd/version.go @@ -0,0 +1,129 @@ +/* +Copyright © 2024 NAME HERE +*/ +package cmd + +import ( + "encoding/json" + "fmt" + + "github.com/coreos/go-semver/semver" + "github.com/spf13/cobra" + "gopkg.in/yaml.v3" +) + +var ( + RawGitVersion string + CommitVersionIdShort string + CommitVersionIdLong string + OutputFormat string + GoVersion string + BuildPlatform string + BuildDate string +) + +type JsonVersion struct { + Major int64 `json:"major"` + Minor int64 `json:"minor"` + Version string `json:"version"` + CommitIdLong string `json:"commitIdLong"` + CommitIdShort string `json:"commitIdShort"` + GoVersion string `json:"goVersion"` + Date string `json:"date"` + Platorm string `json:"plaform"` +} +type YamlVersion struct { + Major int64 `yaml:"major"` + Minor int64 `yaml:"minor"` + Version string `yaml:"version"` + CommitIdLong string `yaml:"commitIdLong"` + CommitIdShort string `yaml:"commitIdShort"` + GoVersion string `yaml:"goVersion"` + Date string `yaml:"date"` + Platorm string `yaml:"plaform"` +} + +func validateInputs() { + if OutputFormat != "" && OutputFormat != "json" && OutputFormat != "yaml" { + OutputFormat = "" + } +} +func CreateJsonVersion() []byte { + version := semver.New(RawGitVersion) + + jsonFormat := &JsonVersion{ + Major: version.Major, + Minor: version.Minor, + Version: RawGitVersion, + CommitIdLong: CommitVersionIdLong, + CommitIdShort: CommitVersionIdShort, + GoVersion: GoVersion, + Date: BuildDate, + Platorm: BuildPlatform, + } + data, err := json.MarshalIndent(&jsonFormat, "", " ") + if err != nil { + + fmt.Println(err) + } + return data +} +func CreateYamlVersion() []byte { + version := semver.New(RawGitVersion) + + yamlFormat := &YamlVersion{ + Major: version.Major, + Minor: version.Minor, + Version: RawGitVersion, + CommitIdLong: CommitVersionIdLong, + CommitIdShort: CommitVersionIdShort, + GoVersion: GoVersion, + Date: BuildDate, + Platorm: BuildPlatform, + } + data, err := yaml.Marshal(&yamlFormat) + if err != nil { + + fmt.Println(err) + } + return data +} + +func generateOutput() { + if OutputFormat == "json" { + fmt.Println(string(CreateJsonVersion())) + } else if OutputFormat == "yaml" { + fmt.Println(string(CreateYamlVersion())) + } else { + fmt.Println(RawGitVersion) + } +} + +// versionCmd represents the version command +var versionCmd = &cobra.Command{ + Use: "version", + Short: "Show the version of the application with the short commit sha associated", + Run: func(cmd *cobra.Command, args []string) { + if OutputFormat == "" { + fmt.Println(RawGitVersion) + } else { + validateInputs() + generateOutput() + } + + }, +} + +func init() { + rootCmd.AddCommand(versionCmd) + versionCmd.Flags().StringVarP(&OutputFormat, "output", "o", "json", "'json' or 'yaml'") + // Here you will define your flags and configuration settings. + + // Cobra supports Persistent Flags which will work for this command + // and all subcommands, e.g.: + // versionCmd.PersistentFlags().String("foo", "", "A help for foo") + + // Cobra supports local flags which will only run when this command + // is called directly, e.g.: + // versionCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") +}