-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: versioning also adds a placeholder to command selector & params * remove placeholder
- Loading branch information
1 parent
af630f4
commit dec39e7
Showing
2 changed files
with
66 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package cmd | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"runtime" | ||
|
||
"github.com/spf13/cobra" | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
const ( | ||
flagFormat = "format" | ||
) | ||
|
||
var ( | ||
// Version defines the application version (defined at compile time) | ||
Version = "" | ||
|
||
// Commit defines the application commit hash (defined at compile time) | ||
Commit = "" | ||
|
||
versionFormat string | ||
) | ||
|
||
type versionInfo struct { | ||
Version string `json:"version" yaml:"version"` | ||
Commit string `json:"commit" yaml:"commit"` | ||
Go string `json:"go" yaml:"go"` | ||
} | ||
|
||
func getVersionCmd() *cobra.Command { | ||
versionCmd := &cobra.Command{ | ||
Use: "version", | ||
Short: "Print binary version information", | ||
RunE: func(_ *cobra.Command, _ []string) error { | ||
verInfo := versionInfo{ | ||
Version: Version, | ||
Commit: Commit, | ||
Go: fmt.Sprintf("%s %s/%s", runtime.Version(), runtime.GOOS, runtime.GOARCH), | ||
} | ||
|
||
var bz []byte | ||
|
||
var err error | ||
switch versionFormat { | ||
case "json": | ||
bz, err = json.Marshal(verInfo) | ||
|
||
default: | ||
bz, err = yaml.Marshal(&verInfo) | ||
} | ||
if err != nil { | ||
return err | ||
} | ||
|
||
_, err = fmt.Println(string(bz)) | ||
return err | ||
}, | ||
} | ||
|
||
versionCmd.Flags().StringVar(&versionFormat, flagFormat, "text", "Print the version in the given format (text|json)") | ||
|
||
return versionCmd | ||
} |