Skip to content

Commit 63c0c6c

Browse files
mohammed786ravitejag
authored andcommitted
Implemented Version Command
Added User Agent for all the API Calls
1 parent 83c3b84 commit 63c0c6c

12 files changed

Lines changed: 142 additions & 38 deletions

File tree

.goreleaser.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ builds:
99
- <<: &build_defaults
1010
binary: lr
1111
main: ./app/lr
12+
ldflags:
13+
- -s -w -X github.com/loginradius/lr-cli/internal/build.Version={{.Version}} -X github.com/loginradius/lr-cli/internal/build.Date={{time "2006-01-02"}}
14+
- -X main.updaterEnabled=cli/cli
1215
id: macos
1316
goos: [darwin]
1417
goarch: [amd64]

cmd/get/email/email.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ func get() error {
5353
if err != nil {
5454
return err
5555
}
56-
fmt.Printf("{EmailLinkExpire, EmailNotificationCount, EmailNotificationFrequency}\n")
57-
fmt.Println(resultResp)
56+
fmt.Println("EmailLinkExpire: ", resultResp.EmailLinkExpire)
57+
fmt.Println("EmailNotificationCount: ", resultResp.EmailNotificationCount)
58+
fmt.Println("EmailNotificationFrequency: ", resultResp.EmailNotificationFrequency)
5859
return nil
5960
}

cmd/login/login.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,10 @@ func NewLoginCmd() *cobra.Command {
3737
$ lr login
3838
`),
3939
RunE: func(cmd *cobra.Command, args []string) error {
40-
isValid, err := validateLogin()
40+
isValid := validateLogin()
4141

42-
if err != nil {
43-
return err
44-
} else if isValid {
45-
fmt.Printf("%s", "You are already been logged in")
42+
if isValid {
43+
fmt.Println("You are already logged in")
4644
return nil
4745
}
4846
cmdutil.Openbrowser(conf.HubPageDomain + "/auth.aspx?return_url=http://localhost:8089/postLogin")
@@ -88,14 +86,17 @@ func doLogin(accessToken string) error {
8886
return nil
8987
}
9088

91-
func validateLogin() (bool, error) {
89+
func validateLogin() bool {
9290
_, err := cmdutil.ReadFile("token.json")
9391
if err != nil {
94-
return false, nil
92+
return false
9593
}
9694
resObj, err := api.AuthValidateToken()
95+
if err != nil {
96+
return false
97+
}
9798
if resObj.AccessToken != "" {
98-
return true, nil
99+
return true
99100
}
100-
return false, nil
101+
return false
101102
}

cmd/root/root.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@ import (
66
"github.com/loginradius/lr-cli/cmd/generateSott"
77
"github.com/loginradius/lr-cli/cmd/resetSecret"
88
"github.com/loginradius/lr-cli/cmd/set"
9+
"github.com/loginradius/lr-cli/cmd/version"
910

1011
"github.com/loginradius/lr-cli/cmd/delete"
1112
"github.com/loginradius/lr-cli/cmd/get"
1213
"github.com/loginradius/lr-cli/cmd/login"
1314
"github.com/loginradius/lr-cli/cmd/logout"
1415
"github.com/loginradius/lr-cli/cmd/register"
1516
"github.com/loginradius/lr-cli/cmd/verify"
17+
18+
"github.com/loginradius/lr-cli/internal/build"
1619
"github.com/spf13/cobra"
1720
)
1821

@@ -30,10 +33,18 @@ func NewRootCmd() *cobra.Command {
3033
helpHelper := func(command *cobra.Command, args []string) {
3134
rootHelpFunc(command, args)
3235
}
33-
36+
rootCmd.PersistentFlags().Bool("help", false, "Show help for command")
3437
rootCmd.SetHelpFunc(helpHelper)
3538

36-
// Authentication Commands
39+
formattedVersion := version.Format(build.Version, build.Date)
40+
rootCmd.SetVersionTemplate(formattedVersion)
41+
rootCmd.Version = formattedVersion
42+
rootCmd.Flags().Bool("version", false, "Show lr version")
43+
44+
// Child Commands
45+
versionCmd := version.NewCmdVersion(build.Version, build.Date)
46+
rootCmd.AddCommand(versionCmd)
47+
3748
loginCmd := login.NewLoginCmd()
3849
rootCmd.AddCommand((loginCmd))
3950

cmd/set/email/email.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ func NewemailCmd() *cobra.Command {
5050
}
5151
fl := cmd.Flags()
5252

53-
fl.IntVarP(&opts.EmailLinkExpire, "link_expire", "a", 0, "email link expire")
54-
fl.IntVarP(&opts.EmailNotificationCount, "notif_count", "b", 0, "number of email notifications")
55-
fl.IntVarP(&opts.EmailNotificationFrequency, "notif_frequency", "c", 0, "frequency of email notification")
53+
fl.IntVarP(&opts.EmailLinkExpire, "link_expire", "l", 0, "Email Token Validity (Minutes)")
54+
fl.IntVarP(&opts.EmailNotificationCount, "notif_count", "c", 0, "Request Limit")
55+
fl.IntVarP(&opts.EmailNotificationFrequency, "notif_frequency", "f", 0, "Request Disabled Period (Minutes)")
5656

5757
return cmd
5858
}

cmd/version/version.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package version
2+
3+
import (
4+
"fmt"
5+
"regexp"
6+
"strings"
7+
8+
"github.com/spf13/cobra"
9+
)
10+
11+
func NewCmdVersion(version, buildDate string) *cobra.Command {
12+
cmd := &cobra.Command{
13+
Use: "version",
14+
Hidden: true,
15+
Run: func(cmd *cobra.Command, args []string) {
16+
fmt.Println(Format(version, buildDate))
17+
},
18+
}
19+
20+
return cmd
21+
}
22+
23+
func Format(version, buildDate string) string {
24+
version = strings.TrimPrefix(version, "v")
25+
26+
var dateStr string
27+
if buildDate != "" {
28+
dateStr = fmt.Sprintf(" (%s)", buildDate)
29+
}
30+
31+
return fmt.Sprintf("lr version %s%s\n%s\n", version, dateStr, changelogURL(version))
32+
}
33+
34+
func changelogURL(version string) string {
35+
path := "https://github.com/loginradius/lr-cli"
36+
r := regexp.MustCompile(`^v?\d+\.\d+\.\d+(-[\w.]+)?$`)
37+
if !r.MatchString(version) {
38+
return fmt.Sprintf("%s/releases/latest", path)
39+
}
40+
41+
url := fmt.Sprintf("%s/releases/tag/v%s", path, strings.TrimPrefix(version, "v"))
42+
return url
43+
}

cmdutil/lrUtils.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ import (
1111
"path"
1212
"path/filepath"
1313
"runtime"
14+
15+
"github.com/shirou/gopsutil/host"
16+
17+
"github.com/loginradius/lr-cli/internal/build"
1418
)
1519

1620
func ReadFile(filename string) ([]byte, error) {
@@ -98,3 +102,9 @@ func DeleteFile(filename string) error {
98102
}
99103
return nil
100104
}
105+
func UAString() string {
106+
if info, err := host.Info(); err == nil {
107+
return `LRCLI/` + build.Version + " (" + info.Platform + "; " + info.KernelArch + ") " + info.OS + "/" + info.PlatformVersion + " (" + info.Hostname + ")"
108+
}
109+
return ""
110+
}

go.mod

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@ go 1.16
44

55
require (
66
github.com/MakeNowJust/heredoc v1.0.0
7+
github.com/StackExchange/wmi v0.0.0-20210224194228-fe8f1750fd46 // indirect
78
github.com/cli/safeexec v1.0.0
9+
github.com/go-ole/go-ole v1.2.5 // indirect
810
github.com/gorilla/mux v1.8.0
911
github.com/gorilla/schema v1.2.0
1012
github.com/kr/text v0.2.0
1113
github.com/rs/cors v1.7.0
14+
github.com/shirou/gopsutil v3.21.4+incompatible
1215
github.com/spf13/cobra v1.1.3
16+
github.com/tklauser/go-sysconf v0.3.5 // indirect
1317
)
1418

1519
replace github.com/loginradius/lr-cli => ../lr-cli

go.sum

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym
1616
github.com/MakeNowJust/heredoc v1.0.0 h1:cXCdzVdstXyiTqTvfqk9SDHpKNjxuom+DOlyEeQ4pzQ=
1717
github.com/MakeNowJust/heredoc v1.0.0/go.mod h1:mG5amYoWBHf8vpLOuehzbGGw0EHxpZZ6lCpQ4fNJ8LE=
1818
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
19+
github.com/StackExchange/wmi v0.0.0-20210224194228-fe8f1750fd46 h1:5sXbqlSomvdjlRbWyNqkPsJ3Fg+tQZCbgeX1VGljbQY=
20+
github.com/StackExchange/wmi v0.0.0-20210224194228-fe8f1750fd46/go.mod h1:3eOhrUMpNV+6aFIbp5/iudMxNCF27Vw2OZgy4xEx0Fg=
1921
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
2022
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
2123
github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o=
@@ -37,6 +39,7 @@ github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfc
3739
github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
3840
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
3941
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
42+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
4043
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4144
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
4245
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
@@ -47,6 +50,8 @@ github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9
4750
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
4851
github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE=
4952
github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk=
53+
github.com/go-ole/go-ole v1.2.5 h1:t4MGB5xEDZvXI+0rMjjsfBsD7yAgp/s9ZDkL1JndXwY=
54+
github.com/go-ole/go-ole v1.2.5/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
5055
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
5156
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
5257
github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4=
@@ -134,6 +139,7 @@ github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FI
134139
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
135140
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
136141
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
142+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
137143
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
138144
github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI=
139145
github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
@@ -152,6 +158,8 @@ github.com/rs/cors v1.7.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU=
152158
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
153159
github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
154160
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
161+
github.com/shirou/gopsutil v3.21.4+incompatible h1:fuHcTm5mX+wzo542cmYcV9RTGQLbnHLI5SyQ5ryTVck=
162+
github.com/shirou/gopsutil v3.21.4+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
155163
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
156164
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
157165
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
@@ -170,8 +178,13 @@ github.com/spf13/viper v1.7.0/go.mod h1:8WkrPz2fc9jxqZNCJI/76HCieCp4Q8HaLFoCha5q
170178
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
171179
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
172180
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
181+
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
173182
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
174183
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
184+
github.com/tklauser/go-sysconf v0.3.5 h1:uu3Xl4nkLzQfXNsWn15rPc/HQCJKObbt1dKJeWp3vU4=
185+
github.com/tklauser/go-sysconf v0.3.5/go.mod h1:MkWzOF4RMCshBAMXuhXJs64Rte09mITnppBXY/rYEFI=
186+
github.com/tklauser/numcpus v0.2.2 h1:oyhllyrScuYI6g+h/zUvNXNp1wy7x8qQy3t/piefldA=
187+
github.com/tklauser/numcpus v0.2.2/go.mod h1:x3qojaO3uyYt0i56EW/VUYs7uBvdl2fkfZFu0T9wgjM=
175188
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
176189
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
177190
go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
@@ -238,6 +251,9 @@ golang.org/x/sys v0.0.0-20190502145724-3ef323f4f1fd/go.mod h1:h1NjWce9XRLGQEsW7w
238251
golang.org/x/sys v0.0.0-20190507160741-ecd444e8653b/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
239252
golang.org/x/sys v0.0.0-20190606165138-5da285871e9c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
240253
golang.org/x/sys v0.0.0-20190624142023-c5567b49c5d0/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
254+
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
255+
golang.org/x/sys v0.0.0-20210316164454-77fc1eacc6aa h1:ZYxPR6aca/uhfRJyaOAtflSHjJYiktO7QnJC5ut7iY4=
256+
golang.org/x/sys v0.0.0-20210316164454-77fc1eacc6aa/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
241257
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
242258
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
243259
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=

internal/build/build.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package build
2+
3+
import (
4+
"runtime/debug"
5+
)
6+
7+
// Version is dynamically set by the toolchain or overridden by the Makefile.
8+
var Version = "DEV"
9+
10+
// Date is dynamically set at build time in the Makefile.
11+
var Date = "" // YYYY-MM-DD
12+
13+
func init() {
14+
if Version == "DEV" {
15+
if info, ok := debug.ReadBuildInfo(); ok && info.Main.Version != "(devel)" {
16+
Version = info.Main.Version
17+
}
18+
}
19+
}

0 commit comments

Comments
 (0)