Skip to content

Commit

Permalink
ok lets try this
Browse files Browse the repository at this point in the history
Signed-off-by: razzle <harry@razzle.cloud>
  • Loading branch information
Noxsios committed Mar 14, 2024
1 parent ff756d4 commit 0f313fa
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 62 deletions.
12 changes: 11 additions & 1 deletion src/cmd/common/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}
2 changes: 1 addition & 1 deletion src/pkg/interactive/components.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func SelectChoiceGroup(componentGroup []types.ZarfComponent) types.ZarfComponent
Options: options,
}

pterm.DefaultBasicText.Println()
pterm.Println()

if err := survey.AskOne(prompt, &chosen); err != nil {
message.Fatalf(nil, lang.PkgDeployErrComponentSelectionCanceled, err.Error())
Expand Down
54 changes: 27 additions & 27 deletions src/pkg/message/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ func PrintCredentialTable(state *types.ZarfState, componentsToDeploy []types.Dep
}

// Pause the logfile's output to avoid credentials being printed to the log file
if LogFile != nil {
LogFile.Pause()
defer LogFile.Resume()
if logFile != nil {
logFile.pause()
defer logFile.resume()
}

loginData := [][]string{}
Expand Down Expand Up @@ -94,9 +94,9 @@ func PrintComponentCredential(state *types.ZarfState, componentName string) {
// PrintCredentialUpdates displays credentials that will be updated
func PrintCredentialUpdates(oldState *types.ZarfState, newState *types.ZarfState, services []string) {
// Pause the logfile's output to avoid credentials being printed to the log file
if LogFile != nil {
LogFile.Pause()
defer LogFile.Resume()
if logFile != nil {
logFile.pause()
defer logFile.resume()
}

for _, service := range services {
Expand All @@ -108,42 +108,42 @@ func PrintCredentialUpdates(oldState *types.ZarfState, newState *types.ZarfState
oR := oldState.RegistryInfo
nR := newState.RegistryInfo
Title("Registry", "the information used to interact with Zarf's container image registry")
pterm.DefaultBasicText.Println()
pterm.DefaultBasicText.Printfln(" %s: %s", pterm.Bold.Sprint("URL Address"), compareStrings(oR.Address, nR.Address, false))
pterm.DefaultBasicText.Printfln(" %s: %s", pterm.Bold.Sprint("Push Username"), compareStrings(oR.PushUsername, nR.PushUsername, false))
pterm.DefaultBasicText.Printfln(" %s: %s", pterm.Bold.Sprint("Push Password"), compareStrings(oR.PushPassword, nR.PushPassword, true))
pterm.DefaultBasicText.Printfln(" %s: %s", pterm.Bold.Sprint("Pull Username"), compareStrings(oR.PullUsername, nR.PullUsername, false))
pterm.DefaultBasicText.Printfln(" %s: %s", pterm.Bold.Sprint("Pull Password"), compareStrings(oR.PullPassword, nR.PullPassword, true))
pterm.Println()
pterm.Printfln(" %s: %s", pterm.Bold.Sprint("URL Address"), compareStrings(oR.Address, nR.Address, false))
pterm.Printfln(" %s: %s", pterm.Bold.Sprint("Push Username"), compareStrings(oR.PushUsername, nR.PushUsername, false))
pterm.Printfln(" %s: %s", pterm.Bold.Sprint("Push Password"), compareStrings(oR.PushPassword, nR.PushPassword, true))
pterm.Printfln(" %s: %s", pterm.Bold.Sprint("Pull Username"), compareStrings(oR.PullUsername, nR.PullUsername, false))
pterm.Printfln(" %s: %s", pterm.Bold.Sprint("Pull Password"), compareStrings(oR.PullPassword, nR.PullPassword, true))
case GitKey:
oG := oldState.GitServer
nG := newState.GitServer
Title("Git Server", "the information used to interact with Zarf's GitOps Git Server")
pterm.DefaultBasicText.Println()
pterm.DefaultBasicText.Printfln(" %s: %s", pterm.Bold.Sprint("URL Address"), compareStrings(oG.Address, nG.Address, false))
pterm.DefaultBasicText.Printfln(" %s: %s", pterm.Bold.Sprint("Push Username"), compareStrings(oG.PushUsername, nG.PushUsername, false))
pterm.DefaultBasicText.Printfln(" %s: %s", pterm.Bold.Sprint("Push Password"), compareStrings(oG.PushPassword, nG.PushPassword, true))
pterm.DefaultBasicText.Printfln(" %s: %s", pterm.Bold.Sprint("Pull Username"), compareStrings(oG.PullUsername, nG.PullUsername, false))
pterm.DefaultBasicText.Printfln(" %s: %s", pterm.Bold.Sprint("Pull Password"), compareStrings(oG.PullPassword, nG.PullPassword, true))
pterm.Println()
pterm.Printfln(" %s: %s", pterm.Bold.Sprint("URL Address"), compareStrings(oG.Address, nG.Address, false))
pterm.Printfln(" %s: %s", pterm.Bold.Sprint("Push Username"), compareStrings(oG.PushUsername, nG.PushUsername, false))
pterm.Printfln(" %s: %s", pterm.Bold.Sprint("Push Password"), compareStrings(oG.PushPassword, nG.PushPassword, true))
pterm.Printfln(" %s: %s", pterm.Bold.Sprint("Pull Username"), compareStrings(oG.PullUsername, nG.PullUsername, false))
pterm.Printfln(" %s: %s", pterm.Bold.Sprint("Pull Password"), compareStrings(oG.PullPassword, nG.PullPassword, true))
case ArtifactKey:
oA := oldState.ArtifactServer
nA := newState.ArtifactServer
Title("Artifact Server", "the information used to interact with Zarf's Artifact Server")
pterm.DefaultBasicText.Println()
pterm.DefaultBasicText.Printfln(" %s: %s", pterm.Bold.Sprint("URL Address"), compareStrings(oA.Address, nA.Address, false))
pterm.DefaultBasicText.Printfln(" %s: %s", pterm.Bold.Sprint("Push Username"), compareStrings(oA.PushUsername, nA.PushUsername, false))
pterm.DefaultBasicText.Printfln(" %s: %s", pterm.Bold.Sprint("Push Token"), compareStrings(oA.PushToken, nA.PushToken, true))
pterm.Println()
pterm.Printfln(" %s: %s", pterm.Bold.Sprint("URL Address"), compareStrings(oA.Address, nA.Address, false))
pterm.Printfln(" %s: %s", pterm.Bold.Sprint("Push Username"), compareStrings(oA.PushUsername, nA.PushUsername, false))
pterm.Printfln(" %s: %s", pterm.Bold.Sprint("Push Token"), compareStrings(oA.PushToken, nA.PushToken, true))
case AgentKey:
oT := oldState.AgentTLS
nT := newState.AgentTLS
Title("Agent TLS", "the certificates used to connect to Zarf's Agent")
pterm.DefaultBasicText.Println()
pterm.DefaultBasicText.Printfln(" %s: %s", pterm.Bold.Sprint("Certificate Authority"), compareStrings(string(oT.CA), string(nT.CA), true))
pterm.DefaultBasicText.Printfln(" %s: %s", pterm.Bold.Sprint("Public Certificate"), compareStrings(string(oT.Cert), string(nT.Cert), true))
pterm.DefaultBasicText.Printfln(" %s: %s", pterm.Bold.Sprint("Private Key"), compareStrings(string(oT.Key), string(nT.Key), true))
pterm.Println()
pterm.Printfln(" %s: %s", pterm.Bold.Sprint("Certificate Authority"), compareStrings(string(oT.CA), string(nT.CA), true))
pterm.Printfln(" %s: %s", pterm.Bold.Sprint("Public Certificate"), compareStrings(string(oT.Cert), string(nT.Cert), true))
pterm.Printfln(" %s: %s", pterm.Bold.Sprint("Private Key"), compareStrings(string(oT.Key), string(nT.Key), true))
}
}

pterm.DefaultBasicText.Println()
pterm.Println()
}

func compareStrings(old string, new string, secret bool) string {
Expand Down
52 changes: 33 additions & 19 deletions src/pkg/message/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ var RuleLine = strings.Repeat("━", TermWidth)
// logLevel holds the pterm compatible log level integer
var logLevel = InfoLevel

// LogFile acts as a buffer for LogFile generation
var LogFile *PausableLogFile
// logFile acts as a buffer for logFile generation
var logFile *pausableLogFile

// DebugWriter represents a writer interface that writes to message.Debug
type DebugWriter struct{}
Expand Down Expand Up @@ -77,20 +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")

f, err := os.CreateTemp("", fmt.Sprintf("zarf-%s-*.log", ts))
f, err := os.CreateTemp(dir, fmt.Sprintf("zarf-%s-*.log", ts))
if err != nil {
WarnErr(err, "Error saving a log file to a temporary directory")
return
return nil, err
}

LogFile = NewPausableLogFile(f)
pterm.SetDefaultOutput(io.MultiWriter(os.Stderr, LogFile))
message := fmt.Sprintf("Saving log file to %s", f.Name())
Note(message)
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.
Expand Down Expand Up @@ -149,7 +158,8 @@ func Warn(message string) {
// Warnf prints a warning message with a given format.
func Warnf(format string, a ...any) {
message := Paragraphn(TermWidth-10, format, a...)
pterm.Warning.Println("\n" + message)
pterm.Println()
pterm.Warning.Println(message)
}

// WarnErr prints an error message as a warning.
Expand Down Expand Up @@ -210,7 +220,8 @@ func Question(text string) {
// Questionf prints a user prompt description message with a given format.
func Questionf(format string, a ...any) {
message := Paragraph(format, a...)
pterm.FgLightGreen.Println("\n" + message)
pterm.Println()
pterm.FgLightGreen.Println(message)
}

// Note prints a note message.
Expand All @@ -228,7 +239,8 @@ func Notef(format string, a ...any) {
Text: "NOTE",
},
}
notePrefix.Println("\n" + message)
pterm.Println()
notePrefix.Println(message)
}

// Title prints a title and an optional help description for that section
Expand All @@ -240,19 +252,21 @@ 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.DefaultHeader.
WithBackgroundStyle(pterm.NewStyle(pterm.BgDarkGray)).
WithTextStyle(pterm.NewStyle(pterm.FgLightWhite)).
WithMargin(2).
Printfln("\n" + message + strings.Repeat(" ", padding))
Printfln(message + strings.Repeat(" ", padding))
}

// HorizontalRule prints a white horizontal rule to separate the terminal
func HorizontalRule() {
pterm.DefaultBasicText.Print("\n" + RuleLine)
pterm.Println()
pterm.Println(RuleLine)
}

// JSONValue prints any value as JSON.
Expand Down Expand Up @@ -282,7 +296,7 @@ func PrintDiff(textA, textB string) {

diffs = dmp.DiffCleanupSemantic(diffs)

pterm.DefaultBasicText.Println(dmp.DiffPrettyText(diffs))
pterm.Println(dmp.DiffPrettyText(diffs))
}

// Truncate truncates provided text to the requested length
Expand All @@ -304,7 +318,7 @@ func Truncate(text string, length int, invert bool) string {

// Table prints a padded table containing the specified header and data
func Table(header []string, data [][]string) {
pterm.DefaultBasicText.Println()
pterm.Println()

// To avoid side effects make copies of the header and data before adding padding
headerCopy := make([]string, len(header))
Expand Down Expand Up @@ -348,12 +362,12 @@ func debugPrinter(offset int, a ...any) {
printer.Println(a...)

// Always write to the log file
if LogFile != nil {
if logFile != nil {
pterm.Debug.
WithShowLineNumber(true).
WithLineNumberOffset(offset).
WithDebugger(false).
WithWriter(LogFile).
WithWriter(logFile).
Println(a...)
}
}
Expand Down
19 changes: 7 additions & 12 deletions src/pkg/message/pausable.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,23 @@ import (
"os"
)

// PausableLogFile is a pausable log file
type PausableLogFile struct {
// pausableLogFile is a pausable log file
type pausableLogFile struct {
wr io.Writer
f *os.File
}

// NewPausableLogFile creates a new pausable log file
func NewPausableLogFile(f *os.File) *PausableLogFile {
return &PausableLogFile{wr: f, f: f}
}

// Pause the log file
func (l *PausableLogFile) Pause() {
// pause the log file
func (l *pausableLogFile) pause() {
l.wr = io.Discard
}

// Resume the log file
func (l *PausableLogFile) Resume() {
// 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) {
func (l *pausableLogFile) Write(p []byte) (n int, err error) {
return l.wr.Write(p)
}
3 changes: 3 additions & 0 deletions src/pkg/message/progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
package message

import (
"os"

"github.com/pterm/pterm"
)

Expand All @@ -28,6 +30,7 @@ func NewProgressBar(total int64, text string) *ProgressBar {
WithTitle(padding + text).
WithRemoveWhenDone(true).
WithMaxWidth(TermWidth).
WithWriter(os.Stderr).
Start()
}

Expand Down
2 changes: 1 addition & 1 deletion src/pkg/packager/interactive.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (

func (p *Packager) confirmAction(stage string) (confirm bool) {

pterm.DefaultBasicText.Println()
pterm.Println()
message.HeaderInfof("📦 PACKAGE DEFINITION")
utils.ColorPrintYAML(p.cfg.Pkg, p.getPackageYAMLHints(stage), true)

Expand Down
2 changes: 1 addition & 1 deletion src/pkg/utils/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func ColorPrintYAML(data any, hints map[string]string, spaceRootLists bool) {
outputYAML = ansiRegex.ReplaceAllString(outputYAML, "")
}

pterm.DefaultBasicText.Println()
pterm.Println()
pterm.Println(outputYAML)
}

Expand Down

0 comments on commit 0f313fa

Please sign in to comment.