From e5fb5222ac8e481bd03fcaa41ed10f487ca0391e Mon Sep 17 00:00:00 2001 From: vkumbhar94 Date: Mon, 4 Jul 2022 17:08:19 +0530 Subject: [PATCH] fix(version): add version command to show utility version info --- .gitignore | 8 +++++ cmd/version.go | 84 +++++++++++++++++++++++++++++++++++++++++++++++++ completion.yaml | 5 ++- main.go | 15 ++++++++- package.json | 20 ++++++++++++ plugin.yaml | 2 +- 6 files changed, 131 insertions(+), 3 deletions(-) create mode 100644 cmd/version.go create mode 100644 package.json diff --git a/.gitignore b/.gitignore index d8d7072..cbb5240 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,11 @@ .idea/ bin/ dist/ +node_modules/ +package-lock.json +argus-configuration.yaml +collectorset-controller-configuration.yaml +argus.yaml.bkp +collectorset-controller.yaml.bkp +lmc-configuration.yaml +lmc-configuration.json diff --git a/cmd/version.go b/cmd/version.go new file mode 100644 index 0000000..77954b5 --- /dev/null +++ b/cmd/version.go @@ -0,0 +1,84 @@ +/* +Copyright © 2022 NAME HERE + +*/ +package cmd + +import ( + "encoding/json" + "github.com/spf13/cobra" + "time" +) + +var ( + Version = "dev" + Commit = "none" + Date = "unknown" + BuiltBy = "unknown" +) + +type versionMap struct { + Version string + Commit string + BuildDateUTC time.Time + BuildDate time.Time + BuiltBy string +} + +// versionCmd represents the version command +var versionCmd = &cobra.Command{ + Use: "version", + ValidArgsFunction: cobra.NoFileCompletions, + Short: "A brief description of your command", + 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) { + if Short { + cmd.Printf("%s\n", Version) + return + } + vm := NewVersionMap() + marshal, err := json.Marshal(vm) // nolint: errcheck + if err != nil { + cmd.Printf("failing to print version info: %s\n", err) + return + } + cmd.Println(string(marshal)) + }, +} + +func NewVersionMap() *versionMap { + vm := &versionMap{ + Version: Version, + Commit: Commit, + BuiltBy: BuiltBy, + } + date, err := time.Parse(time.RFC3339, Date) + if err != nil { + return nil + } + vm.BuildDateUTC = date.UTC() + vm.BuildDate = date.Local() + return vm +} + +var Short bool + +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") + versionCmd.Flags().BoolVar(&Short, "short", false, "Show short Version") +} diff --git a/completion.yaml b/completion.yaml index ba96c1f..9dfb1b5 100644 --- a/completion.yaml +++ b/completion.yaml @@ -21,4 +21,7 @@ commands: - name: bash - name: zsh - name: powershell - - name: fish \ No newline at end of file + - name: fish + - name: version + flags: + - short \ No newline at end of file diff --git a/main.go b/main.go index 6332514..bbf80da 100644 --- a/main.go +++ b/main.go @@ -4,8 +4,21 @@ Copyright © 2022 NAME HERE */ package main -import "github.com/logicmonitor/lmc/cmd" +import ( + "github.com/logicmonitor/lmc/cmd" +) + +var ( + version = "dev" + commit = "none" + date = "unknown" + builtBy = "unknown" +) func main() { + cmd.Version = version + cmd.Commit = commit + cmd.Date = date + cmd.BuiltBy = builtBy cmd.Execute() } diff --git a/package.json b/package.json new file mode 100644 index 0000000..3241f21 --- /dev/null +++ b/package.json @@ -0,0 +1,20 @@ +{ + "dependencies": { + "@commitlint/cli": "^12.1.4", + "@commitlint/config-conventional": "^12.1.4", + "husky": "^7.0.4" + }, + "devDependencies": { + "commitizen": "^4.2.4", + "cz-conventional-changelog": "^3.0.1" + }, + "scripts": { + "prepare": "husky install", + "postinstall": "husky install" + }, + "config": { + "commitizen": { + "path": "./node_modules/cz-conventional-changelog" + } + } +} diff --git a/plugin.yaml b/plugin.yaml index 16bfcea..6e29b7d 100644 --- a/plugin.yaml +++ b/plugin.yaml @@ -1,5 +1,5 @@ name: "lmc" -version: "1.0.0-ea03" +version: "1.0.0-ea04" usage: "Logicmonitor LM Container Utility" description: |- Logicmonitor LM Container Utility