Skip to content

Commit

Permalink
feat: add logger
Browse files Browse the repository at this point in the history
  • Loading branch information
Jasonzyt committed Feb 7, 2024
1 parent fc0a286 commit 5c17b52
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 35 deletions.
36 changes: 36 additions & 0 deletions logger/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package logger

import (
"fmt"
)

const (
ColorRed = "\033[91m"
ColorGreen = "\033[92m"
ColorYellow = "\033[93m"
ColorBlue = "\033[94m"
ColorReset = "\033[0m"
)

func Log(a ...interface{}) {
fmt.Println(a...)
}

func Print(a ...interface{}) {
fmt.Print(a...)
}

// LogError logs an error message
func LogError(err error) {
fmt.Println(ColorRed+"ERROR:", err, ColorReset)
}

// LogSuccess logs a success message
func LogSuccess(message string) {
fmt.Println(ColorGreen + message + ColorReset)
}

// LogWarning logs a warning message
func LogWarning(message string) {
fmt.Println(ColorYellow + message + ColorReset)
}
46 changes: 21 additions & 25 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,10 @@ import (
"strings"

"github.com/akamensky/argparse"
"github.com/liteldev/bdsdown/logger"
"github.com/liteldev/bdsdown/utils"
)

const (
ColorRed = "\033[91m"
ColorGreen = "\033[92m"
ColorYellow = "\033[93m"
ColorBlue = "\033[94m"
ColorReset = "\033[0m"
)

func getDefaultCacheDir() string {
currentUser, err := user.Current()
if err != nil {
Expand Down Expand Up @@ -78,65 +71,68 @@ func main() {
if clearCache {
err := os.RemoveAll(cacheDir)
if err != nil {
fmt.Println(ColorRed+"ERROR:", err, ColorReset)
logger.LogError(err)
return
}
fmt.Println(ColorGreen + "Cache cleared." + ColorReset)
logger.LogSuccess("Cache cleared.")
return
}

fmt.Println("Before using this software, please read: ")
fmt.Println("- Minecraft End User License Agreement https://minecraft.net/terms")
fmt.Println("- Microsoft Privacy Policy https://go.microsoft.com/fwlink/?LinkId=521839")
logger.Log("Before using this software, please read: ")
logger.Log("- Minecraft End User License Agreement https://minecraft.net/terms")
logger.Log("- Microsoft Privacy Policy https://go.microsoft.com/fwlink/?LinkId=521839")
fmt.Print("Please enter y if you agree with the above terms: ")
var agree string
if skipAgree {
agree = "y"
fmt.Println(agree)
logger.Log(agree)
} else {
fmt.Scanln(&agree)
}
if agree != "y" {
fmt.Println(ColorYellow + "You must agree with the above terms to use this software." + ColorReset)
logger.LogWarning("You must agree to the terms to use this software.")
return
}
fmt.Println("=====================================================")
logger.Log("=====================================================")

if len(excludedFiles) > 0 {
fmt.Println("The following files will be excluded from installation: ", excludedFiles)
logger.Log("The following files will be excluded from installation: ", excludedFiles)
}

if usePreview {
fmt.Println(ColorYellow + "Using preview version." + ColorReset)
logger.LogWarning("Using preview version.")
}

if targetVersion != "" {
err := utils.Install()
if err != nil {
fmt.Println(ColorRed+"ERROR:", err, ColorReset)
logger.LogError(err)
return
}
fmt.Println(ColorGreen + "Install complete." + ColorReset)
logger.LogSuccess("Install complete.")
} else {
var version string
var err error
if usePreview {
version, err = utils.GetLatestPreviewVersion()
} else {
fmt.Println("No version specified, using latest release version.")
logger.Log("No version specified, using latest release version.")
version, err = utils.GetLatestReleaseVersion()
}
if err != nil {
fmt.Println(ColorRed+"ERROR:", err)
logger.LogError(err)
return
}
fmt.Println("Latest version: " + ColorBlue + version + ColorReset)
logger.Log("Latest version:", logger.ColorBlue+version+logger.ColorReset)
utils.SetTargetVersion(version)

err = utils.Install()
if err != nil {
fmt.Println(ColorRed+"ERROR:", err, ColorReset)
logger.LogError(err)
// TODO: Add rollback
// TODO: Add tips for common errors(clearing cache, etc.)
return
}
fmt.Println(ColorGreen + "Install complete." + ColorReset)
logger.LogSuccess("Install complete.")
}
}
21 changes: 11 additions & 10 deletions utils/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package utils
import (
"archive/zip"
"compress/flate"
"fmt"
"io"
"os"
"path"

"github.com/liteldev/bdsdown/logger"
"github.com/schollz/progressbar/v3"
)

Expand Down Expand Up @@ -84,25 +84,25 @@ func Install() error {
var err error

if useCache {
fmt.Println("Checking cache...")
logger.Log("Checking cache...")
path, err = CheckCache(version, cacheDir)
if err == nil {
fmt.Println(" Found cache!")
fmt.Println("Unziping cached files...")
logger.Log(" Found cache!")
logger.Log("Unziping cached files...")
goto Unzip
} else {
fmt.Println(" Cache not found.")
logger.Log(" Cache not found.")
}
}

fmt.Println("Downloading BDS v" + version + "...")
logger.Log("Downloading BDS v" + version + "...")
path, err = DownloadVersion(version, usePreview)
if err != nil {
return err
}
fmt.Println(" Download complete!")
logger.Log(" Download complete!")

fmt.Println("Unziping downloaded files...")
logger.Log("Unziping downloaded files...")

Unzip:
file, err := os.OpenFile(path, os.O_RDONLY, 0)
Expand All @@ -125,15 +125,16 @@ Unzip:
}))

Unzip(file, bar)
fmt.Println(" Unzip complete!")
logger.Log(" Unzip complete!")

file.Close()
if !useCache {
fmt.Println("Cleaning up...")
logger.Log("Cleaning up...")
err = os.Remove(path)
if err != nil {
return err
}
logger.Log(" Clean up complete!")
}

return nil
Expand Down

0 comments on commit 5c17b52

Please sign in to comment.