From aaaf30e7ccd19d5734a806f1862d357e185dde71 Mon Sep 17 00:00:00 2001 From: Navratan Lal Gupta Date: Fri, 31 Mar 2023 19:33:24 +0530 Subject: [PATCH] feeat #5: Print version --- cmd/version.go | 47 +++++++++++++++++++++++++++++++++++++++ internal/config/config.go | 11 +++++++++ 2 files changed, 58 insertions(+) create mode 100644 cmd/version.go diff --git a/cmd/version.go b/cmd/version.go new file mode 100644 index 0000000..4078faf --- /dev/null +++ b/cmd/version.go @@ -0,0 +1,47 @@ +/* +Copyright © 2023 NAME HERE +*/ +package cmd + +import ( + "encoding/json" + "fmt" + "os" + + "github.com/navilg/k8senv/internal/config" + "github.com/spf13/cobra" +) + +// versionCmd represents the version command +var versionCmd = &cobra.Command{ + Use: "version", + Short: "Print version of K8senv", + // Long: `A longer description that spans multiple lines and likely contains examples + // and usage of using your command. For example: + + // Cobra is a CLI library for Go that empowers applications. + // This application is a tool to generate the needed files + // to quickly create a Cobra application.`, + Run: func(cmd *cobra.Command, args []string) { + data, err := json.Marshal(config.Version) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + fmt.Println("VersionInfo", string(data)) + }, +} + +func init() { + rootCmd.AddCommand(versionCmd) + + // 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") +} diff --git a/internal/config/config.go b/internal/config/config.go index 50e3188..682fd10 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -2,9 +2,20 @@ package config import ( "os" + "runtime" "strings" ) +type VersionInfo struct { + GoVersion string `json:"GoVersion"` + K8senv string `json:"K8senv"` +} + +var Version = VersionInfo{ + GoVersion: runtime.Version(), + K8senv: "v0.1.2", +} + func GetDotK8senvPath() *string { pathenv := os.Getenv("PATH") paths := strings.Split(pathenv, ":")