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.
This commit is contained in:
2026-06-17 05:54:48 +02:00
parent 706284d590
commit 3113014b58
2 changed files with 28 additions and 6 deletions

View File

@@ -46,18 +46,25 @@ func setupGlobals(lc fx.Lifecycle, g *globals.Globals, v *vaultik.Vaultik, opts
// 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("")
writeStartupBanner(v.UI, g.StartTime, g.ShortCommit())
}
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,9 +5,12 @@ import (
"os"
"path/filepath"
"strings"
"time"
"github.com/adrg/xdg"
"github.com/spf13/cobra"
"sneak.berlin/go/vaultik/internal/globals"
"sneak.berlin/go/vaultik/internal/ui"
)
// RootFlags holds global flags that apply to all commands.
@@ -32,6 +35,18 @@ func NewRootCommand() *cobra.Command {
public keys and uploads to S3-compatible storage. No private keys are needed
on the source system.`,
SilenceUsage: true,
// When invoked with no subcommand, print the banner then the
// usage/help. Cobra's default behavior (without a Run) just
// prints help, which skips the banner.
Run: func(cmd *cobra.Command, args []string) {
startTime := time.Now().UTC()
short := globals.Commit
if len(short) > 12 {
short = short[:12]
}
writeStartupBanner(ui.New(os.Stdout), startTime, short)
_ = cmd.Help()
},
}
// Add global flags