From 87d7112f26405550fbc8a26ea56e444eacc8d4f7 Mon Sep 17 00:00:00 2001 From: Compey Date: Fri, 25 Nov 2022 20:56:59 +0530 Subject: [PATCH] refactor: reduce boilerplate logging code using shared logger --- lib/checkers/verifySignature.go | 17 ++++++++--------- lib/core/core.go | 20 ++++++++++---------- src/main.go | 24 +++++++++--------------- util/logger.go | 24 ++++++++++++++++++++++++ 4 files changed, 51 insertions(+), 34 deletions(-) create mode 100644 util/logger.go diff --git a/lib/checkers/verifySignature.go b/lib/checkers/verifySignature.go index f2e725b..3243620 100644 --- a/lib/checkers/verifySignature.go +++ b/lib/checkers/verifySignature.go @@ -7,12 +7,11 @@ import ( "strings" "time" + logger "github.com/CompeyDev/wsl-archlinux-manager/util" "github.com/briandowns/spinner" "github.com/cavaliergopher/grab/v3" - "github.com/gookit/color" ) -// TODO: func VerifySignature(mirrorUrl string) { bar := spinner.New(spinner.CharSets[14], 100*time.Millisecond) bar.Prefix = " " @@ -22,20 +21,20 @@ func VerifySignature(mirrorUrl string) { success, _ := pullSig(mirrorUrl) if !success { - color.Red.Println("\r ❎ Failed to download signature of RootFS. Refusing to continue.") + logger.Error("Failed to download signature of RootFS. Refusing to continue.") os.Exit(1) } userHomeDir, homeDirErr := os.UserHomeDir() if homeDirErr != nil { - color.Red.Println("\r ❎ Failed to fetch installation directory, cannot verify authenticity of RootFS.") + logger.Error("Failed to fetch installation directory, cannot verify authenticity of RootFS.") os.Exit(1) } cwd, dirErr := os.Getwd() if dirErr != nil { - color.Red.Println("\r ❎ Failed to fetch installation directory, cannot verify authenticity of RootFS.") + logger.Error("Failed to fetch installation directory, cannot verify authenticity of RootFS.") os.Exit(1) } @@ -46,11 +45,11 @@ func VerifySignature(mirrorUrl string) { getAuthenticity, authenticityErr := exec.Command("powershell.exe", fmt.Sprintf(`wsl bash -c "cd %s && gpg --keyserver-options auto-key-retrieve --verify archlinux-bootstrap-2022.11.01-x86_64.tar.gz.sig"`, unixWd)).Output() if authenticityErr != nil { - color.Red.Println("\r ❎ Failed to verify authenticity of RootFS. Refusing to continue.") + logger.Error("Failed to verify authenticity of RootFS. Refusing to continue.") os.Exit(1) } - color.Green.Println("\r ✅ Successfully matched checksums and verified authenticity!") + logger.Info("Successfully matched checksums and verified authenticity!") bar.Stop() fmt.Println(strings.Trim(string(getAuthenticity), "\n\r")) @@ -66,11 +65,11 @@ func pullSig(url string) (isSuccessful bool, error error) { res, err := grab.Get(".", structuredUrl) if err != nil { - color.Red.Println("\r ❎ Failed to download Signature.") + logger.Error("Failed to download Signature.") return false, err } - color.Green.Println("\r ✅ Downloaded Signature", res.Filename) + logger.Info(fmt.Sprintf("Downloaded Signature %s", res.Filename)) return true, nil } diff --git a/lib/core/core.go b/lib/core/core.go index 23190e5..8d86271 100644 --- a/lib/core/core.go +++ b/lib/core/core.go @@ -11,9 +11,9 @@ import ( "encoding/json" "github.com/CompeyDev/wsl-archlinux-manager/lib/checkers" + logger "github.com/CompeyDev/wsl-archlinux-manager/util" "github.com/briandowns/spinner" "github.com/cavaliergopher/grab/v3" - "github.com/gookit/color" ) // TODO: @@ -32,7 +32,7 @@ func Build() { body, reqErr := io.ReadAll(userLocation.Body) if reqErr != nil { - color.Red.Println("\r ❎ An internal error occurred when attempting to pull the RootFS. This is probably a bug; you might want to report this.") + logger.Error("An internal error occurred when attempting to pull the RootFS. This is probably a bug; you might want to report this.") bar.Stop() os.Exit(1) } @@ -57,7 +57,7 @@ func Build() { parseErr := json.Unmarshal([]byte(body), &resStruct) if parseErr != nil { - color.Red.Println("\r ❎ Failed to parse response body! This is a probably a bug; you might want to report this.") + logger.Error("Failed to parse response body! This is a probably a bug; you might want to report this.") bar.Stop() os.Exit(1) } @@ -68,9 +68,9 @@ func Build() { isSuccessful_1, _ := pullArchive(mirror) if !isSuccessful_1 { - color.Yellow.Println("\r ❎ Attempt #1 to pull RootFS failed, trying again with Worldwide...") + logger.Warn("Attempt #1 to pull RootFS failed, trying again with Worldwide mirror...") - bar.Suffix = " Attempt #1 to pull RootFS failed, trying again with Worldwide..." + bar.Suffix = " Attempt #1 to pull RootFS failed, trying again with Worldwide mirror..." bar.Start() @@ -78,7 +78,7 @@ func Build() { isSuccessful_2, _ := pullArchive(globalMirror) if !isSuccessful_2 { - color.Red.Println("\r ❎ Attempt #2 to pull RootFS failed. Please try again.") + logger.Error("Attempt #2 to pull RootFS failed. Please try again.") bar.Stop() os.Exit(1) } else { @@ -94,12 +94,12 @@ func Build() { func getMirror(country string) string { resp, err := http.Get("https://archlinux.org/download/") if err != nil { - color.Red.Println("❎ Failed to download RootFS.") + logger.Error("Failed to download RootFS.") } defer resp.Body.Close() body, err := io.ReadAll(resp.Body) if err != nil { - color.Red.Println("❎ An internal error occurred when attempting to pull the RootFS. This is probably a bug; you might want to report this.") + logger.Error("An internal error occurred when attempting to pull the RootFS. This is probably a bug; you might want to report this.") } mirrorLink := strings.Split(strings.Split(strings.Split(strings.Split(strings.Split(strings.Split(string(body), fmt.Sprintf(`title="%s"`, country))[1], `title="Download from`)[0], fmt.Sprintf(`> %s`, country))[1], `