fix: handle output redirect and NO_COLOR for 'uc caddy config'

This commit is contained in:
Pasha Sviderski
2026-10-28 20:24:16 +10:00
parent 5d640b9a57
commit 80d2a323c6
2 changed files with 4 additions and 12 deletions
+3 -10
View File
@@ -3,8 +3,8 @@ package caddy
import ( import (
"context" "context"
"fmt" "fmt"
"os"
"charm.land/lipgloss/v2"
"github.com/alecthomas/chroma/v2/quick" "github.com/alecthomas/chroma/v2/quick"
"github.com/psviderski/uncloud/internal/cli" "github.com/psviderski/uncloud/internal/cli"
"github.com/psviderski/uncloud/internal/cli/completion" "github.com/psviderski/uncloud/internal/cli/completion"
@@ -15,7 +15,6 @@ import (
type configOptions struct { type configOptions struct {
machine string machine string
noColor bool
} }
func NewConfigCommand() *cobra.Command { func NewConfigCommand() *cobra.Command {
@@ -33,8 +32,6 @@ func NewConfigCommand() *cobra.Command {
cmd.Flags().StringVarP(&opts.machine, "machine", "m", "", cmd.Flags().StringVarP(&opts.machine, "machine", "m", "",
"Name or ID of the machine to get the configuration from. (default is connected machine)") "Name or ID of the machine to get the configuration from. (default is connected machine)")
cmd.Flags().BoolVar(&opts.noColor, "no-color", false,
"Disable syntax highlighting for the output.")
completion.MachinesFlag(cmd) completion.MachinesFlag(cmd)
@@ -54,13 +51,9 @@ func runConfig(ctx context.Context, uncli *cli.CLI, opts configOptions) error {
} }
// Print the Caddyfile with syntax highlighting. // Print the Caddyfile with syntax highlighting.
if opts.noColor { if err = quick.Highlight(lipgloss.Writer, config.Caddyfile, "caddy", "terminal256", "monokai"); err != nil {
fmt.Print(config.Caddyfile)
} else {
if err = quick.Highlight(os.Stdout, config.Caddyfile, "caddy", "terminal256", "monokai"); err != nil {
// If highlighting fails, fall back to plain output. // If highlighting fails, fall back to plain output.
fmt.Print(config.Caddyfile) lipgloss.Print(config.Caddyfile)
}
} }
if config.LastReconciliationError != "" { if config.LastReconciliationError != "" {
@@ -15,7 +15,6 @@ uc caddy config [flags]
``` ```
-h, --help help for config -h, --help help for config
-m, --machine string Name or ID of the machine to get the configuration from. (default is connected machine) -m, --machine string Name or ID of the machine to get the configuration from. (default is connected machine)
--no-color Disable syntax highlighting for the output.
``` ```
## Options inherited from parent commands ## Options inherited from parent commands