Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
store PTermWriter in message rather than relying on some Pterm info jank
Browse files Browse the repository at this point in the history
Signed-off-by: Kit Patella <kit@defenseunicorns.com>
mkcp committed Nov 8, 2024
1 parent 4a7f65b commit 500147c
Showing 5 changed files with 22 additions and 16 deletions.
3 changes: 1 addition & 2 deletions src/cmd/common/table.go
Original file line number Diff line number Diff line change
@@ -10,7 +10,6 @@ import (

"github.com/defenseunicorns/pkg/helpers/v2"
"github.com/fatih/color"
"github.com/pterm/pterm"
"github.com/zarf-dev/zarf/src/pkg/lint"
"github.com/zarf-dev/zarf/src/pkg/message"
)
@@ -48,7 +47,7 @@ func PrintFindings(lintErr *lint.LintError) {
// Print table to our OutputWriter
// HACK(mkcp): Setting a PTerm global isn't ideal or thread-safe. However, it lets us render even when message
// is disabled.
lastWriter := pterm.Info.Writer
lastWriter := *message.PTermWriter.Load()
message.InitializePTerm(OutputWriter)
message.Notef("Linting package %q at %s", findings[0].PackageNameOverride, packagePathFromUser)
message.Table([]string{"Type", "Path", "Message"}, lintData)
9 changes: 5 additions & 4 deletions src/cmd/package.go
Original file line number Diff line number Diff line change
@@ -16,7 +16,6 @@ import (

"github.com/AlecAivazis/survey/v2"
"github.com/defenseunicorns/pkg/helpers/v2"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"oras.land/oras-go/v2/registry"
@@ -278,10 +277,12 @@ var packageListCmd = &cobra.Command{

// NOTE(mkcp): Renders table with message.
header := []string{"Package", "Version", "Components"}
// HACK(mkcp): Setting a PTerm global isn't ideal or thread-safe. However, it lets us render even when message
// is disabled.
pterm.SetDefaultOutput(OutputWriter)
// HACK(mkcp): Re-initializing PTerm globally isn't ideal or thread-safe. However, it lets us render even when
// message is disabled.
lastWriter := *message.PTermWriter.Load()
message.InitializePTerm(OutputWriter)
message.Table(header, packageData)
message.InitializePTerm(lastWriter)

// Print out any unmarshalling errors
if err != nil {
3 changes: 1 addition & 2 deletions src/pkg/message/connect.go
Original file line number Diff line number Diff line change
@@ -8,7 +8,6 @@ import (
"fmt"
"os"

"github.com/pterm/pterm"
"github.com/zarf-dev/zarf/src/types"
)

@@ -24,7 +23,7 @@ func PrintConnectStringTable(connectStrings types.ConnectStrings) {

// HACK(mkcp): Setting a PTerm global during runtime isn't ideal or thread-safe. However, it lets us render
// even when message is disabled.
lastWriter := pterm.Info.Writer
lastWriter := *PTermWriter.Load()
InitializePTerm(os.Stdout)
// Create the table output with the data
header := []string{"Connect Command", "Description"}
17 changes: 12 additions & 5 deletions src/pkg/message/message.go
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@ import (
"io"
"os"
"strings"
"sync/atomic"
"time"

"github.com/defenseunicorns/pkg/helpers/v2"
@@ -45,6 +46,12 @@ var logLevel = InfoLevel
// logFile acts as a buffer for logFile generation
var logFile *PausableWriter

// PTermWriter is an unholy hack that allows us to retrieve the writer passed into InitializePTerm. Under no
// circumstances should this be considered stable or supported. It's only purpose is so we change the writer to
// Stdout for specific logs, then back to its original intended value that we set at the start of the command.
// It should be replaced by something threadsafe as soon as possible. Blame mkcp for this contraption.
var PTermWriter atomic.Pointer[io.Writer]

// DebugWriter represents a writer interface that writes to message.Debug
type DebugWriter struct{}

@@ -69,13 +76,13 @@ func InitializePTerm(w io.Writer) {
Text: " ERROR:",
Style: pterm.NewStyle(pterm.BgLightRed, pterm.FgBlack),
}

// HACK(mkcp): Unforgivable hack to set a writer directly on Info's prefixprinter so we can retrieve it elsewhere.
info := pterm.Info
info.Prefix = pterm.Prefix{
pterm.Info.Prefix = pterm.Prefix{
Text: " •",
}
pterm.Info = *info.WithWriter(w)

// HACK(mkcp): See the comments on the var above but this should not be used for anything other than its intended
// use and should be removed as soon as possible.
PTermWriter.Store(&w)

pterm.SetDefaultOutput(w)
}
6 changes: 3 additions & 3 deletions src/pkg/utils/yaml.go
Original file line number Diff line number Diff line change
@@ -103,9 +103,9 @@ func ColorPrintYAML(data any, hints map[string]string, spaceRootLists bool) erro
outputYAML = ansiRegex.ReplaceAllString(outputYAML, "")
}

// HACK(mkcp): Setting a PTerm global isn't ideal or thread-safe. However, it lets us render even when message
// is disabled.
lastWriter := pterm.Info.Writer
// HACK(mkcp): Re-initializing a PTerm global isn't ideal or thread-safe. However, it lets us render even when
// message is disabled.
lastWriter := *message.PTermWriter.Load()
message.InitializePTerm(os.Stdout)
pterm.Println()
pterm.Println(outputYAML)

0 comments on commit 500147c

Please sign in to comment.