~bigbes/ci-cacher

ref: 969662b88d348767a794537fc8aadc2255abfa76 ci-cacher/cmd/root.go -rw-r--r-- 1.2 KiB
969662b8 — Eugene Blikh Add goreleaser + CHANGELOG.md; cross-platform downloads on site 2 days 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
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",
	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)")
}