From 083cc6f1e2898ab0e52cc221ae4ffb73b20ba133 Mon Sep 17 00:00:00 2001 From: razzle Date: Thu, 14 Mar 2024 16:18:50 -0500 Subject: [PATCH] squash Signed-off-by: razzle --- src/cmd/common/setup.go | 12 +++++++- src/pkg/message/credentials.go | 25 +++++++--------- src/pkg/message/generic.go | 16 ----------- src/pkg/message/message.go | 52 ++++++++++++++++------------------ src/pkg/message/pausable.go | 27 ++++++++++++++++++ src/pkg/message/progress.go | 3 ++ 6 files changed, 75 insertions(+), 60 deletions(-) delete mode 100644 src/pkg/message/generic.go create mode 100644 src/pkg/message/pausable.go diff --git a/src/cmd/common/setup.go b/src/cmd/common/setup.go index 8bde89e640..1466dfe927 100644 --- a/src/cmd/common/setup.go +++ b/src/cmd/common/setup.go @@ -5,12 +5,14 @@ package common import ( + "io" "os" "github.com/defenseunicorns/zarf/src/config" "github.com/defenseunicorns/zarf/src/config/lang" "github.com/defenseunicorns/zarf/src/pkg/message" "github.com/defenseunicorns/zarf/src/pkg/utils/exec" + "github.com/pterm/pterm" ) // LogLevelCLI holds the log level as input from a command @@ -50,6 +52,14 @@ func SetupCLI() { } if !config.SkipLogFile { - message.UseLogFile() + logFile, err := message.UseLogFile("") + if err != nil { + message.WarnErr(err, "Error saving a log file to a temporary directory") + return + } + + pterm.SetDefaultOutput(io.MultiWriter(os.Stderr, logFile)) + location := message.LogFileLocation() + message.Notef("Saving log file to %s", location) } } diff --git a/src/pkg/message/credentials.go b/src/pkg/message/credentials.go index 15e1e87c71..60ca971119 100644 --- a/src/pkg/message/credentials.go +++ b/src/pkg/message/credentials.go @@ -6,7 +6,6 @@ package message import ( "fmt" - "os" "strings" "github.com/defenseunicorns/zarf/src/config" @@ -31,8 +30,11 @@ func PrintCredentialTable(state *types.ZarfState, componentsToDeploy []types.Dep componentsToDeploy = []types.DeployedComponent{{Name: "logging"}, {Name: "git-server"}} } - // Set output to os.Stderr to avoid creds being printed in logs - pterm.SetDefaultOutput(os.Stderr) + // Pause the logfile's output to avoid credentials being printed to the log file + if logFile != nil { + logFile.pause() + defer logFile.resume() + } loginData := [][]string{} if state.RegistryInfo.InternalRegistry { @@ -61,11 +63,6 @@ func PrintCredentialTable(state *types.ZarfState, componentsToDeploy []types.Dep header := []string{"Application", "Username", "Password", "Connect", "Get-Creds Key"} Table(header, loginData) } - - // Restore the log file if it was specified - if !config.SkipLogFile { - UseLogFile() - } } // PrintComponentCredential displays credentials for a single component @@ -96,8 +93,11 @@ func PrintComponentCredential(state *types.ZarfState, componentName string) { // PrintCredentialUpdates displays credentials that will be updated func PrintCredentialUpdates(oldState *types.ZarfState, newState *types.ZarfState, services []string) { - // Set output to os.Stderr to avoid creds being printed in logs - pterm.SetDefaultOutput(os.Stderr) + // Pause the logfile's output to avoid credentials being printed to the log file + if logFile != nil { + logFile.pause() + defer logFile.resume() + } for _, service := range services { @@ -144,11 +144,6 @@ func PrintCredentialUpdates(oldState *types.ZarfState, newState *types.ZarfState } pterm.Println() - - // Restore the log file if it was specified - if !config.SkipLogFile { - UseLogFile() - } } func compareStrings(old string, new string, secret bool) string { diff --git a/src/pkg/message/generic.go b/src/pkg/message/generic.go deleted file mode 100644 index 272bd773fd..0000000000 --- a/src/pkg/message/generic.go +++ /dev/null @@ -1,16 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -// SPDX-FileCopyrightText: 2021-Present The Zarf Authors - -// Package message provides a rich set of functions for displaying messages to the user. -package message - -import "github.com/pterm/pterm" - -// Generic is used to implement the io.Writer interface for generic messages. -type Generic struct{} - -func (g *Generic) Write(p []byte) (n int, err error) { - text := string(p) - pterm.Println(text) - return len(p), nil -} diff --git a/src/pkg/message/message.go b/src/pkg/message/message.go index d17baca298..4cc6e7fd39 100644 --- a/src/pkg/message/message.go +++ b/src/pkg/message/message.go @@ -44,17 +44,11 @@ var NoProgress bool // RuleLine creates a line of ━ as wide as the terminal var RuleLine = strings.Repeat("━", TermWidth) -// LogWriter is the stream to write logs to. -var LogWriter io.Writer = os.Stderr - // logLevel holds the pterm compatible log level integer var logLevel = InfoLevel // logFile acts as a buffer for logFile generation -var logFile *os.File - -// useLogFile controls whether to use the log file or not -var useLogFile bool +var logFile *pausableLogFile // DebugWriter represents a writer interface that writes to message.Debug type DebugWriter struct{} @@ -83,27 +77,29 @@ func init() { } // UseLogFile writes output to stderr and a logFile. -func UseLogFile() { +func UseLogFile(dir string) (io.Writer, error) { // Prepend the log filename with a timestamp. ts := time.Now().Format("2006-01-02-15-04-05") - var err error - if logFile != nil { - // Use the existing log file if logFile is set - LogWriter = io.MultiWriter(os.Stderr, logFile) - pterm.SetDefaultOutput(LogWriter) - } else { - // Try to create a temp log file if one hasn't been made already - if logFile, err = os.CreateTemp("", fmt.Sprintf("zarf-%s-*.log", ts)); err != nil { - WarnErr(err, "Error saving a log file to a temporary directory") - } else { - useLogFile = true - LogWriter = io.MultiWriter(os.Stderr, logFile) - pterm.SetDefaultOutput(LogWriter) - message := fmt.Sprintf("Saving log file to %s", logFile.Name()) - Note(message) - } + f, err := os.CreateTemp(dir, fmt.Sprintf("zarf-%s-*.log", ts)) + if err != nil { + return nil, err + } + + logFile = &pausableLogFile{ + wr: f, + f: f, } + + return logFile, nil +} + +// LogFileLocation returns the location of the log file. +func LogFileLocation() string { + if logFile == nil { + return "" + } + return logFile.f.Name() } // SetLogLevel sets the log level. @@ -223,8 +219,8 @@ func Question(text string) { // Questionf prints a user prompt description message with a given format. func Questionf(format string, a ...any) { - pterm.Println() message := Paragraph(format, a...) + pterm.Println() pterm.FgLightGreen.Println(message) } @@ -235,7 +231,6 @@ func Note(text string) { // Notef prints a note message with a given format. func Notef(format string, a ...any) { - pterm.Println() message := Paragraphn(TermWidth-7, format, a...) notePrefix := pterm.PrefixPrinter{ MessageStyle: &pterm.ThemeDefault.InfoMessageStyle, @@ -244,6 +239,7 @@ func Notef(format string, a ...any) { Text: "NOTE", }, } + pterm.Println() notePrefix.Println(message) } @@ -256,10 +252,10 @@ func Title(title string, help string) { // HeaderInfof prints a large header with a formatted message. func HeaderInfof(format string, a ...any) { + pterm.Println() message := Truncate(fmt.Sprintf(format, a...), TermWidth, false) // Ensure the text is consistent for the header width padding := TermWidth - len(message) - pterm.Println() pterm.DefaultHeader. WithBackgroundStyle(pterm.NewStyle(pterm.BgDarkGray)). WithTextStyle(pterm.NewStyle(pterm.FgLightWhite)). @@ -366,7 +362,7 @@ func debugPrinter(offset int, a ...any) { printer.Println(a...) // Always write to the log file - if useLogFile { + if logFile != nil { pterm.Debug. WithShowLineNumber(true). WithLineNumberOffset(offset). diff --git a/src/pkg/message/pausable.go b/src/pkg/message/pausable.go new file mode 100644 index 0000000000..5393126a1a --- /dev/null +++ b/src/pkg/message/pausable.go @@ -0,0 +1,27 @@ +package message + +import ( + "io" + "os" +) + +// pausableLogFile is a pausable log file +type pausableLogFile struct { + wr io.Writer + f *os.File +} + +// pause the log file +func (l *pausableLogFile) pause() { + l.wr = io.Discard +} + +// resume the log file +func (l *pausableLogFile) resume() { + l.wr = l.f +} + +// Write writes the data to the log file +func (l *pausableLogFile) Write(p []byte) (n int, err error) { + return l.wr.Write(p) +} diff --git a/src/pkg/message/progress.go b/src/pkg/message/progress.go index 185edbf517..1c6e732042 100644 --- a/src/pkg/message/progress.go +++ b/src/pkg/message/progress.go @@ -5,6 +5,8 @@ package message import ( + "os" + "github.com/pterm/pterm" ) @@ -28,6 +30,7 @@ func NewProgressBar(total int64, text string) *ProgressBar { WithTitle(padding + text). WithRemoveWhenDone(true). WithMaxWidth(TermWidth). + WithWriter(os.Stderr). Start() }