Compare commits
4 Commits
706284d590
...
301ea217e8
| Author | SHA1 | Date | |
|---|---|---|---|
| 301ea217e8 | |||
| 9f537b9c4c | |||
| cf5b643bee | |||
| 3113014b58 |
@@ -35,29 +35,34 @@ 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 {
|
|
||||||
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
|
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.
|
// NewApp creates a new fx application with common modules.
|
||||||
// It sets up the base modules (config, database, logging, globals) and
|
// It sets up the base modules (config, database, logging, globals) and
|
||||||
// combines them with any additional modules specified in the options.
|
// combines them with any additional modules specified in the options.
|
||||||
|
|||||||
@@ -5,11 +5,39 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/adrg/xdg"
|
"github.com/adrg/xdg"
|
||||||
"github.com/spf13/cobra"
|
"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.
|
// 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 {
|
||||||
@@ -32,8 +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,
|
||||||
|
// 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
|
// 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