Print startup banner on every invocation (except -q / --cron)
Adds maybePrintBanner() called from three cobra hooks: - PersistentPreRun on root: covers every subcommand invocation - Custom HelpFunc on root: covers --help and group-level help - Run on root: covers bare 'vaultik' with no subcommand bannerOnce sync.Once ensures the banner prints exactly once per process regardless of which hook(s) fire. Removed the duplicate banner-print from fx setupGlobals; that hook still handles the --cron/--quiet UI swap for the rest of the output.
This commit is contained in:
@@ -35,18 +35,16 @@ type AppOptions struct {
|
|||||||
Invokes []fx.Option
|
Invokes []fx.Option
|
||||||
}
|
}
|
||||||
|
|
||||||
// setupGlobals records the startup time and prints the startup banner.
|
// setupGlobals records the startup time and, when an output-suppression
|
||||||
// In --cron mode the banner is suppressed (LogOptions.Cron == true).
|
// flag is active, replaces the UI writer with a discarding one so no
|
||||||
|
// user-facing output is emitted. The startup banner itself is printed
|
||||||
|
// by the root command's PersistentPreRun (see maybePrintBanner).
|
||||||
func setupGlobals(lc fx.Lifecycle, g *globals.Globals, v *vaultik.Vaultik, opts log.LogOptions) {
|
func setupGlobals(lc fx.Lifecycle, g *globals.Globals, v *vaultik.Vaultik, opts log.LogOptions) {
|
||||||
lc.Append(fx.Hook{
|
lc.Append(fx.Hook{
|
||||||
OnStart: func(ctx context.Context) error {
|
OnStart: func(ctx context.Context) error {
|
||||||
g.StartTime = time.Now().UTC()
|
g.StartTime = time.Now().UTC()
|
||||||
if opts.Cron || opts.Quiet {
|
if opts.Cron || opts.Quiet {
|
||||||
// Replace UI writer with a discarding one so all
|
|
||||||
// user-facing output is suppressed.
|
|
||||||
v.UI = ui.NewWithColor(io.Discard, false)
|
v.UI = ui.NewWithColor(io.Discard, false)
|
||||||
} else {
|
|
||||||
writeStartupBanner(v.UI, g.StartTime, g.ShortCommit())
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/adrg/xdg"
|
"github.com/adrg/xdg"
|
||||||
@@ -13,6 +14,30 @@ import (
|
|||||||
"sneak.berlin/go/vaultik/internal/ui"
|
"sneak.berlin/go/vaultik/internal/ui"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// bannerOnce ensures the banner is printed at most once per process,
|
||||||
|
// even if multiple cobra hooks (PersistentPreRun, Help, Run) would
|
||||||
|
// otherwise each call maybePrintBanner.
|
||||||
|
var bannerOnce sync.Once
|
||||||
|
|
||||||
|
// maybePrintBanner prints the application banner unless an output-
|
||||||
|
// suppression flag is active. Safe to call multiple times — it prints
|
||||||
|
// at most once per process.
|
||||||
|
func maybePrintBanner(cmd *cobra.Command) {
|
||||||
|
if rootFlags.Quiet {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if cronFlag := cmd.Flags().Lookup("cron"); cronFlag != nil && cronFlag.Value.String() == "true" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
bannerOnce.Do(func() {
|
||||||
|
short := globals.Commit
|
||||||
|
if len(short) > 12 {
|
||||||
|
short = short[:12]
|
||||||
|
}
|
||||||
|
writeStartupBanner(ui.New(os.Stdout), time.Now().UTC(), short)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
// RootFlags holds global flags that apply to all commands.
|
// RootFlags holds global flags that apply to all commands.
|
||||||
// These flags are defined on the root command and inherited by all subcommands.
|
// These flags are defined on the root command and inherited by all subcommands.
|
||||||
type RootFlags struct {
|
type RootFlags struct {
|
||||||
@@ -35,20 +60,25 @@ func NewRootCommand() *cobra.Command {
|
|||||||
public keys and uploads to S3-compatible storage. No private keys are needed
|
public keys and uploads to S3-compatible storage. No private keys are needed
|
||||||
on the source system.`,
|
on the source system.`,
|
||||||
SilenceUsage: true,
|
SilenceUsage: true,
|
||||||
// When invoked with no subcommand, print the banner then the
|
// Banner before every subcommand invocation that doesn't
|
||||||
// usage/help. Cobra's default behavior (without a Run) just
|
// suppress output. fx setupGlobals will not print it again.
|
||||||
// prints help, which skips the banner.
|
PersistentPreRun: func(cmd *cobra.Command, args []string) {
|
||||||
|
maybePrintBanner(cmd)
|
||||||
|
},
|
||||||
|
// Bare 'vaultik' (no subcommand): banner + help.
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
startTime := time.Now().UTC()
|
maybePrintBanner(cmd)
|
||||||
short := globals.Commit
|
|
||||||
if len(short) > 12 {
|
|
||||||
short = short[:12]
|
|
||||||
}
|
|
||||||
writeStartupBanner(ui.New(os.Stdout), startTime, short)
|
|
||||||
_ = cmd.Help()
|
_ = cmd.Help()
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Help output (--help and group-level cmds) also gets the banner.
|
||||||
|
defaultHelp := cmd.HelpFunc()
|
||||||
|
cmd.SetHelpFunc(func(c *cobra.Command, args []string) {
|
||||||
|
maybePrintBanner(c)
|
||||||
|
defaultHelp(c, args)
|
||||||
|
})
|
||||||
|
|
||||||
// Add global flags
|
// Add global flags
|
||||||
cmd.PersistentFlags().StringVar(&rootFlags.ConfigPath, "config", "", "Path to config file (default: $VAULTIK_CONFIG or platform config dir)")
|
cmd.PersistentFlags().StringVar(&rootFlags.ConfigPath, "config", "", "Path to config file (default: $VAULTIK_CONFIG or platform config dir)")
|
||||||
cmd.PersistentFlags().BoolVarP(&rootFlags.Verbose, "verbose", "v", false, "Enable verbose output")
|
cmd.PersistentFlags().BoolVarP(&rootFlags.Verbose, "verbose", "v", false, "Enable verbose output")
|
||||||
|
|||||||
Reference in New Issue
Block a user