Adds a proper CLI interface

This commit is contained in:
Miguel de la Cruz 2024-06-29 22:19:25 +02:00
parent 873ae748dc
commit 6efa563204
6 changed files with 50 additions and 30 deletions

View file

@ -2,4 +2,4 @@ fmt:
go fmt ./...
build:
go build -o gitssg ./gitssg.go
go build -o gitssg ./...

View file

@ -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.

16
cmd.go Normal file
View file

@ -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."`
}

View file

@ -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)
}
}

5
go.mod
View file

@ -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

8
go.sum
View file

@ -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=