Home

ugit @6b07a4651d82b44e5ac7d71eb1edacebb28ae094 - refs - log -
-
https://git.jolheiser.com/ugit.git
The code powering this h*ckin' site
ugit / cmd / ugitd / args.go
- raw -
 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package main

//go:generate go tool gen -schema schema.cue -go args.gen.go -nix ../../nix/options.gen.nix

import (
	_ "embed"
	"flag"
	"fmt"
	"log/slog"
	"strings"

	"github.com/peterbourgon/ff/v3"
	"go.jolheiser.com/ffjsonnet"
)

//go:embed schema.cue
var schema string

func parseArgs(args []string) (cliArgs, error) {
	fs := flag.NewFlagSet("ugitd", flag.ContinueOnError)
	fs.String("config", "ugit.jsonnet", "Path to config file")

	c := defaultArgs()
	registerFlags(fs, &c)

	parser := ffjsonnet.ParseConfig{Schema: schema}
	err := ff.Parse(fs, args,
		ff.WithEnvVarPrefix("UGIT"),
		ff.WithConfigFileFlag("config"),
		ff.WithAllowMissingConfigFile(true),
		ff.WithConfigFileParser(parser.Parse),
	)
	if err != nil {
		return c, err
	}

	return c, c.validate()
}

func parseLogLevel(s string) (slog.Level, error) {
	var lvl slog.Level
	switch strings.ToLower(s) {
	case "debug":
		lvl = slog.LevelDebug
	case "info":
		lvl = slog.LevelInfo
	case "warn", "warning":
		lvl = slog.LevelWarn
	case "error":
		lvl = slog.LevelError
	default:
		return -1, fmt.Errorf("unknown log level %q: options are [debug, info, warn, error]", s)
	}
	return lvl, nil
}

type profileLink struct {
	Name string
	URL  string
}

func parseProfileLink(s string) (profileLink, error) {
	parts := strings.SplitN(s, ",", 2)
	if len(parts) != 2 {
		return profileLink{}, fmt.Errorf("invalid profile link %q", s)
	}
	return profileLink{
		Name: parts[0],
		URL:  parts[1],
	}, nil
}