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 ( import (
"encoding/json" "encoding/json"

View file

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

View file

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

22
main.go
View file

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