Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds json response of version info #1262

Merged
merged 2 commits into from
May 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ type RootFlags struct {
version bool
}

type VersionInfo struct {
K3d string `json:"k3d"`
K3s string `json:"k3s"`
}

var flags = RootFlags{}

func NewCmdK3d() *cobra.Command {
Expand Down Expand Up @@ -207,20 +212,46 @@ func initRuntime() {
}

func NewCmdVersion() *cobra.Command {
type VersionOutputFormat string

const (
VersionOutputFormatJson VersionOutputFormat = "json"
)

cmd := &cobra.Command{
Use: "version",
Short: "Show k3d and default k3s version",
Long: "Show k3d and default k3s version",
Run: func(cmd *cobra.Command, args []string) {
printVersion()
output, _ := cmd.Flags().GetString("output")

if output == string(VersionOutputFormatJson) {
printJsonVersion()
} else {
printVersion()
}
},
Args: cobra.NoArgs,
}

cmd.Flags().StringP("output", "o", "", "This will return version information as a different format. Only json is supported")

cmd.AddCommand(NewCmdVersionLs())

return cmd
}

func printJsonVersion() {
versionInfo := VersionInfo{
K3d: version.GetVersion(),
K3s: version.K3sVersion,
}
versionJson, err := json.Marshal(versionInfo)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(versionJson))
}

func printVersion() {
Expand Down