Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: export setupMessage function #3221

Merged
merged 3 commits into from
Nov 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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