@@ -2,7 +2,7 @@ package cli
22
33// ////////////////////////////////////////////////////////////////////////////////// //
44// //
5- // Copyright (c) 2022 ESSENTIAL KAOS //
5+ // Copyright (c) 2023 ESSENTIAL KAOS //
66// Apache License, Version 2.0 <http://www.apache.org/licenses/LICENSE-2.0> //
77// //
88// ////////////////////////////////////////////////////////////////////////////////// //
@@ -30,13 +30,15 @@ import (
3030 "github.com/essentialkaos/ek/v12/usage/update"
3131
3232 "github.com/essentialkaos/sslscan/v13"
33+
34+ "github.com/essentialkaos/sslcli/cli/support"
3335)
3436
3537// ////////////////////////////////////////////////////////////////////////////////// //
3638
3739const (
3840 APP = "SSLScan Client"
39- VER = "2.7.3 "
41+ VER = "2.7.4 "
4042 DESC = "Command-line client for the SSL Labs API"
4143)
4244
@@ -54,6 +56,7 @@ const (
5456 OPT_HELP = "h:help"
5557 OPT_VER = "v:version"
5658
59+ OPT_VERB_VER = "vv:verbose-version"
5760 OPT_COMPLETION = "completion"
5861 OPT_GENERATE_MAN = "generate-man"
5962)
@@ -100,9 +103,10 @@ var optMap = options.Map{
100103 OPT_QUIET : {Type : options .BOOL },
101104 OPT_NOTIFY : {Type : options .BOOL },
102105 OPT_NO_COLOR : {Type : options .BOOL },
103- OPT_HELP : {Type : options .BOOL , Alias : "u:usage" },
104- OPT_VER : {Type : options .BOOL , Alias : "ver" },
106+ OPT_HELP : {Type : options .BOOL },
107+ OPT_VER : {Type : options .BOOL },
105108
109+ OPT_VERB_VER : {Type : options .BOOL },
106110 OPT_COMPLETION : {},
107111 OPT_GENERATE_MAN : {Type : options .BOOL },
108112}
@@ -127,44 +131,64 @@ var serverMessageShown bool
127131
128132// ////////////////////////////////////////////////////////////////////////////////// //
129133
130- // Init starts initialization rutine
131- func Init ( ) {
132- args , errs := options . Parse ( optMap )
134+ // Run is main function
135+ func Run ( gitRev string , gomod [] byte ) {
136+ runtime . GOMAXPROCS ( 2 )
133137
134- if len (errs ) != 0 {
135- printError ("Arguments parsing errors:" )
138+ preConfigureUI ()
136139
137- for _ , err := range errs {
138- printError (" %v" , err )
139- }
140+ args , errs := options .Parse (optMap )
140141
142+ if len (errs ) != 0 {
143+ printError (errs [0 ].Error ())
141144 os .Exit (1 )
142145 }
143146
144- if options .Has (OPT_COMPLETION ) {
145- os .Exit (genCompletion ())
146- }
147-
148- if options .Has (OPT_GENERATE_MAN ) {
149- os .Exit (genMan ())
150- }
151-
152147 configureUI ()
153148 prepare ()
154149
155- if options .GetB (OPT_VER ) {
156- showAbout ()
157- return
150+ switch {
151+ case options .Has (OPT_COMPLETION ):
152+ os .Exit (printCompletion ())
153+ case options .Has (OPT_GENERATE_MAN ):
154+ printMan ()
155+ os .Exit (0 )
156+ case options .GetB (OPT_VER ):
157+ genAbout (gitRev ).Print ()
158+ os .Exit (0 )
159+ case options .GetB (OPT_VERB_VER ):
160+ support .Print (APP , VER , gitRev , gomod )
161+ os .Exit (0 )
162+ case options .GetB (OPT_HELP ) || len (args ) == 0 :
163+ genUsage ().Print ()
164+ os .Exit (0 )
158165 }
159166
160- if options .GetB (OPT_HELP ) || len (args ) == 0 {
161- showUsage ()
162- return
167+ process (args )
168+ }
169+
170+ // preConfigureUI preconfigures UI based on information about user terminal
171+ func preConfigureUI () {
172+ term := os .Getenv ("TERM" )
173+
174+ fmtc .DisableColors = true
175+
176+ if term != "" {
177+ switch {
178+ case strings .Contains (term , "xterm" ),
179+ strings .Contains (term , "color" ),
180+ term == "screen" :
181+ fmtc .DisableColors = false
182+ }
163183 }
164184
165- runtime .GOMAXPROCS (2 )
185+ if ! fsutil .IsCharacterDevice ("/dev/stdout" ) && os .Getenv ("FAKETTY" ) == "" {
186+ fmtc .DisableColors = true
187+ }
166188
167- process (args )
189+ if os .Getenv ("NO_COLOR" ) != "" {
190+ fmtc .DisableColors = true
191+ }
168192}
169193
170194// configureUI configures user interface
@@ -594,30 +618,8 @@ func printError(f string, a ...interface{}) {
594618
595619// ////////////////////////////////////////////////////////////////////////////////// //
596620
597- // showUsage prints usage info
598- func showUsage () {
599- genUsage ().Render ()
600- }
601-
602- // showAbout prints info about version
603- func showAbout () {
604- genAbout ().Render ()
605- }
606-
607- // genMan generates man page
608- func genMan () int {
609- fmt .Println (
610- man .Generate (
611- genUsage (),
612- genAbout (),
613- ),
614- )
615-
616- return 0
617- }
618-
619- // genCompletion generates completion for different shells
620- func genCompletion () int {
621+ // printCompletion prints completion for given shell
622+ func printCompletion () int {
621623 info := genUsage ()
622624
623625 switch options .GetS (OPT_COMPLETION ) {
@@ -634,6 +636,16 @@ func genCompletion() int {
634636 return 0
635637}
636638
639+ // printMan prints man page
640+ func printMan () {
641+ fmt .Println (
642+ man .Generate (
643+ genUsage (),
644+ genAbout ("" ),
645+ ),
646+ )
647+ }
648+
637649// genUsage generates usage info
638650func genUsage () * usage.Info {
639651 info := usage .NewInfo ("" , "host…" )
@@ -661,7 +673,7 @@ func genUsage() *usage.Info {
661673}
662674
663675// genAbout generates info about version
664- func genAbout () * usage.About {
676+ func genAbout (gitRev string ) * usage.About {
665677 about := & usage.About {
666678 App : APP ,
667679 Version : VER ,
@@ -672,5 +684,9 @@ func genAbout() *usage.About {
672684 UpdateChecker : usage.UpdateChecker {"essentialkaos/sslcli" , update .GitHubChecker },
673685 }
674686
687+ if gitRev != "" {
688+ about .Build = "git:" + gitRev
689+ }
690+
675691 return about
676692}
0 commit comments