Skip to content

Commit

Permalink
readd message logging now that we can disable it
Browse files Browse the repository at this point in the history
Signed-off-by: Kit Patella <[email protected]>
  • Loading branch information
mkcp committed Oct 24, 2024
1 parent 63e660c commit 5ffcb48
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 198 deletions.
5 changes: 3 additions & 2 deletions src/cmd/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ var packageCreateCmd = &cobra.Command{

var isCleanPathRegex = regexp.MustCompile(`^[a-zA-Z0-9\_\-\/\.\~\\:]+$`)
if !isCleanPathRegex.MatchString(config.CommonOptions.CachePath) {
// TODO(mkcp): Remove message on logger release
message.Warnf(lang.CmdPackageCreateCleanPathErr, config.ZarfDefaultCachePath)
l.Warn("invalid characters in Zarf cache path, using default", "cfg", config.ZarfDefaultCachePath, "default", config.ZarfDefaultCachePath)
config.CommonOptions.CachePath = config.ZarfDefaultCachePath
}
Expand All @@ -70,8 +72,7 @@ var packageCreateCmd = &cobra.Command{
}
defer pkgClient.ClearTempPaths()

// TODO(mkcp): Finish migrating packager.Create
err = pkgClient.Create()
err = pkgClient.Create(cmd.Context())

// NOTE(mkcp): LintErrors are rendered with a table
var lintErr *lint.LintError
Expand Down
50 changes: 37 additions & 13 deletions src/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,6 @@ func preRun(cmd *cobra.Command, _ []string) error {
skipLogFile = true
}

// Configure the global message instance.
err := setupMessage(LogLevelCLI, skipLogFile, NoColor)
if err != nil {
return err
}

// Configure logger and add it to cmd context.
l, err := setupLogger(LogLevelCLI, LogFormat)
if err != nil {
Expand All @@ -97,6 +91,21 @@ func preRun(cmd *cobra.Command, _ []string) error {
ctx := logger.WithContext(cmd.Context(), l)
cmd.SetContext(ctx)

// Configure the global message instance.
var disableMessage bool
if LogFormat != "" {
disableMessage = true
}
err = setupMessage(messageCfg{
level: LogLevelCLI,
skipLogFile: skipLogFile,
noColor: NoColor,
featureDisabled: disableMessage,
})
if err != nil {
return err
}

// Print out config location
common.PrintViperConfigUsed(cmd.Context())
return nil
Expand Down Expand Up @@ -186,14 +195,29 @@ 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
}

// setupMessage configures message while we migrate over to logger.
func setupMessage(logLevel string, skipLogFile, noColor bool) error {
// TODO(mkcp): Delete no-color
if noColor {
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 {
message.InitializePTerm(io.Discard)
return nil
}

if cfg.noColor {
message.DisableColor()
}

if logLevel != "" {
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 @@ -202,12 +226,12 @@ func setupMessage(logLevel string, skipLogFile, noColor bool) error {
"debug": message.DebugLevel,
"trace": message.TraceLevel,
}
lvl, ok := match[logLevel]
lvl, ok := match[level]
if !ok {
return errors.New("invalid log level, valid options are warn, info, debug, error, and trace")
}
message.SetLogLevel(lvl)
message.Debug("Log level set to " + logLevel)
message.Debug("Log level set to " + level)
}

// Disable progress bars for CI envs
Expand All @@ -216,7 +240,7 @@ func setupMessage(logLevel string, skipLogFile, noColor bool) error {
message.NoProgress = true
}

if !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
1 change: 1 addition & 0 deletions src/config/lang/english.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ $ zarf package mirror-resources <your-package.tar.zst> \
CmdPackageCreateFlagDifferential = "[beta] Build a package that only contains the differential changes from local resources and differing remote resources from the specified previously built package"
CmdPackageCreateFlagRegistryOverride = "Specify a map of domains to override on package create when pulling images (e.g. --registry-override docker.io=dockerio-reg.enterprise.intranet)"
CmdPackageCreateFlagFlavor = "The flavor of components to include in the resulting package (i.e. have a matching or empty \"only.flavor\" key)"
CmdPackageCreateCleanPathErr = "Invalid characters in Zarf cache path, defaulting to %s"

CmdPackageDeployFlagConfirm = "Confirms package deployment without prompting. ONLY use with packages you trust. Skips prompts to review SBOM, configure variables, select optional components and review potential breaking changes."
CmdPackageDeployFlagAdoptExistingResources = "Adopts any pre-existing K8s resources into the Helm charts managed by Zarf. ONLY use when you have existing deployments you want Zarf to takeover."
Expand Down
Loading

0 comments on commit 5ffcb48

Please sign in to comment.