Skip to content

Commit

Permalink
feat: add verbose logging
Browse files Browse the repository at this point in the history
- I still need to figure out the best places to add verbose logging but it's in place
- the once place I've consistently add is letting the user know where the configuration is loaded from when retrieving information which appears to be helpful and informative
- not publishing this yet
  • Loading branch information
stcrestrada committed May 28, 2024
1 parent 4bcdbd6 commit f02b01f
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 2 deletions.
1 change: 1 addition & 0 deletions cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,6 @@ func (cmd *AddCommand) LoadConfiguration() (*types.GeeContext, error) {
if err != nil {
return nil, err
}
util.VerboseLog("loaded gee.toml configuration from %s", cwd)
return util.NewConfigHelper().LoadConfig(cwd)
}
1 change: 1 addition & 0 deletions cmd/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,5 +123,6 @@ func (cmd *CloneCommand) LoadConfiguration() (*types.GeeContext, error) {
if err != nil {
return nil, err
}
util.VerboseLog("loaded gee.toml configuration from %s", cwd)
return util.NewConfigHelper().LoadConfig(cwd)
}
4 changes: 3 additions & 1 deletion cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (cmd *InitCommand) Run(c *cli.Context) error {
util.Warning("Warning: %s \n", err)
return err
}

util.VerboseLog("initializing gee.toml in %s", cwd)
err = cmd.RepoUtils.GeeCreate(cwd)
if err != nil {
return err
Expand All @@ -48,6 +48,7 @@ func (cmd *InitCommand) Run(c *cli.Context) error {

// insert dummy data into gee.toml
geeCtx := cmd.RepoUtils.NewDummyGeeContext(cwd)
util.VerboseLog("adding dummy data to gee.toml")
err = cmd.RepoUtils.InsertConfigIntoGeeToml(geeCtx)
if err != nil {
return err
Expand All @@ -64,5 +65,6 @@ func (cmd *InitCommand) LoadConfiguration() (*types.GeeContext, error) {
if err != nil {
return nil, err
}
util.VerboseLog("loaded gee.toml configuration from %s", cwd)
return util.NewConfigHelper().LoadConfig(cwd)
}
3 changes: 3 additions & 0 deletions cmd/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func (cmd *PullCommand) Run(c *cli.Context) error {
repo := repos[i]
state := states[i]
return func() (interface{}, error) {
util.VerboseLog("pulling %s", repo.Name)
fullPath := cmd.RepoUtils.FullPathWithRepo(repo.Path, repo.Name)
err = cmd.RepoUtils.GetOrCreateDir(repo.Path)
if err != nil {
Expand All @@ -76,6 +77,7 @@ func (cmd *PullCommand) Run(c *cli.Context) error {
cmd.Git.Pull(repo.Name, fullPath, rc, func(onFinish *types.CommandOnFinish) {
cmd.RepoUtils.HandlePullFinish(&repo, onFinish, state)
commandOnFinish[i] = onFinish
util.VerboseLog("completed pulling %s", repo.Name)
})
return nil, nil
}
Expand Down Expand Up @@ -109,5 +111,6 @@ func (cmd *PullCommand) LoadConfiguration() (*types.GeeContext, error) {
if err != nil {
return nil, err
}
util.VerboseLog("loaded gee.toml configuration from %s", cwd)
return util.NewConfigHelper().LoadConfig(cwd)
}
2 changes: 2 additions & 0 deletions cmd/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func (cmd *RemoveCommand) Run(c *cli.Context) error {
return util.NewWarning("please specify the repository name to remove")
}
}
util.VerboseLog("removing repository %s", repoName)

// Load the configuration
geeCtx, err := cmd.LoadConfiguration()
Expand Down Expand Up @@ -98,6 +99,7 @@ func (cmd *RemoveCommand) LoadConfiguration() (*types.GeeContext, error) {
if err != nil {
return nil, err
}
util.VerboseLog("loaded gee.toml configuration from %s", cwd)
return util.NewConfigHelper().LoadConfig(cwd)
}

Expand Down
1 change: 1 addition & 0 deletions cmd/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,5 +114,6 @@ func (cmd *StatusCommand) LoadConfiguration() (*types.GeeContext, error) {
if err != nil {
return nil, err
}
util.VerboseLog("loaded gee.toml configuration from %s", cwd)
return util.NewConfigHelper().LoadConfig(cwd)
}
16 changes: 16 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,22 @@ var (
var validate *validator.Validate

func main() {
app.Flags = []cli.Flag{
&cli.BoolFlag{
Name: "verbose",
Usage: "Enable verbose logging",
},
}

app.Before = func(c *cli.Context) error {
verbose := c.Bool("verbose")
util.SetVerbose(verbose)
if verbose {
util.VerboseLog("Verbose logging enabled")
}
return nil
}

app.Commands = []*cli.Command{
cmd.InitCmd(),
cmd.AddCmd(),
Expand Down
16 changes: 15 additions & 1 deletion pkg/util/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ import (
"github.com/fatih/color"
)

var Verbose bool

// SetVerbose sets the logging verbosity
func SetVerbose(verbose bool) {
Verbose = verbose
}

// WarningError represents a custom warning error with a message
type WarningError struct {
Message string
Expand Down Expand Up @@ -46,13 +53,20 @@ func Info(format string, args ...interface{}) {
logMessage(color.FgGreen, format, args...)
}

// VerboseLog logs a verbose message if verbose logging is enabled
func VerboseLog(format string, args ...interface{}) {
if Verbose {
logMessage(color.FgCyan, format, args...)
}
}

// CheckIfError logs an error message and panics if the error is not nil
func CheckIfError(err error) {
if err == nil {
return
}
logMessage(color.FgHiRed, "error: %s", err)
panic(err)
//panic(err)
}

// Warning logs a warning message with yellow text and bold style
Expand Down

0 comments on commit f02b01f

Please sign in to comment.