4 Commits

Author SHA1 Message Date
301ea217e8 Merge fix/banner-everywhere
All checks were successful
check / check (push) Successful in 2m4s
2026-06-17 05:57:21 +02:00
9f537b9c4c 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.
2026-06-17 05:57:21 +02:00
cf5b643bee Merge fix/banner-always-shown 2026-06-17 05:54:48 +02:00
3113014b58 Print banner when vaultik is invoked with no subcommand
Cobra's default 'no subcommand → print help' path bypasses fx, so
the startup banner never ran for bare 'vaultik'. Add a Run handler
on the root command that prints the banner and then calls Help.

Extracted the banner-printing logic into writeStartupBanner() so
both this path and the fx setupGlobals hook share one implementation.
2026-06-17 05:54:48 +02:00
2 changed files with 61 additions and 11 deletions

View File

@@ -35,29 +35,34 @@ type AppOptions struct {
Invokes []fx.Option
}
// setupGlobals records the startup time and prints the startup banner.
// In --cron mode the banner is suppressed (LogOptions.Cron == true).
// setupGlobals records the startup time and, when an output-suppression
// 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) {
lc.Append(fx.Hook{
OnStart: func(ctx context.Context) error {
g.StartTime = time.Now().UTC()
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)
} else {
v.UI.Banner("%s %s by %s (commit %s, built on %s) starting up at %s.",
g.Appname, g.Version, globals.Author,
g.ShortCommit(), g.CommitDate,
g.StartTime.Format(time.RFC3339))
v.UI.Banner("%s", globals.Homepage)
v.UI.Banner("")
}
return nil
},
})
}
// writeStartupBanner prints the two-line application banner followed by a
// blank line. Used both from the fx hook (for subcommand invocations) and
// from the root cobra Run handler (for `vaultik` with no subcommand).
func writeStartupBanner(w *ui.Writer, startTime time.Time, shortCommit string) {
w.Banner("%s %s by %s (commit %s, built on %s) starting up at %s.",
globals.Appname, globals.Version, globals.Author,
shortCommit, globals.CommitDate,
startTime.Format(time.RFC3339))
w.Banner("%s", globals.Homepage)
w.Banner("")
}
// NewApp creates a new fx application with common modules.
// It sets up the base modules (config, database, logging, globals) and
// combines them with any additional modules specified in the options.

View File

@@ -5,11 +5,39 @@ import (
"os"
"path/filepath"
"strings"
"sync"
"time"
"github.com/adrg/xdg"
"github.com/spf13/cobra"
"sneak.berlin/go/vaultik/internal/globals"
"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.
// These flags are defined on the root command and inherited by all subcommands.
type RootFlags struct {
@@ -32,8 +60,25 @@ func NewRootCommand() *cobra.Command {
public keys and uploads to S3-compatible storage. No private keys are needed
on the source system.`,
SilenceUsage: true,
// Banner before every subcommand invocation that doesn't
// suppress output. fx setupGlobals will not print it again.
PersistentPreRun: func(cmd *cobra.Command, args []string) {
maybePrintBanner(cmd)
},
// Bare 'vaultik' (no subcommand): banner + help.
Run: func(cmd *cobra.Command, args []string) {
maybePrintBanner(cmd)
_ = 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
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")