From 8a7aff62bd8accdcea4a5a0d4af3e2f61faa7419 Mon Sep 17 00:00:00 2001 From: abdfnx Date: Sat, 12 Mar 2022 23:14:53 +0300 Subject: [PATCH] fix api spinner suffix, update and fix `secman doctor` commands --- api/get-latest.go | 4 +- pkg/doctor/doctor.go | 50 +++++++++++++--- pkg/doctor/fix.go | 134 +++++++++++++++++++++++-------------------- 3 files changed, 114 insertions(+), 74 deletions(-) diff --git a/api/get-latest.go b/api/get-latest.go index b510843f..bdbd47a0 100644 --- a/api/get-latest.go +++ b/api/get-latest.go @@ -31,10 +31,10 @@ func GetLatest(product string, isChecker bool) string { os.Exit(0) } - suffix := " 🔍 Checking for updates..." + suffix := " 🔍 Requesting..." if isChecker { - suffix = " 🔍 Requesting..." + suffix = " 🔍 Checking for updates..." } s := spinner.New(spinner.CharSets[11], 100*time.Millisecond) diff --git a/pkg/doctor/doctor.go b/pkg/doctor/doctor.go index b9addaac..9e55a6cc 100644 --- a/pkg/doctor/doctor.go +++ b/pkg/doctor/doctor.go @@ -1,27 +1,27 @@ package doctor import ( - "os" - "fmt" "bytes" + "fmt" + "os" + "strings" "github.com/abdfnx/gosh" - "github.com/spf13/viper" "github.com/abdfnx/looker" - "github.com/scmn-dev/secman/api" "github.com/charmbracelet/lipgloss" + "github.com/scmn-dev/secman/api" "github.com/scmn-dev/secman/constants" + "github.com/spf13/viper" ) var ( _, err = looker.LookPath("scc") + status = "" smVersionStatus = "" sccStatus = "" sccVersionStatus = "" secmanConfigStatus = "" - latestVersion = api.GetLatest("secman-cli", false) - latestSCCVersion = api.GetLatest("scc", false) outErr, out, errout = gosh.RunOutput("scc -v") configErr = viper.ReadConfig(bytes.NewBuffer(constants.SecmanConfig())) ) @@ -29,6 +29,9 @@ var ( func Doctor(buildVersion string) { if len(os.Args) > 1 { if (os.Args[1] == "doctor" || os.Args[1] == "check") { + latestVersion := api.GetLatest("secman-cli", false) + latestSCCVersion := api.GetLatest("scc", false) + if err == nil { sccStatus = constants.Checkmark + "secman core cli is installed." } else { @@ -46,8 +49,9 @@ func Doctor(buildVersion string) { } if outErr != nil { - fmt.Println(errout) - os.Exit(0) + if strings.Contains(errout, "not") { + sccVersionStatus = "" + } } else { if latestSCCVersion == out { sccVersionStatus = constants.Checkmark + "secman core cli on the latest version." @@ -64,7 +68,35 @@ func Doctor(buildVersion string) { secmanConfigStatus = constants.X + "secman config is not found." } - fmt.Println(lipgloss.NewStyle().PaddingLeft(2).SetString(constants.Logo("Secman Doctor") + "\n\n" + smVersionStatus + "\n" + sccStatus + "\n" + sccVersionStatus + "\n" + secmanConfigStatus)) + if smVersionStatus != "" { + status += smVersionStatus + } + + if sccStatus != "" { + if status != "" { + status += "\n" + sccStatus + } else { + status += sccStatus + } + } + + if sccVersionStatus != "" { + if status != "" { + status += "\n" + sccVersionStatus + } else { + status += sccVersionStatus + } + } + + if secmanConfigStatus != "" { + if status != "" { + status += "\n" + secmanConfigStatus + } else { + status += secmanConfigStatus + } + } + + fmt.Println(lipgloss.NewStyle().PaddingLeft(2).SetString(constants.Logo("Secman Doctor") + "\n\n" + status)) } } } diff --git a/pkg/doctor/fix.go b/pkg/doctor/fix.go index 98fe05e4..e6a90891 100644 --- a/pkg/doctor/fix.go +++ b/pkg/doctor/fix.go @@ -1,9 +1,11 @@ package doctor import ( + "os" "fmt" "github.com/abdfnx/gosh" + "github.com/scmn-dev/secman/api" "github.com/charmbracelet/lipgloss" "github.com/scmn-dev/secman/constants" ) @@ -13,69 +15,75 @@ func CommandStyle(cmd string) string { } func Fix(buildVersion string) { - var ( - bug1 string - bug2 string - bug3 string - bug4 string - ) - - _, out, _ := gosh.RunOutput("scc -v") - if out != "" { - out = out[:len(out)-1] - } - - if buildVersion != latestVersion { - bug1 = "to upgrade run " + CommandStyle("`secman upgrade`") + " to download the latest version of secman." - } - - if err != nil { - bug2 = "to install secman core cli run " + CommandStyle("`npm i -g @secman/scc`") - } - - if err == nil { - if latestSCCVersion != out { - bug3 = "to upgrade secman core cli to the latest version run " + CommandStyle("`npm update -g @secman/scc`") + if len(os.Args) > 1 { + if (os.Args[2] == "fix") { + var ( + bug1 string + bug2 string + bug3 string + bug4 string + latestVersion = api.GetLatest("secman-cli", false) + latestSCCVersion = api.GetLatest("scc", false) + ) + + _, out, _ := gosh.RunOutput("scc -v") + if out != "" { + out = out[:len(out)-1] + } + + if buildVersion != latestVersion { + bug1 = "to upgrade run " + CommandStyle("`secman upgrade`") + " to download the latest version of secman." + } + + if err != nil { + bug2 = "to install secman core cli run " + CommandStyle("`npm i -g @secman/scc`") + } + + if err == nil { + if latestSCCVersion != out { + bug3 = "to upgrade secman core cli to the latest version run " + CommandStyle("`npm update -g @secman/scc`") + } + } + + if configErr != nil { + bug4 = "to initialize secman config run " + CommandStyle("`secman init`") + } + + fixSteps := "" + + if bug1 != "" { + fixSteps += bug1 + } + + if bug2 != "" { + if fixSteps != "" { + fixSteps += "\n" + bug2 + } else { + fixSteps += bug2 + } + } + + if bug3 != "" { + if fixSteps != "" { + fixSteps += "\n" + bug3 + } else { + fixSteps += bug3 + } + } + + if bug4 != "" { + if fixSteps != "" { + fixSteps += "\n" + bug4 + } else { + fixSteps += bug4 + } + } + + if fixSteps == "" { + fixSteps = "there's nothing to fix, everything is good" + } + + fmt.Println(lipgloss.NewStyle().PaddingLeft(2).SetString(constants.Logo("Secman Doctor") + "\n\n" + fixSteps)) } } - - if configErr != nil { - bug4 = "to initialize secman config run " + CommandStyle("`secman init`") - } - - fixSteps := "" - - if bug1 != "" { - fixSteps += bug1 - } - - if bug2 != "" { - if fixSteps != "" { - fixSteps += "\n" + bug2 - } else { - fixSteps += bug2 - } - } - - if bug3 != "" { - if fixSteps != "" { - fixSteps += "\n" + bug3 - } else { - fixSteps += bug3 - } - } - - if bug4 != "" { - if fixSteps != "" { - fixSteps += "\n" + bug4 - } else { - fixSteps += bug4 - } - } - - if fixSteps == "" { - fixSteps = "there's nothing to fix, everything is good" - } - - fmt.Println(lipgloss.NewStyle().PaddingLeft(2).SetString(constants.Logo("Secman Doctor") + "\n\n" + fixSteps)) }