refactor: move the subcommands to internal

Move the subcommands and FlagSets to a new internal package.
This commit is contained in:
Dan Anglin 2023-08-12 10:26:23 +01:00
parent 54d5fa1831
commit 2c5c7332be
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638
6 changed files with 33 additions and 18 deletions

View file

@ -1,4 +1,4 @@
package main
package cmd
import (
"encoding/json"

View file

@ -1,6 +1,7 @@
package main
package cmd
import (
"embed"
"flag"
"fmt"
"io"
@ -15,6 +16,9 @@ import (
tf "codeflow.dananglin.me.uk/apollo/spruce/internal/templateFuncs"
)
//go:embed templates/tex/*
var templates embed.FS
type GenerateCommand struct {
*flag.FlagSet
input string

View file

@ -1,4 +1,4 @@
package main
package cmd
import (
"flag"
@ -7,19 +7,20 @@ import (
type VersionCommand struct {
*flag.FlagSet
fullVersion bool
}
var (
fullVersion bool
binaryVersion string
buildTime string
goVersion string
gitCommit string
)
}
func NewVersionCommand() *VersionCommand {
func NewVersionCommand(binaryVersion, buildTime, goVersion, gitCommit string) *VersionCommand {
vc := VersionCommand{
FlagSet: flag.NewFlagSet("version", flag.ExitOnError),
FlagSet: flag.NewFlagSet("version", flag.ExitOnError),
binaryVersion: binaryVersion,
buildTime: buildTime,
goVersion: goVersion,
gitCommit: gitCommit,
}
vc.BoolVar(&vc.fullVersion, "full", false, "prints the full version")
@ -37,9 +38,9 @@ func (v *VersionCommand) Run() error {
Build date: %s
`
version = fmt.Sprintf(fullVersionFmt, binaryVersion, gitCommit, goVersion, buildTime)
version = fmt.Sprintf(fullVersionFmt, v.binaryVersion, v.gitCommit, v.goVersion, v.buildTime)
} else {
version = binaryVersion + "\n"
version = v.binaryVersion + "\n"
}
fmt.Print(version)

22
main.go
View file

@ -1,9 +1,10 @@
package main
import (
"embed"
"log"
"os"
"codeflow.dananglin.me.uk/apollo/spruce/internal/cmd"
)
type Runner interface {
@ -12,14 +13,23 @@ type Runner interface {
Run() error
}
//go:embed templates/tex/*
var templates embed.FS
var (
binaryVersion string
buildTime string
goVersion string
gitCommit string
)
func main() {
commandMap := map[string]Runner{
"version": NewVersionCommand(),
"generate": NewGenerateCommand(),
"create": NewCreateCommand(),
"version": cmd.NewVersionCommand(
binaryVersion,
buildTime,
goVersion,
gitCommit,
),
"generate": cmd.NewGenerateCommand(),
"create": cmd.NewCreateCommand(),
}
subcommand := os.Args[1]