Skip to content

Commit

Permalink
chore: refactor logger
Browse files Browse the repository at this point in the history
  • Loading branch information
jozefcipa committed May 8, 2024
1 parent ca834a4 commit 8a2ae5f
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 21 deletions.
4 changes: 2 additions & 2 deletions cmd/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ defined in the novus.yml configuration file.
`,
Run: func(cmd *cobra.Command, args []string) {
nginx.Stop()
logger.Messagef("🚫 Nginx stopped.\n")
logger.Infof("🚫 Nginx stopped.")

dnsmasq.Stop()
logger.Messagef("🚫 DNSMasq stopped.\n")
logger.Infof("🚫 DNSMasq stopped.")
},
}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ go 1.21.0

require (
github.com/arsham/figurine v1.3.0
github.com/fatih/color v1.16.0
github.com/go-playground/validator/v10 v10.18.0
github.com/ivanpirog/coloredcobra v1.0.1
github.com/jedib0t/go-pretty/v6 v6.5.8
Expand All @@ -15,6 +14,7 @@ require (
require (
github.com/arsham/rainbow v1.2.1 // indirect
github.com/common-nighthawk/go-figure v0.0.0-20210622060536-734e95fb86be // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
Expand Down
3 changes: 1 addition & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46t
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w=
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE=
github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0=
github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk=
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
Expand Down
2 changes: 1 addition & 1 deletion internal/brew/brew.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func IsSudoServiceRunning(svc string) bool {
}

func brewInstall(bin string) {
logger.Messagef("⏳ Installing %s...\n", bin)
logger.Infof("⏳ Installing %s...", bin)
logger.Debugf("Running \"brew install %s\"", bin)

cmd := exec.Command("brew", "install", bin)
Expand Down
38 changes: 27 additions & 11 deletions internal/logger/logger.go
Original file line number Diff line number Diff line change
@@ -1,34 +1,50 @@
package logger

import (
"github.com/fatih/color"
import "fmt"

// Bash Color codes
// https://stackoverflow.com/a/69648792/4480179
const (
RED string = "\033[38;5;196m"
GREEN string = "\033[38;5;046m"
CYAN string = "\033[38;5;014m"
ORANGE string = "\033[38;5;202m"
YELLOW string = "\033[38;5;011m"
MAGENTA string = "\033[38;5;201m"
GRAY string = "\033[38;5;245m"
WHITE string = "\033[38;5;255m"
RESET string = "\033[0m"
)

var Messagef = color.New(color.FgCyan).PrintfFunc()
func Infof(format string, a ...interface{}) {
fmt.Printf(WHITE+format+RESET+"\n", a...)
}

var Warnf = color.New(color.FgYellow).PrintfFunc()
func Warnf(format string, a ...interface{}) {
fmt.Printf(YELLOW+"⚠️ "+format+RESET+"\n", a...)
}

func Checkf(format string, a ...interface{}) {
Messagef("✔ "+format+"\n", a...)
fmt.Printf(GREEN+"✔ "+GRAY+format+RESET+"\n", a...)
}

func Hintf(format string, a ...interface{}) {
Warnf("💡 "+format+"\n", a...)
func Successf(format string, a ...interface{}) {
fmt.Printf(GREEN+"✅ "+format+RESET+"\n", a...)
}

func Successf(format string, a ...interface{}) {
color.New(color.FgHiGreen).PrintfFunc()("✅ "+format+"\n", a...)
func Hintf(format string, a ...interface{}) {
fmt.Printf(ORANGE+"💡 "+format+RESET+"\n", a...)
}

func Errorf(format string, a ...interface{}) {
color.New(color.FgRed).PrintfFunc()("❌ "+format+"\n", a...)
fmt.Printf(RED+"❌ "+format+RESET+"\n", a...)
}

// This variable gets its value in cmd/root.go from the CLI flag
var DebugEnabled bool

func Debugf(format string, a ...interface{}) {
if DebugEnabled {
color.New(color.FgMagenta).Printf("[DEBUG] "+format+"\n", a...)
fmt.Printf(MAGENTA+"[DEBUG] "+format+RESET+"\n", a...)
}
}
2 changes: 1 addition & 1 deletion internal/novus/sudoers.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func CreateSudoersFile() {
brewBinPath,
)

logger.Messagef("⏳ Creating /etc/sudoers.d file for Novus.\n")
logger.Infof("⏳ Creating /etc/sudoers.d file for Novus.")
fs.WriteFileWithSudoOrExit(sudoersFile, sudoPermissions)

// sudoers file must be owned by root
Expand Down
4 changes: 1 addition & 3 deletions internal/tui/tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package tui
import (
"bufio"
"fmt"
"log"
"os"

"github.com/jedib0t/go-pretty/v6/table"
Expand All @@ -13,14 +12,13 @@ import (
)

func AskUser(prompt string) string {
logger.Messagef(prompt)
logger.Infof(prompt)

scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()

err := scanner.Err()
if err != nil {
log.Fatal(err)
logger.Errorf("Failed to read from CLI: %v", err)
os.Exit(1)
}
Expand Down

0 comments on commit 8a2ae5f

Please sign in to comment.