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 (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
|
@ -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
|
|
@ -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
22
main.go
|
@ -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]
|
||||||
|
|
Loading…
Reference in a new issue