Skip to content
This repository has been archived by the owner on Sep 9, 2022. It is now read-only.

Commit

Permalink
fix api spinner suffix, update and fix secman doctor commands
Browse files Browse the repository at this point in the history
  • Loading branch information
abdfnx committed Mar 12, 2022
1 parent 48d2377 commit 8a7aff6
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 74 deletions.
4 changes: 2 additions & 2 deletions api/get-latest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
50 changes: 41 additions & 9 deletions pkg/doctor/doctor.go
Original file line number Diff line number Diff line change
@@ -1,34 +1,37 @@
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()))
)

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 {
Expand All @@ -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."
Expand All @@ -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))
}
}
}
134 changes: 71 additions & 63 deletions pkg/doctor/fix.go
Original file line number Diff line number Diff line change
@@ -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"
)
Expand All @@ -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))
}

0 comments on commit 8a7aff6

Please sign in to comment.