mirror of
https://github.com/CompeyDev/wsl-archlinux-manager.git
synced 2024-12-12 04:30:37 +00:00
refactor: reduce boilerplate logging code using shared logger
This commit is contained in:
parent
b0a57eed59
commit
87d7112f26
4 changed files with 51 additions and 34 deletions
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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(`></span> %s</h5>`, country))[1], `<ul>`)[1], `<li><a href="`)[1], `"`)[0]
|
||||
|
@ -116,10 +116,10 @@ func pullArchive(url string) (isSuccessful bool, error error) {
|
|||
res, err := grab.Get(".", structuredUrl)
|
||||
|
||||
if err != nil {
|
||||
color.Red.Println("\r ❎ Failed to download RootFS.")
|
||||
logger.Error("Failed to download RootFS.")
|
||||
return false, err
|
||||
}
|
||||
|
||||
color.Bold.Println("\r ✅ Downloaded RootFS", res.Filename)
|
||||
logger.Info(fmt.Sprintf("Downloaded RootFS %s", res.Filename))
|
||||
return true, nil
|
||||
}
|
||||
|
|
24
src/main.go
24
src/main.go
|
@ -11,6 +11,7 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/CompeyDev/wsl-archlinux-manager/lib/core"
|
||||
logger "github.com/CompeyDev/wsl-archlinux-manager/util"
|
||||
"github.com/briandowns/spinner"
|
||||
"github.com/gookit/color"
|
||||
)
|
||||
|
@ -25,12 +26,6 @@ func main() {
|
|||
}
|
||||
}
|
||||
|
||||
func build() {
|
||||
checks()
|
||||
|
||||
}
|
||||
|
||||
// TODO: Add colors
|
||||
func checks() {
|
||||
color.Blueln("======> Pre-installation checks")
|
||||
fmt.Println("🔃 Running checks...")
|
||||
|
@ -49,11 +44,11 @@ func checks() {
|
|||
getAvailability, availabilityErr := exec.Command("powershell.exe", "(Get-WindowsOptionalFeature -Online -FeatureName *Subsystem*).State").Output()
|
||||
|
||||
if availabilityErr != nil {
|
||||
color.Red.Println("\r ❎ Failed to check for WSL availability.")
|
||||
logger.Error("Failed to check for WSL availability.")
|
||||
}
|
||||
|
||||
if strings.Trim(string(getAvailability), "\n\r") == "Enabled" {
|
||||
color.Green.Println("\r ✅ WSL is enabled.")
|
||||
logger.Info("WSL is enabled.")
|
||||
bar.Stop()
|
||||
|
||||
bar.Prefix = " "
|
||||
|
@ -64,12 +59,12 @@ func checks() {
|
|||
preInstalledDistro, installedDistroErr := exec.Command("powershell.exe", "wsl").Output()
|
||||
|
||||
if installedDistroErr != nil {
|
||||
color.Red.Println("\r ❎ Failed to check for preinstalled distributions.")
|
||||
logger.Error("Failed to check for preinstalled distributions.")
|
||||
bar.Stop()
|
||||
}
|
||||
|
||||
if strings.Contains(string(preInstalledDistro), "no installed distributions") {
|
||||
color.Red.Println("\r ❎ Preinstalled distributions do exist. (Please make sure the default distribution is Debian-based)")
|
||||
logger.Error("Preinstalled distributions do not exist. (Please make sure the default WSL distribution is Debian-based)")
|
||||
bar.Stop()
|
||||
}
|
||||
|
||||
|
@ -79,10 +74,9 @@ func checks() {
|
|||
|
||||
bar.Suffix = " Creating install directory..."
|
||||
bar.Start()
|
||||
time.Sleep(300000000)
|
||||
time.Sleep(1 * time.Second)
|
||||
if homeDirErr != nil {
|
||||
fmt.Println("❎ Failed to initialize installation directory.")
|
||||
os.Exit(1)
|
||||
logger.Error("Failed to initialize installation directory.")
|
||||
}
|
||||
|
||||
installDir := path.Join(userHomeDir, ".wslm")
|
||||
|
@ -91,7 +85,7 @@ func checks() {
|
|||
os.Mkdir(installDir, fs.FileMode(os.O_RDWR))
|
||||
os.Mkdir(archDir, fs.FileMode(os.O_RDWR))
|
||||
bar.Stop()
|
||||
color.Green.Print("\r ✅ Successfully initialized installation directory.")
|
||||
color.Bold.Println("\n✅ Initialized WSLm.")
|
||||
logger.Info("Successfully initialized installation directory.")
|
||||
logger.Progress("Initialized WSLm.")
|
||||
color.Blueln("===============================")
|
||||
}
|
||||
|
|
24
util/logger.go
Normal file
24
util/logger.go
Normal file
|
@ -0,0 +1,24 @@
|
|||
package logger
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"github.com/gookit/color"
|
||||
)
|
||||
|
||||
func Error(log string) {
|
||||
color.Red.Println("\r ❎ ", log)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
func Warn(log string) {
|
||||
color.Yellow.Println("\r ❎ ", log)
|
||||
}
|
||||
|
||||
func Info(log string) {
|
||||
color.Green.Println("\r ✅ ", log)
|
||||
}
|
||||
|
||||
func Progress(log string) {
|
||||
color.Bold.Println("\n✅ ", log)
|
||||
}
|
Loading…
Reference in a new issue