Skip to content

Commit

Permalink
fix(version): add version command to show utility version info
Browse files Browse the repository at this point in the history
  • Loading branch information
vkumbhar94 committed Jul 4, 2022
1 parent 4c91607 commit e5fb522
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 3 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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
84 changes: 84 additions & 0 deletions cmd/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
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")
}
5 changes: 4 additions & 1 deletion completion.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,7 @@ commands:
- name: bash
- name: zsh
- name: powershell
- name: fish
- name: fish
- name: version
flags:
- short
15 changes: 14 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,21 @@ Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
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()
}
20 changes: 20 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
2 changes: 1 addition & 1 deletion plugin.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit e5fb522

Please sign in to comment.