Home

ugit @c4ca33ae7dea59f56da4008556190fa72af97daa - refs - log -
-
https://git.jolheiser.com/ugit.git
The code powering this h*ckin' site
ugit / cmd / ugitd / args.gen.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
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Code generated by gen from schema.cue; DO NOT EDIT.

package main

import (
	"errors"
	"flag"
	"fmt"
	"log/slog"
	"regexp"
)

var (
	reURL = regexp.MustCompile("^[a-z][a-z0-9+.-]*://")
)

type cliArgs struct {
	// Path to directory containing repositories
	RepoDir string
	SSH     sshArgs
	HTTP    httpArgs
	Meta    metaArgs
	Profile profileArgs
	Log     logArgs
	// Show private repos in web interface
	ShowPrivate bool
}

type sshArgs struct {
	// Enable SSH server
	Enable bool
	// Path to authorized_keys
	AuthorizedKeys string
	// SSH clone URL base
	CloneURL string
	// SSH port
	Port int
	// SSH host key (created if it doesn't exist)
	HostKey string
}

type httpArgs struct {
	// Enable HTTP server
	Enable bool
	// HTTP clone URL base
	CloneURL string
	// HTTP port
	Port int
}

type metaArgs struct {
	// App title
	Title string
	// App description
	Description string
}

type profileArgs struct {
	// Username for index page
	Username string
	// Email for index page
	Email string
	// Link(s) for index page
	Links []profileLink
}

type logArgs struct {
	// Logging level
	Level slog.Level
	// Print logs in JSON(L) format
	JSON bool
}

// defaultArgs returns the defaults from schema.cue.
func defaultArgs() cliArgs {
	return cliArgs{
		RepoDir: ".ugit",
		SSH: sshArgs{
			Enable:         true,
			AuthorizedKeys: ".ssh/authorized_keys",
			CloneURL:       "ssh://localhost:8448",
			Port:           8448,
			HostKey:        ".ssh/ugit_ed25519",
		},
		HTTP: httpArgs{
			Enable:   true,
			CloneURL: "http://localhost:8449",
			Port:     8449,
		},
		Meta: metaArgs{
			Title:       "ugit",
			Description: "Minimal git server",
		},
		Log: logArgs{
			Level: mustParse(parseLogLevel, "error"),
		},
	}
}

// registerFlags defines a flag for every field of c, using c's current
// values as flag defaults.
func registerFlags(fs *flag.FlagSet, c *cliArgs) {
	fs.StringVar(&c.RepoDir, "repo-dir", c.RepoDir, "Path to directory containing repositories")
	fs.BoolVar(&c.SSH.Enable, "ssh.enable", c.SSH.Enable, "Enable SSH server")
	fs.StringVar(&c.SSH.AuthorizedKeys, "ssh.authorized-keys", c.SSH.AuthorizedKeys, "Path to authorized_keys")
	fs.StringVar(&c.SSH.CloneURL, "ssh.clone-url", c.SSH.CloneURL, "SSH clone URL base")
	fs.IntVar(&c.SSH.Port, "ssh.port", c.SSH.Port, "SSH port")
	fs.StringVar(&c.SSH.HostKey, "ssh.host-key", c.SSH.HostKey, "SSH host key (created if it doesn't exist)")
	fs.BoolVar(&c.HTTP.Enable, "http.enable", c.HTTP.Enable, "Enable HTTP server")
	fs.StringVar(&c.HTTP.CloneURL, "http.clone-url", c.HTTP.CloneURL, "HTTP clone URL base")
	fs.IntVar(&c.HTTP.Port, "http.port", c.HTTP.Port, "HTTP port")
	fs.StringVar(&c.Meta.Title, "meta.title", c.Meta.Title, "App title")
	fs.StringVar(&c.Meta.Description, "meta.description", c.Meta.Description, "App description")
	fs.StringVar(&c.Profile.Username, "profile.username", c.Profile.Username, "Username for index page")
	fs.StringVar(&c.Profile.Email, "profile.email", c.Profile.Email, "Email for index page")
	{
		set := false
		fs.Func("profile.links", "Link(s) for index page (repeatable)", func(s string) error {
			v, err := parseProfileLink(s)
			if err != nil {
				return err
			}
			if !set {
				c.Profile.Links, set = nil, true
			}
			c.Profile.Links = append(c.Profile.Links, v)
			return nil
		})
	}
	fs.Func("log.level", "Logging level (one of: debug, info, warn, warning, error; default: error)", func(s string) error {
		v, err := parseLogLevel(s)
		if err != nil {
			return err
		}
		c.Log.Level = v
		return nil
	})
	fs.BoolVar(&c.Log.JSON, "log.json", c.Log.JSON, "Print logs in JSON(L) format")
	fs.BoolVar(&c.ShowPrivate, "show-private", c.ShowPrivate, "Show private repos in web interface")
}

// validate checks c against the constraints in schema.cue.
func (c *cliArgs) validate() error {
	var errs []error
	if c.RepoDir == "" {
		errs = append(errs, errors.New("repo-dir: must not be empty"))
	}
	if c.SSH.AuthorizedKeys == "" {
		errs = append(errs, errors.New("ssh.authorized-keys: must not be empty"))
	}
	if !reURL.MatchString(c.SSH.CloneURL) {
		errs = append(errs, fmt.Errorf("ssh.clone-url: %q does not match %s", c.SSH.CloneURL, reURL))
	}
	if c.SSH.Port < 1 {
		errs = append(errs, fmt.Errorf("ssh.port: must be >= 1, got %v", c.SSH.Port))
	}
	if c.SSH.Port > 65535 {
		errs = append(errs, fmt.Errorf("ssh.port: must be <= 65535, got %v", c.SSH.Port))
	}
	if c.SSH.HostKey == "" {
		errs = append(errs, errors.New("ssh.host-key: must not be empty"))
	}
	if !reURL.MatchString(c.HTTP.CloneURL) {
		errs = append(errs, fmt.Errorf("http.clone-url: %q does not match %s", c.HTTP.CloneURL, reURL))
	}
	if c.HTTP.Port < 1 {
		errs = append(errs, fmt.Errorf("http.port: must be >= 1, got %v", c.HTTP.Port))
	}
	if c.HTTP.Port > 65535 {
		errs = append(errs, fmt.Errorf("http.port: must be <= 65535, got %v", c.HTTP.Port))
	}
	return errors.Join(errs...)
}

// mustParse applies a hand-written parse hook to a schema default. gen has
// already checked the default against the schema, so a failure here means
// the hook and the schema disagree.
func mustParse[T any](parse func(string) (T, error), s string) T {
	v, err := parse(s)
	if err != nil {
		panic(fmt.Sprintf("schema default %q rejected by parse hook: %v", s, err))
	}
	return v
}