Skip to content

Commit

Permalink
chore: export setupMessage function (#3221)
Browse files Browse the repository at this point in the history
Signed-off-by: catsby <[email protected]>
  • Loading branch information
catsby authored Nov 8, 2024
1 parent 36b779f commit f4c80cb
Showing 1 changed file with 19 additions and 18 deletions.
37 changes: 19 additions & 18 deletions src/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,11 @@ func preRun(cmd *cobra.Command, _ []string) error {
ctx := logger.WithLoggingEnabled(ctx, true)
cmd.SetContext(ctx)
}
err = setupMessage(messageCfg{
level: LogLevelCLI,
skipLogFile: skipLogFile,
noColor: NoColor,
featureDisabled: disableMessage,
err = SetupMessage(MessageCfg{
Level: LogLevelCLI,
SkipLogFile: skipLogFile,
NoColor: NoColor,
FeatureDisabled: disableMessage,
})
if err != nil {
return err
Expand Down Expand Up @@ -208,32 +208,33 @@ func setupLogger(level, format string) (*slog.Logger, error) {
return l, nil
}

type messageCfg struct {
level string
skipLogFile bool
noColor bool
// featureDisabled is a feature flag that disables it
featureDisabled bool
// MessageCfg is used to configure the Message package output options.
type MessageCfg struct {
Level string
SkipLogFile bool
NoColor bool
// FeatureDisabled is a feature flag that disables it
FeatureDisabled bool
}

// setupMessage configures message while we migrate over to logger.
func setupMessage(cfg messageCfg) error {
// SetupMessage configures message while we migrate over to logger.
func SetupMessage(cfg MessageCfg) error {
// HACK(mkcp): Discard message logs if feature is disabled. message calls InitializePTerm once in its init() fn so
// this ends up being a messy solution.
if cfg.featureDisabled {
if cfg.FeatureDisabled {
// Discard all* PTerm messages. *see below
message.InitializePTerm(io.Discard)
// Disable all progress bars and spinners
message.NoProgress = true
return nil
}

if cfg.noColor {
if cfg.NoColor {
message.DisableColor()
}

level := cfg.level
if cfg.level != "" {
level := cfg.Level
if cfg.Level != "" {
match := map[string]message.LogLevel{
// NOTE(mkcp): Add error for forwards compatibility with logger
"error": message.WarnLevel,
Expand All @@ -256,7 +257,7 @@ func setupMessage(cfg messageCfg) error {
message.NoProgress = true
}

if !cfg.skipLogFile {
if !cfg.SkipLogFile {
ts := time.Now().Format("2006-01-02-15-04-05")
f, err := os.CreateTemp("", fmt.Sprintf("zarf-%s-*.log", ts))
if err != nil {
Expand Down

0 comments on commit f4c80cb

Please sign in to comment.