From 6efa5632041dee5ed41014a5324367d10dc76330 Mon Sep 17 00:00:00 2001 From: Miguel de la Cruz Date: Sat, 29 Jun 2024 22:19:25 +0200 Subject: [PATCH] Adds a proper CLI interface --- Makefile | 2 +- README.md | 6 +++--- cmd.go | 16 ++++++++++++++++ gitssg.go | 43 ++++++++++++++++++------------------------- go.mod | 5 ++++- go.sum | 8 ++++++++ 6 files changed, 50 insertions(+), 30 deletions(-) create mode 100644 cmd.go diff --git a/Makefile b/Makefile index fa28473..b71ee95 100644 --- a/Makefile +++ b/Makefile @@ -2,4 +2,4 @@ fmt: go fmt ./... build: - go build -o gitssg ./gitssg.go + go build -o gitssg ./... diff --git a/README.md b/README.md index 2aa6c06..3804b36 100644 --- a/README.md +++ b/README.md @@ -7,13 +7,13 @@ Project heavily inspired by the amazing ## Roadmap -- [ ] Embed templates for the index and repository pages. -- [ ] Generate the index html file for the -index subcommand. +- [X] Embed templates mechanism. +- [X] Generate the index html file for the `index` subcommand. - [ ] Generate the log html file for a repository. - [ ] Detect and link README, LICENSE and CONTRIBUTING files. - [ ] Generate the files html file and file structure. - [ ] Generate the refs html file. -- [ ] Add a proper CLI parsing and subcommands. +- [X] Add a proper CLI parsing and subcommands. - [ ] Add a sample CSS file for the default templates. - [ ] Add a subcommand to dump the embedded templates so they can be modified. diff --git a/cmd.go b/cmd.go new file mode 100644 index 0000000..a04f4b8 --- /dev/null +++ b/cmd.go @@ -0,0 +1,16 @@ +package main + +type indexCmd struct { + Paths []string `arg:"" help:"The paths to the repositories to include in the index."` +} + +type repoCmd struct { + Path string `arg:"" help:"The path to the repository."` +} + +var cli struct { + Debug bool `help:"Print debug information."` + + Index indexCmd `cmd:"" help:"Generate the repository index page."` + Repo repoCmd `cmd:"" help:"Generate the repository pages."` +} diff --git a/gitssg.go b/gitssg.go index fa1791c..69d14a8 100644 --- a/gitssg.go +++ b/gitssg.go @@ -12,6 +12,7 @@ import ( "strings" "time" + "github.com/alecthomas/kong" "github.com/go-git/go-git/v5" ) @@ -50,35 +51,27 @@ func errAndExit(msg string, args ...any) { os.Exit(1) } -func main() { - indexFlag := flag.Bool("index", false, "set to true if you want to generate the index") - repoFlag := flag.String("repo", "", "the repository path to generate the static files from") - debugFlag := flag.Bool("debug", false, "print debug information") - flag.Parse() +func (i *indexCmd) Run() error { + slog.Debug("Generating index", "args", flag.Args()) - if *debugFlag { + return generateIndex(i.Paths) +} + +func (r *repoCmd) Run() error { + slog.Debug("Generating repository", "path", r.Path) + + return generateRepo(r.Path) +} + +func main() { + ctx := kong.Parse(&cli) + if cli.Debug { slog.SetLogLoggerLevel(slog.LevelDebug) } - switch { - case *indexFlag: - if len(flag.Args()) == 0 { - errAndExit("Usage of -index:\n %s -index [repositories]\n", os.Args[0]) - } - - slog.Debug("Generating index", "args", flag.Args()) - - if err := generateIndex(flag.Args()); err != nil { - errAndExit("ERROR: %s\n", err) - } - case *repoFlag != "": - slog.Debug("Generating repository", "path", *repoFlag) - - if err := generateRepo(*repoFlag); err != nil { - errAndExit("ERROR: %s\n", err) - } - default: - flag.Usage() + if err := ctx.Run(); err != nil { + fmt.Fprintf(os.Stderr, "error: %s\n", err) + os.Exit(1) } } diff --git a/go.mod b/go.mod index f4ed354..89ac3a8 100644 --- a/go.mod +++ b/go.mod @@ -2,7 +2,10 @@ module git.ctrlz.es/gitssg go 1.22.3 -require github.com/go-git/go-git/v5 v5.12.0 +require ( + github.com/alecthomas/kong v0.9.0 + github.com/go-git/go-git/v5 v5.12.0 +) require ( dario.cat/mergo v1.0.0 // indirect diff --git a/go.sum b/go.sum index d7b8453..c1f4dd8 100644 --- a/go.sum +++ b/go.sum @@ -5,6 +5,12 @@ github.com/Microsoft/go-winio v0.6.1 h1:9/kr64B9VUZrLm5YYwbGtUJnMgqWVOdUAXu6Migc github.com/Microsoft/go-winio v0.6.1/go.mod h1:LRdKpFKfdobln8UmuiYcKPot9D2v6svN5+sAH+4kjUM= github.com/ProtonMail/go-crypto v1.0.0 h1:LRuvITjQWX+WIfr930YHG2HNfjR1uOfyf5vE0kC2U78= github.com/ProtonMail/go-crypto v1.0.0/go.mod h1:EjAoLdwvbIOoOQr3ihjnSoLZRtE8azugULFRteWMNc0= +github.com/alecthomas/assert/v2 v2.6.0 h1:o3WJwILtexrEUk3cUVal3oiQY2tfgr/FHWiz/v2n4FU= +github.com/alecthomas/assert/v2 v2.6.0/go.mod h1:Bze95FyfUr7x34QZrjL+XP+0qgp/zg8yS+TtBj1WA3k= +github.com/alecthomas/kong v0.9.0 h1:G5diXxc85KvoV2f0ZRVuMsi45IrBgx9zDNGNj165aPA= +github.com/alecthomas/kong v0.9.0/go.mod h1:Y47y5gKfHp1hDc7CH7OeXgLIpp+Q2m1Ni0L5s3bI8Os= +github.com/alecthomas/repr v0.4.0 h1:GhI2A8MACjfegCPVq9f1FLvIBS+DrQ2KQBFZP1iFzXc= +github.com/alecthomas/repr v0.4.0/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4= github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be h1:9AeTilPcZAjCFIImctFaOjnTIavg87rW78vTPkQqLI8= github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be/go.mod h1:ySMOLuWl6zY27l47sB3qLNK6tF2fkHG55UZxx8oIVo4= github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio= @@ -36,6 +42,8 @@ github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da h1:oI5xCqsCo564l github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM= +github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg= github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A= github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo= github.com/kevinburke/ssh_config v1.2.0 h1:x584FjTGwHzMwvHx18PXxbBVzfnxogHaAReU4gf13a4=