refactor: move the subcommands to internal
Move the subcommands and FlagSets to a new internal package.
This commit is contained in:
parent
54d5fa1831
commit
2c5c7332be
6 changed files with 33 additions and 18 deletions
|
@ -1,4 +1,4 @@
|
|||
package main
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"encoding/json"
|
|
@ -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
|
|
@ -1,4 +1,4 @@
|
|||
package main
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"flag"
|
||||
|
@ -8,18 +8,19 @@ import (
|
|||
type VersionCommand struct {
|
||||
*flag.FlagSet
|
||||
fullVersion bool
|
||||
}
|
||||
|
||||
var (
|
||||
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),
|
||||
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
22
main.go
|
@ -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]
|
||||
|
|
Loading…
Reference in a new issue