Skip to content

Commit

Permalink
Merge pull request #63 from wttech/console-text-opt
Browse files Browse the repository at this point in the history
Console log opt
  • Loading branch information
krystian-panek-vmltech authored Feb 14, 2023
2 parents 785631b + 9a87e4e commit 583f13a
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 33 deletions.
43 changes: 20 additions & 23 deletions cmd/aem/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ type CLI struct {
started time.Time
ended time.Time

outputFormat string
outputValue string
outputBuffer *bytes.Buffer
outputLogFile string
outputLogText bool
outputResponse *OutputResponse
outputWriter io.Writer
outputFormat string
outputValue string
outputBuffer *bytes.Buffer
outputLogFile string
outputLogConsole bool
outputResponse *OutputResponse
outputWriter io.Writer
}

func NewCLI(aem *pkg.Aem, config *cfg.Config) *CLI {
Expand All @@ -54,7 +54,7 @@ func NewCLI(aem *pkg.Aem, config *cfg.Config) *CLI {
result.config = config

result.outputLogFile = common.LogFile
result.outputLogText = true
result.outputLogConsole = true
result.outputValue = common.OutputValueAll
result.outputFormat = fmtx.Text
result.outputBuffer = bytes.NewBufferString("")
Expand Down Expand Up @@ -100,21 +100,21 @@ func (c *CLI) configureOutput() {
c.outputValue = c.config.Values().Output.Value
c.outputFormat = strings.ReplaceAll(c.config.Values().Output.Format, "yaml", "yml")
c.outputLogFile = c.config.Values().Output.Log.File
c.outputLogText = c.config.Values().Output.Log.Text
c.outputLogConsole = c.config.Values().Output.Log.Console

if c.outputValue != common.OutputValueNone && c.outputValue != common.OutputValueAll {
c.outputFormat = fmtx.Text
c.outputLogText = true
c.outputLogConsole = false
}
if c.outputFormat != fmtx.Text {
c.outputLogText = true
c.outputLogConsole = false
}
if !lo.Contains(cfg.OutputFormats(), c.outputFormat) {
log.Fatalf("unsupported CLI output format detected '%s'! supported ones are: %s", c.outputFormat, strings.Join(cfg.OutputFormats(), ", "))
}

if c.outputFormat == fmtx.Text {
if c.outputLogText {
if !c.outputLogConsole {
outputWriter := c.openOutputLogFile()
c.aem.SetOutput(outputWriter)
log.SetOutput(outputWriter)
Expand Down Expand Up @@ -174,25 +174,22 @@ func (c *CLI) printOutputText() {

func (c *CLI) printCommandResult() {
r := c.outputResponse
entry := fmt.Sprintf("%s", r.Msg)
if color.NoColor || c.outputLogText {
msg := fmt.Sprintf("%s", r.Msg)
if color.NoColor || !c.outputLogConsole {
entry := log.WithField("changed", r.Changed).WithField("elapsed", r.Elapsed)
if r.Failed {
log.Errorf(entry)
entry.Errorf(msg)
} else {
if r.Changed {
log.Info(fmt.Sprintf("%s [changed]", entry))
} else {
log.Info(fmt.Sprintf("%s [unchanged]", entry))
}
entry.Infof(msg)
}
} else {
if r.Failed {
log.Errorf(color.RedString(entry))
log.Errorf(color.RedString(msg))
} else {
if r.Changed {
log.Info(color.YellowString(entry))
log.Info(color.YellowString(msg))
} else {
log.Info(color.GreenString(entry))
log.Info(color.GreenString(msg))
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/aem/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ func (c *CLI) rootFlags(cmd *cobra.Command) {
cmd.PersistentFlags().StringVar(&(c.config.Values().Output.Log.File),
"output-log-file", c.config.Values().Output.Log.File,
"Controls output file path")
cmd.PersistentFlags().BoolVar(&(c.config.Values().Output.Log.Text),
"output-log-text", c.config.Values().Output.Log.Text,
"Write outputs and log entries to file instead of printing to console (when output format is \"text\")")
cmd.PersistentFlags().BoolVar(&(c.config.Values().Output.Log.Console),
"output-log-console", c.config.Values().Output.Log.Console,
"Print outputs and log entries to console instead of writing to file (when format is \"text\")")
cmd.PersistentFlags().StringVar(&(c.config.Values().Output.Value),
"output-value", c.config.Values().Output.Value,
"Limits output to single variable")
Expand Down
2 changes: 1 addition & 1 deletion pkg/cfg/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func setDefaults(v *viper.Viper) {
v.SetDefault("output.format", fmtx.Text)
v.SetDefault("output.value", common.OutputValueAll)
v.SetDefault("output.log.file", common.LogFile)
v.SetDefault("output.log.text", false)
v.SetDefault("output.log.console", true)

v.SetDefault("instance.processing_mode", instance.ProcessingAuto)

Expand Down
4 changes: 2 additions & 2 deletions pkg/cfg/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ type ConfigValues struct {
Format string `mapstructure:"format" yaml:"format"`
Value string `mapstructure:"value" yaml:"value"`
Log struct {
File string `mapstructure:"file" yaml:"file"`
Text bool `mapstructure:"text" yaml:"text"`
File string `mapstructure:"file" yaml:"file"`
Console bool `mapstructure:"console" yaml:"console"`
} `mapstructure:"log" yaml:"log"`
} `mapstructure:"output" yaml:"output"`

Expand Down
4 changes: 2 additions & 2 deletions project/classic/aem/default/etc/aem.yml
Original file line number Diff line number Diff line change
Expand Up @@ -191,5 +191,5 @@ output:
log:
# File path of logs written especially when output format is different than 'text'
file: aem/home/var/log/aem.log
# Controls if console output and log entries should be written to file instead of printed to console when output format is 'text'
text: false
# Controls if script outputs and log entries should be printed to console instead of written to file when output format is 'text'
console: true
4 changes: 2 additions & 2 deletions project/cloud/aem/default/etc/aem.yml
Original file line number Diff line number Diff line change
Expand Up @@ -191,5 +191,5 @@ output:
log:
# File path of logs written especially when output format is different than 'text'
file: aem/home/var/log/aem.log
# Controls if console output and log entries should be printed to console when output format is 'text'
text: false
# Controls if script outputs and log entries should be printed to console instead of written to file when output format is 'text'
console: true

0 comments on commit 583f13a

Please sign in to comment.