Skip to content

Commit

Permalink
fixes logging
Browse files Browse the repository at this point in the history
  • Loading branch information
UncleGedd committed Mar 21, 2024
1 parent c8562c3 commit 4ec59eb
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 21 deletions.
3 changes: 0 additions & 3 deletions src/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,6 @@ var (

// HelmTimeout is the default timeout for helm deploys
HelmTimeout = 15 * time.Minute

// TeaEnabled is a flag to enable BubbleTea for select bundle ops as we refactor the TUI
TeaEnabled bool
)

// GetArch returns the arch based on a priority list with options for overriding.
Expand Down
2 changes: 0 additions & 2 deletions src/pkg/bundle/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (

"github.com/defenseunicorns/uds-cli/src/config"
"github.com/defenseunicorns/uds-cli/src/pkg/bundler/fetcher"
"github.com/defenseunicorns/uds-cli/src/pkg/utils"
"github.com/defenseunicorns/uds-cli/src/types"
"github.com/defenseunicorns/zarf/src/config/lang"
"github.com/defenseunicorns/zarf/src/pkg/cluster"
Expand Down Expand Up @@ -75,7 +74,6 @@ func NewOrDie(cfg *types.BundleConfig) *Bundle {

// ClearPaths closes any files and clears out the paths used by Bundle
func (b *Bundle) ClearPaths() {
utils.CacheLogFile.Close()
_ = os.RemoveAll(b.tmp)
}

Expand Down
2 changes: 2 additions & 0 deletions src/pkg/bundle/tui/deploy/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/defenseunicorns/uds-cli/src/config"
"github.com/defenseunicorns/uds-cli/src/pkg/bundle/tui"
"github.com/defenseunicorns/uds-cli/src/pkg/utils"
"github.com/defenseunicorns/zarf/src/pkg/message"
zarfTypes "github.com/defenseunicorns/zarf/src/types"
)

Expand Down Expand Up @@ -108,6 +109,7 @@ func (m *Model) handleDone(err error) tea.Cmd {
cmds = append(cmds, genSuccessOrFailCmds(m)...)
if err != nil {
hint := lightBlueText.Render("uds logs")
message.Debug(err) // capture err in debug logs
errMsg := lipgloss.NewStyle().Padding(0, 4).Render(fmt.Sprintf("\n❌ Error deploying bundle: %s\n\nRun %s to view deployment logs", lightGrayText.Render(err.Error()), hint) + "\n")
cmds = []tea.Cmd{tea.Println(errMsg), tui.Pause(), tea.Quit}
return tea.Sequence(cmds...)
Expand Down
31 changes: 15 additions & 16 deletions src/pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ import (
"github.com/pterm/pterm"
)

var (
CacheLogFile *os.File
)

// GracefulPanic in the event of a panic, attempt to reset the terminal using the 'reset' command.
func GracefulPanic() {
if r := recover(); r != nil {
Expand Down Expand Up @@ -58,45 +54,48 @@ func IsValidTarballPath(path string) bool {

// ConfigureLogs sets up the log file, log cache and output for the CLI
func ConfigureLogs(op string) error {
// don't configure UDS logs for vendored Zarf cmds
if op == "zarf COMMAND" {
return nil
}
writer, err := message.UseLogFile("")
logFile := writer
if err != nil {
return err

}
location := message.LogFileLocation()
config.LogFileName = location

// empty cache logs file
os.Remove(filepath.Join(config.CommonOptions.CachePath, config.CachedLogs))
tmpLogLocation := message.LogFileLocation()
config.LogFileName = tmpLogLocation

// Set up cache dir and cache logs file
cacheDir := filepath.Join(config.CommonOptions.CachePath)
if err := os.MkdirAll(cacheDir, 0o0755); err != nil { // Ensure the directory exists
return fmt.Errorf("failed to create cache directory: %w", err)
}
CacheLogFile, err = os.OpenFile(filepath.Join(config.CommonOptions.CachePath, config.CachedLogs), os.O_CREATE|os.O_WRONLY, 0o644)
if err != nil {

// remove old cache logs file, and set up symlink to the new log file
os.Remove(filepath.Join(config.CommonOptions.CachePath, config.CachedLogs))
if err = os.Symlink(tmpLogLocation, filepath.Join(config.CommonOptions.CachePath, config.CachedLogs)); err != nil {
return err
}
logWriter := io.MultiWriter(logFile, CacheLogFile)

logWriter := io.MultiWriter(logFile)

// use Zarf pterm output if no-tea flag is set
// todo: as more bundle ops use BubbleTea, need to also check them alongside 'deploy'
if !strings.Contains(op, "deploy") || config.CommonOptions.NoTea {
message.Notef("Saving log file to %s", location)
logWriter = io.MultiWriter(os.Stderr, CacheLogFile, logFile)
message.Notef("Saving log file to %s", tmpLogLocation)
logWriter = io.MultiWriter(os.Stderr, logFile)
pterm.SetDefaultOutput(logWriter)
return nil
}

// set pterm output to only go to this logfile
pterm.SetDefaultOutput(logWriter)

// disable progress bars (otherwise they will still get printed to STDERR)
message.NoProgress = true

message.Debugf(fmt.Sprintf("Saving log file to %s", location))
message.Debugf(fmt.Sprintf("Saving log file to %s", tmpLogLocation))
return nil
}

Expand Down

0 comments on commit 4ec59eb

Please sign in to comment.