~bigbes/ci-cacher

ref: bca3f572b28a6f4a945d92c2020b981af4cf58b4 ci-cacher/cmd/root.go -rw-r--r-- 1.4 KiB
bca3f572 — Eugene Blikh README: refresh for v0.1.1+v0.1.2 a day ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package cmd

import (
	"fmt"
	"log/slog"
	"os"

	"github.com/spf13/cobra"

	"go.bigb.es/cacher/internal/logging"
)

var (
	flagLogLevel   string
	flagLogFormat  string
	flagConfigPath string
)

var rootCmd = &cobra.Command{
	Use:   "cacher",
	Short: "S3-backed CI cache helper for builds.sr.ht — replaces .builds/lib/ci-lib.sh",
	// Subcommands fail in normal, scriptable ways (cache miss, key absent).
	// Dumping the cobra usage block on every such error is noise — the
	// short error line is enough.
	SilenceUsage: true,
	PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
		level := slog.LevelInfo
		switch flagLogLevel {
		case "debug":
			level = slog.LevelDebug
		case "info":
			level = slog.LevelInfo
		case "warn":
			level = slog.LevelWarn
		case "error":
			level = slog.LevelError
		default:
			return fmt.Errorf("invalid --log-level %q (want debug|info|warn|error)", flagLogLevel)
		}
		logging.Setup(flagLogFormat, os.Stderr, level)
		return nil
	},
}

func Execute() {
	if err := rootCmd.Execute(); err != nil {
		os.Exit(exitCodeFor(err))
	}
}

func init() {
	rootCmd.PersistentFlags().StringVar(&flagLogLevel, "log-level", "info", "log level (debug|info|warn|error)")
	rootCmd.PersistentFlags().StringVar(&flagLogFormat, "log-format", "human", "log format (human|json)")
	rootCmd.PersistentFlags().StringVar(&flagConfigPath, "config", "", "config file (default ~/.config/cacher/config.toml)")
}