Mirror of https://git.jolheiser.com/ugit
fork

Configure Feed

Select the types of activity you want to include in your feed.

feat: multiple log levels and json logging

Signed-off-by: jolheiser <git@jolheiser.com>

+46 -18
+19 -2
cmd/ugitd/args.go
··· 5 5 "fmt" 6 6 "strings" 7 7 8 + "github.com/charmbracelet/log" 8 9 "github.com/peterbourgon/ff/v3" 9 10 "github.com/peterbourgon/ff/v3/ffyaml" 10 11 ) 11 12 12 13 type cliArgs struct { 13 - Debug bool 14 14 RepoDir string 15 15 SSH sshArgs 16 16 HTTP httpArgs 17 17 Meta metaArgs 18 18 Profile profileArgs 19 + Log logArgs 19 20 } 20 21 21 22 type sshArgs struct { ··· 46 47 URL string 47 48 } 48 49 50 + type logArgs struct { 51 + Level log.Level 52 + JSON bool 53 + } 54 + 49 55 func parseArgs(args []string) (c cliArgs, e error) { 50 56 fs := flag.NewFlagSet("ugitd", flag.ContinueOnError) 51 57 fs.String("config", "ugit.yaml", "Path to config file") ··· 65 71 Meta: metaArgs{ 66 72 Title: "ugit", 67 73 Description: "Minimal git server", 74 + }, 75 + Log: logArgs{ 76 + Level: log.InfoLevel, 68 77 }, 69 78 } 70 79 71 - fs.BoolVar(&c.Debug, "debug", c.Debug, "Debug logging") 80 + fs.Func("log.level", "Logging level", func(s string) error { 81 + lvl, err := log.ParseLevel(s) 82 + if err != nil { 83 + return err 84 + } 85 + c.Log.Level = lvl 86 + return nil 87 + }) 88 + fs.BoolVar(&c.Log.JSON, "log.json", c.Log.JSON, "Print logs in JSON(L) format") 72 89 fs.StringVar(&c.RepoDir, "repo-dir", c.RepoDir, "Path to directory containing repositories") 73 90 fs.StringVar(&c.SSH.AuthorizedKeys, "ssh.authorized-keys", c.SSH.AuthorizedKeys, "Path to authorized_keys") 74 91 fs.StringVar(&c.SSH.CloneURL, "ssh.clone-url", c.SSH.CloneURL, "SSH clone URL base")
+19 -9
cmd/ugitd/main.go
··· 4 4 "errors" 5 5 "flag" 6 6 "fmt" 7 + "log/slog" 7 8 "os" 8 9 "os/signal" 9 10 "path/filepath" 10 11 "strconv" 11 12 "strings" 12 13 14 + "github.com/charmbracelet/log" 15 + "github.com/go-chi/chi/v5/middleware" 16 + "github.com/go-chi/httplog/v2" 13 17 "github.com/go-git/go-git/v5/plumbing/protocol/packp" 18 + "github.com/go-git/go-git/v5/utils/trace" 14 19 "go.jolheiser.com/ugit/internal/git" 15 - 16 20 "go.jolheiser.com/ugit/internal/http" 17 21 "go.jolheiser.com/ugit/internal/ssh" 18 - 19 - "github.com/charmbracelet/log" 20 - "github.com/go-chi/chi/v5/middleware" 21 - "github.com/go-git/go-git/v5/utils/trace" 22 22 ) 23 23 24 24 func main() { ··· 39 39 panic(err) 40 40 } 41 41 42 - if args.Debug { 42 + log.SetLevel(args.Log.Level) 43 + middleware.DefaultLogger = httplog.RequestLogger(httplog.NewLogger("ugit", httplog.Options{ 44 + JSON: args.Log.JSON, 45 + LogLevel: slog.Level(args.Log.Level), 46 + Concise: args.Log.Level != log.DebugLevel, 47 + })) 48 + 49 + if args.Log.Level == log.DebugLevel { 43 50 trace.SetTarget(trace.Packet) 44 - log.SetLevel(log.DebugLevel) 45 51 } else { 46 52 middleware.DefaultLogger = http.NoopLogger 47 53 ssh.DefaultLogger = ssh.NoopLogger 48 54 } 49 55 56 + if args.Log.JSON { 57 + log.SetFormatter(log.JSONFormatter) 58 + } 59 + 50 60 if err := requiredFS(args.RepoDir); err != nil { 51 61 panic(err) 52 62 } ··· 63 73 panic(err) 64 74 } 65 75 go func() { 66 - fmt.Printf("SSH listening on ssh://localhost:%d\n", sshSettings.Port) 76 + log.Debugf("SSH listening on ssh://localhost:%d\n", sshSettings.Port) 67 77 if err := sshSrv.ListenAndServe(); err != nil { 68 78 panic(err) 69 79 } ··· 88 98 } 89 99 httpSrv := http.New(httpSettings) 90 100 go func() { 91 - fmt.Printf("HTTP listening on http://localhost:%d\n", httpSettings.Port) 101 + log.Debugf("HTTP listening on http://localhost:%d\n", httpSettings.Port) 92 102 if err := httpSrv.ListenAndServe(); err != nil { 93 103 panic(err) 94 104 }
+1 -6
flake.nix
··· 128 128 description = "Group account under which ugit runs"; 129 129 }; 130 130 131 - debug = mkOption { 132 - type = types.bool; 133 - default = false; 134 - }; 135 - 136 131 openFirewall = mkOption { 137 132 type = types.bool; 138 133 default = false; ··· 160 155 if (builtins.length cfg.authorizedKeys) > 0 161 156 then authorizedKeysFile 162 157 else cfg.authorizedKeysFile; 163 - args = ["--config=${configFile}" "--repo-dir=${cfg.repoDir}" "--ssh.authorized-keys=${authorizedKeysPath}" "--ssh.host-key=${cfg.hostKeyFile}"] ++ lib.optionals cfg.debug ["--debug"]; 158 + args = ["--config=${configFile}" "--repo-dir=${cfg.repoDir}" "--ssh.authorized-keys=${authorizedKeysPath}" "--ssh.host-key=${cfg.hostKeyFile}"]; 164 159 in "${cfg.package}/bin/ugitd ${builtins.concatStringsSep " " args}"; 165 160 wantedBy = ["multi-user.target"]; 166 161 after = ["network.target"];
+1
go.mod
··· 12 12 github.com/charmbracelet/wish v1.3.0 13 13 github.com/dustin/go-humanize v1.0.1 14 14 github.com/go-chi/chi/v5 v5.0.11 15 + github.com/go-chi/httplog/v2 v2.1.1 15 16 github.com/go-git/go-billy/v5 v5.5.0 16 17 github.com/go-git/go-git/v5 v5.11.0 17 18 github.com/peterbourgon/ff/v3 v3.4.0
+1 -1
go.mod.sri
··· 1 - sha256-8kI94hcJupAUye6cEAmIlN+CrtYSXlgoAlmpyXArfF8= 1 + sha256-m3s1SYLLrADRQc4a2njyu/xpvsHKcX/KGGzAay7DoIU=
+2
go.sum
··· 64 64 github.com/gliderlabs/ssh v0.3.5/go.mod h1:8XB4KraRrX39qHhT6yxPsHedjA08I/uBVwj4xC+/+z4= 65 65 github.com/go-chi/chi/v5 v5.0.11 h1:BnpYbFZ3T3S1WMpD79r7R5ThWX40TaFB7L31Y8xqSwA= 66 66 github.com/go-chi/chi/v5 v5.0.11/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= 67 + github.com/go-chi/httplog/v2 v2.1.1 h1:ojojiu4PIaoeJ/qAO4GWUxJqvYUTobeo7zmuHQJAxRk= 68 + github.com/go-chi/httplog/v2 v2.1.1/go.mod h1:/XXdxicJsp4BA5fapgIC3VuTD+z0Z/VzukoB3VDc1YE= 67 69 github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 h1:+zs/tPmkDkHx3U66DAb0lQFJrpS6731Oaa12ikc+DiI= 68 70 github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376/go.mod h1:an3vInlBmSxCcxctByoQdvwPiA7DTK7jaaFDBTtu0ic= 69 71 github.com/go-git/go-billy/v5 v5.5.0 h1:yEY4yhzCDuMGSv83oGxiBotRzhwhNr8VZyphhiu+mTU=
+3
gomod2nix.toml
··· 70 70 [mod."github.com/go-chi/chi/v5"] 71 71 version = "v5.0.11" 72 72 hash = "sha256-95LKg/OVzhik2HUz6cirHH3eAT4qbHSg52bSvkc+XOY=" 73 + [mod."github.com/go-chi/httplog/v2"] 74 + version = "v2.1.1" 75 + hash = "sha256-bMpoHUSNk3Uds9NfrStwhDsdCONR4pJso9sVUhqfidk=" 73 76 [mod."github.com/go-git/gcfg"] 74 77 version = "v1.5.1-0.20230307220236-3a3c6141e376" 75 78 hash = "sha256-f4k0gSYuo0/q3WOoTxl2eFaj7WZpdz29ih6CKc8Ude8="