Skip to content

Commit

Permalink
refactor: fix unproper logger print
Browse files Browse the repository at this point in the history
  • Loading branch information
deryrahman authored and irainia committed Oct 4, 2022
1 parent 6a1ec5a commit 4e125d0
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 30 deletions.
8 changes: 0 additions & 8 deletions cmd/commands.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package cmd

import (
"os"

"github.com/MakeNowJust/heredoc"
"github.com/odpf/salt/cmdx"
cli "github.com/spf13/cobra"
Expand All @@ -12,7 +10,6 @@ import (
"github.com/odpf/optimus/cmd/deploy"
"github.com/odpf/optimus/cmd/extension"
"github.com/odpf/optimus/cmd/initialize"
"github.com/odpf/optimus/cmd/internal/logger"
"github.com/odpf/optimus/cmd/job"
"github.com/odpf/optimus/cmd/namespace"
"github.com/odpf/optimus/cmd/playground"
Expand All @@ -23,18 +20,13 @@ import (
"github.com/odpf/optimus/cmd/secret"
"github.com/odpf/optimus/cmd/serve"
"github.com/odpf/optimus/cmd/version"
"github.com/odpf/optimus/utils"
)

// New constructs the 'root' command. It houses all other sub commands
// default output of logging should go to stdout
// interactive output like progress bars should go to stderr
// unless the stdout/err is a tty, colors/progressbar should be disabled
func New() *cli.Command {
if utils.IsTerminal(os.Stdout) {
logger.InitializeColor()
}

cmd := &cli.Command{
Use: "optimus <command> <subcommand> [flags]",
Long: heredoc.Doc(`
Expand Down
14 changes: 7 additions & 7 deletions cmd/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (d *deployCommand) PreRunE(_ *cobra.Command, _ []string) error {
}

func (d *deployCommand) RunE(_ *cobra.Command, _ []string) error {
d.logger.Info("Registering project [%s] to [%s]", d.clientConfig.Project.Name, d.clientConfig.Host)
d.logger.Info(fmt.Sprintf("Registering project [%s] to [%s]", d.clientConfig.Project.Name, d.clientConfig.Host))
if err := project.RegisterProject(d.logger, d.clientConfig.Host, d.clientConfig.Project); err != nil {
return err
}
Expand All @@ -102,7 +102,7 @@ func (d *deployCommand) RunE(_ *cobra.Command, _ []string) error {
}
d.logger.Info("validation finished!\n")

d.logger.Info("Registering namespaces for [%s] to [%s]", d.clientConfig.Project.Name, d.clientConfig.Host)
d.logger.Info(fmt.Sprintf("Registering namespaces for [%s] to [%s]", d.clientConfig.Project.Name, d.clientConfig.Host))
if err := namespace.RegisterSelectedNamespaces(d.logger, d.clientConfig.Host, d.clientConfig.Project.Name, selectedNamespaces...); err != nil {
return err
}
Expand Down Expand Up @@ -146,7 +146,7 @@ func (d *deployCommand) deployJobs(conn *connectivity.Connectivity, selectedName
for _, namespace := range selectedNamespaces {
namespaceNames = append(namespaceNames, namespace.Name)
}
d.logger.Info("\n> Deploying jobs from namespaces [%s]", strings.Join(namespaceNames, ","))
d.logger.Info(fmt.Sprintf("\n> Deploying jobs from namespaces [%s]", strings.Join(namespaceNames, ",")))

stream, err := d.getJobStreamClient(conn)
if err != nil {
Expand Down Expand Up @@ -455,19 +455,19 @@ func PollJobDeployment(ctx context.Context, l log.Logger, jobSpecService pb.JobS
if len(resp.Failures) > 0 {
for _, failedJob := range resp.Failures {
if failedJob.GetJobName() != "" {
l.Error("Unable to deploy job %s: %s", failedJob.GetJobName(), failedJob.GetMessage())
l.Error(fmt.Sprintf("Unable to deploy job %s: %s", failedJob.GetJobName(), failedJob.GetMessage()))
} else {
l.Error("Job deployment failed: %s", failedJob.GetMessage())
l.Error(fmt.Sprintf("Job deployment failed: %s", failedJob.GetMessage()))
}
}
}
if len(resp.UnknownDependencies) > 0 {
l.Error("Unable to create sensors for below jobs:")
for jobName, dependencies := range resp.UnknownDependencies {
l.Error("- %s: invalid dependency name(s): %s.", jobName, dependencies)
l.Error(fmt.Sprintf("- %s: invalid dependency name(s): %s.", jobName, dependencies))
}
}
l.Error("Deployed %d/%d jobs.", resp.SuccessCount, resp.SuccessCount+resp.FailureCount)
l.Error(fmt.Sprintf("Deployed %d/%d jobs.", resp.SuccessCount, resp.SuccessCount+resp.FailureCount))
return errors.New("job deployment failed")
}

Expand Down
20 changes: 6 additions & 14 deletions cmd/internal/logger/color.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,17 @@
package logger

import (
"fmt"

"github.com/muesli/termenv"
"github.com/odpf/salt/term"
)

var (
// ColoredSuccess format message with color for success
ColoredSuccess = fmt.Sprintf

tp = termenv.EnvColorProfile()
ColorYellow = tp.Color("#FFAF00")
ColorWhite = tp.Color("#FFFFFF")
ColorRed = tp.Color("#D70000")
)

func InitializeColor() {
cs := term.NewColorScheme()
cs = term.NewColorScheme()
ColoredSuccess = func(s string, a ...interface{}) string {
return cs.Greenf(s, a...)
}
}

tp = termenv.EnvColorProfile()
ColorYellow = tp.Color("#DBAB79")
ColorRed = tp.Color("#E88388")
)
2 changes: 1 addition & 1 deletion cmd/internal/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
type colorFormatter int

func (*colorFormatter) Format(entry *logrus.Entry) ([]byte, error) {
var colorcode = ColorWhite
var colorcode termenv.Color
switch entry.Level {
case logrus.ErrorLevel, logrus.FatalLevel, logrus.PanicLevel:
colorcode = ColorRed
Expand Down

0 comments on commit 4e125d0

Please sign in to comment.