2023-12-29 16:06:09 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
2024-01-15 22:26:51 +00:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
2023-12-29 16:06:09 +00:00
|
|
|
|
|
|
|
"github.com/peterbourgon/ff/v3"
|
2024-06-27 03:24:45 +00:00
|
|
|
"go.jolheiser.com/ffdhall"
|
2023-12-29 16:06:09 +00:00
|
|
|
)
|
|
|
|
|
2024-01-15 22:26:51 +00:00
|
|
|
type cliArgs struct {
|
|
|
|
Debug bool
|
|
|
|
RepoDir string
|
|
|
|
SSH sshArgs
|
|
|
|
HTTP httpArgs
|
|
|
|
Meta metaArgs
|
|
|
|
Profile profileArgs
|
2023-12-29 16:06:09 +00:00
|
|
|
}
|
|
|
|
|
2024-01-15 22:26:51 +00:00
|
|
|
type sshArgs struct {
|
|
|
|
AuthorizedKeys string
|
|
|
|
CloneURL string
|
|
|
|
Port int
|
|
|
|
HostKey string
|
|
|
|
}
|
|
|
|
|
|
|
|
type httpArgs struct {
|
|
|
|
CloneURL string
|
|
|
|
Port int
|
|
|
|
}
|
|
|
|
|
|
|
|
type metaArgs struct {
|
|
|
|
Title string
|
|
|
|
Description string
|
|
|
|
}
|
|
|
|
|
|
|
|
type profileArgs struct {
|
|
|
|
Username string
|
|
|
|
Email string
|
|
|
|
Links []profileLink
|
|
|
|
}
|
|
|
|
|
|
|
|
type profileLink struct {
|
|
|
|
Name string
|
|
|
|
URL string
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseArgs(args []string) (c cliArgs, e error) {
|
2023-12-29 16:06:09 +00:00
|
|
|
fs := flag.NewFlagSet("ugitd", flag.ContinueOnError)
|
2024-06-27 03:24:45 +00:00
|
|
|
fs.String("config", "ugit.dhall", "Path to config file")
|
2024-01-15 22:26:51 +00:00
|
|
|
|
|
|
|
c = cliArgs{
|
|
|
|
RepoDir: ".ugit",
|
|
|
|
SSH: sshArgs{
|
|
|
|
AuthorizedKeys: ".ssh/authorized_keys",
|
|
|
|
CloneURL: "ssh://localhost:8448",
|
|
|
|
Port: 8448,
|
|
|
|
HostKey: ".ssh/ugit_ed25519",
|
|
|
|
},
|
|
|
|
HTTP: httpArgs{
|
|
|
|
CloneURL: "http://localhost:8449",
|
|
|
|
Port: 8449,
|
|
|
|
},
|
|
|
|
Meta: metaArgs{
|
|
|
|
Title: "ugit",
|
|
|
|
Description: "Minimal git server",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
fs.BoolVar(&c.Debug, "debug", c.Debug, "Debug logging")
|
|
|
|
fs.StringVar(&c.RepoDir, "repo-dir", c.RepoDir, "Path to directory containing repositories")
|
|
|
|
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.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")
|
|
|
|
fs.Func("profile.links", "Link(s) for index page", func(s string) error {
|
|
|
|
parts := strings.SplitN(s, ",", 2)
|
|
|
|
if len(parts) != 2 {
|
|
|
|
return fmt.Errorf("invalid profile link %q", s)
|
|
|
|
}
|
|
|
|
c.Profile.Links = append(c.Profile.Links, profileLink{
|
|
|
|
Name: parts[0],
|
|
|
|
URL: parts[1],
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return c, ff.Parse(fs, args,
|
2023-12-29 16:06:09 +00:00
|
|
|
ff.WithEnvVarPrefix("UGIT"),
|
|
|
|
ff.WithConfigFileFlag("config"),
|
|
|
|
ff.WithAllowMissingConfigFile(true),
|
2024-06-27 03:24:45 +00:00
|
|
|
ff.WithConfigFileParser(ffdhall.DhallParser),
|
2023-12-29 16:06:09 +00:00
|
|
|
)
|
|
|
|
}
|