-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update k8s-kms-plugin version command
Signed-off-by: Nicolas-Peiffer <[email protected]>
- Loading branch information
1 parent
ca4b69c
commit 2a9cff0
Showing
5 changed files
with
293 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,129 +1,77 @@ | ||
/* | ||
Copyright © 2024 NAME HERE <EMAIL ADDRESS> | ||
*/ | ||
// MIT License | ||
// | ||
// Copyright (c) 2024 Thales. All rights reserved. | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
|
||
package cmd | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"log/slog" | ||
|
||
"github.com/coreos/go-semver/semver" | ||
"github.com/spf13/cobra" | ||
"gopkg.in/yaml.v3" | ||
) | ||
"github.com/ThalesGroup/k8s-kms-plugin/pkg/version" | ||
|
||
var ( | ||
RawGitVersion string | ||
CommitVersionIdShort string | ||
CommitVersionIdLong string | ||
OutputFormat string | ||
GoVersion string | ||
BuildPlatform string | ||
BuildDate string | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
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 { | ||
// CLI options pflags names | ||
var outputVersion string // One of 'yaml' or 'json'. | ||
|
||
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) | ||
} | ||
} | ||
// prettyPrintVersion defined by the user with flag --pretty | ||
var prettyPrintVersion bool | ||
|
||
// versionCmd represents the version command | ||
var versionCmd = &cobra.Command{ | ||
Use: "version", | ||
Short: "Show the version of the application with the short commit sha associated", | ||
Short: "Print the version information.", | ||
Long: `Print the version information with various level of details | ||
including information of the build and git repository metadata. | ||
Examples: | ||
# print the version information with git repository details as a one liner | ||
# JSON string. | ||
k8s-kms-plugin version -o json --pretty=false`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
if OutputFormat == "" { | ||
fmt.Println(RawGitVersion) | ||
} else { | ||
validateInputs() | ||
generateOutput() | ||
slog.Debug("running command: k8s-kms-plugin version") | ||
|
||
// Add logic here to check if flag output is used with JSON or YAML. | ||
flagOutputUsed := cmd.Flags().Changed("output") | ||
flagPrettyUsed := cmd.Flags().Changed("pretty") | ||
|
||
if flagOutputUsed && !(outputVersion == "yaml" || outputVersion == "json") { | ||
slog.Error("Invalid output format. Must be json or yaml.", | ||
"output", outputVersion) | ||
} | ||
|
||
if flagOutputUsed && flagPrettyUsed && outputVersion == "yaml" { | ||
slog.Error("Flag --pretty must NOT be used when output format is yaml." + | ||
" --pretty is only used when output format is json") | ||
} else { | ||
fmt.Fprintln(cmd.OutOrStdout(), version.VersionOutputToString(outputVersion, prettyPrintVersion)) | ||
} | ||
}, | ||
} | ||
|
||
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") | ||
versionCmd.Flags().StringVarP(&outputVersion, "output", "o", "", "Format of the version output. One of 'yaml' or 'json'.") | ||
versionCmd.PersistentFlags().BoolVarP(&prettyPrintVersion, "pretty", "", true, "Activate pretty print output for JSON.") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.