Skip to content

Commit

Permalink
Improve logging for command operations
Browse files Browse the repository at this point in the history
- Enhanced log messages for clearer communication to the user
- Added detailed error messages
- Consistent formatting across various logs
  • Loading branch information
stcrestrada committed May 29, 2024
1 parent 55545b7 commit 8c39c33
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 13 deletions.
8 changes: 4 additions & 4 deletions cmd/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (cmd *CloneCommand) Run(c *cli.Context) error {
for i, repo := range repos {
states[i] = &ui.SpinnerState{
State: ui.StateLoading,
Msg: fmt.Sprintf("Pulling %s", repo.Name),
Msg: fmt.Sprintf("cloning %s", repo.Name),
}
}

Expand All @@ -78,15 +78,15 @@ func (cmd *CloneCommand) Run(c *cli.Context) error {
if strings.Contains(rc.StdErr.String(), "already exists") {
onFinish.Failed = false
state.State = ui.StateSuccess
state.Msg = fmt.Sprintf("Already cloned %s", repo.Name)
state.Msg = fmt.Sprintf("%s is already cloned", repo.Name)
} else {
state.State = ui.StateError
state.Msg = fmt.Sprintf("Failed to clone %s", repo.Name)
state.Msg = fmt.Sprintf("failed to clone %s", repo.Name)
}

} else {
state.State = ui.StateSuccess
state.Msg = fmt.Sprintf("Finished cloning %s", repo.Name)
state.Msg = fmt.Sprintf("finished cloning %s", repo.Name)
}
commandOnFinish[i] = onFinish
})
Expand Down
2 changes: 0 additions & 2 deletions cmd/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ 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 @@ -77,7 +76,6 @@ 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
2 changes: 1 addition & 1 deletion cmd/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func (cmd *StatusCommand) Run(c *cli.Context) error {
if !onFinish.Failed {
stdout := indent.String(" ", onFinish.RunConfig.StdOut.String())
stderr := indent.String(" ", onFinish.RunConfig.StdErr.String())
fmt.Printf("🟢 Status %s \n Stdout:\n%s\n StdErr:\n%s\n", onFinish.Repo, stdout, stderr)
fmt.Printf("🟣 Status %s \n Stdout:\n%s\n StdErr:\n%s\n", onFinish.Repo, stdout, stderr)
} else {
stdout := indent.String(" ", onFinish.RunConfig.StdOut.String())
stderr := indent.String(" ", onFinish.RunConfig.StdErr.String())
Expand Down
17 changes: 14 additions & 3 deletions pkg/ui/spinner.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ui

import (
"fmt"
"github.com/fatih/color"
"io"
"time"
)
Expand All @@ -14,6 +15,12 @@ type SpinnerState struct {
Err string
}

func FormatSpinnerMessage(symbolColor color.Attribute, symbol string, messageColor color.Attribute, format string, args ...interface{}) string {
symbolColored := color.New(symbolColor, color.Bold).Sprintf(symbol)
messageColored := color.New(messageColor, color.Bold).Sprintf(format, args...)
return fmt.Sprintf("%s %s\n", symbolColored, messageColored)
}

const StateLoading = State("loading")
const StateError = State("error")
const StateSuccess = State("success")
Expand All @@ -35,15 +42,19 @@ func PrintSpinnerStates(writer io.Writer, states []*SpinnerState) func() {

shouldContinue := false
for _, state := range states {
var formattedMessage string
spinnerIcon := spinnerUnicodeStates[i%len(spinnerUnicodeStates)]
if state.State == StateSuccess {
spinnerIcon = "✅"
spinnerIcon = "✓"
formattedMessage = FormatSpinnerMessage(color.FgGreen, spinnerIcon, color.FgWhite, state.Msg)
} else if state.State == StateError {
spinnerIcon = "❌"
spinnerIcon = "✗"
formattedMessage = FormatSpinnerMessage(color.FgRed, spinnerIcon, color.FgWhite, state.Msg)
} else {
shouldContinue = true
formattedMessage = FormatSpinnerMessage(color.FgWhite, spinnerIcon, color.FgWhite, state.Msg)
}
writer.Write([]byte(fmt.Sprintf("%s %s\n", spinnerIcon, state.Msg)))
writer.Write([]byte(formattedMessage))
}
return shouldContinue
}
Expand Down
6 changes: 6 additions & 0 deletions pkg/util/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,9 @@ func Warning(format string, args ...interface{}) {
func WarningRed(format string, args ...interface{}) {
logMessage(color.FgRed, "✗", color.FgWhite, format, args...)
}

func ReturnSpinnerOutput(symbolColor color.Attribute, symbol string, messageColor color.Attribute, format string, args ...interface{}) string {
symbolColored := color.New(symbolColor, color.Bold).Sprintf(symbol)
messageColored := color.New(messageColor, color.Bold).Sprintf(format, args...)
return fmt.Sprintf("%s %s\n", symbolColored, messageColored)
}
15 changes: 12 additions & 3 deletions pkg/util/repo_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,23 @@ func (r *RepoUtils) GeeAdd(ctx *types.GeeContext, cwd string) error {
func (r *RepoUtils) HandlePullFinish(repo *types.Repo, onFinish *types.CommandOnFinish, state *ui.SpinnerState) {
switch {
case onFinish.Failed && strings.Contains(onFinish.RunConfig.StdErr.String(), "No such file or directory") && repo.Remote != "":
state.Msg = fmt.Sprintf("Cloning instead...")
state.State = ui.StateLoading
state.Msg = fmt.Sprintf("pull operation failed: no such file or directory. cloning %s instead...", repo.Name)
onFinish.Failed = false
onFinish.Error = nil
rc := &types.RunConfig{
StdErr: &bytes.Buffer{},
StdOut: &bytes.Buffer{},
}
onFinish.RunConfig = rc

r.RepoOp.Clone(repo.Name, repo.Remote, repo.Path, onFinish.RunConfig, r.HandleCloneFinish(repo, state))
case onFinish.Failed:
state.State = ui.StateError
state.Msg = fmt.Sprintf("Failed to pull %s", repo.Name)
state.Msg = fmt.Sprintf("failed to pull %s", repo.Name)
default:
state.State = ui.StateSuccess
state.Msg = fmt.Sprintf("%s already up to date", onFinish.Repo)
state.Msg = fmt.Sprintf("%s is already up to date", onFinish.Repo)
}
}

Expand Down

0 comments on commit 8c39c33

Please sign in to comment.