add install only version

This commit is contained in:
Greg T. Wallace 2024-02-02 18:35:21 -05:00
parent a089d12c87
commit d6bb512b3e
4 changed files with 26 additions and 9 deletions

15
cmd/install_only/main.go Normal file
View file

@ -0,0 +1,15 @@
package main
import (
"apc-p15-tool/pkg/app"
"os"
)
// a version of the tool that always calls the `install` subcommand
func main() {
// insert install command
args := []string{os.Args[0], "install"}
args = append(args, os.Args[1:]...)
app.Start(args)
}

View file

@ -2,6 +2,7 @@ package main
import "apc-p15-tool/pkg/app"
// main command line tool
func main() {
app.Start()
app.Start(nil)
}

View file

@ -24,7 +24,7 @@ type app struct {
}
// actual application start
func Start() {
func Start(args []string) {
// make app w/ initial logger pre-config
initLogLevel := "debug"
app := &app{
@ -34,8 +34,13 @@ func Start() {
// log start
app.logger.Infof("apc-p15-tool v%s", appVersion)
// get os.Args if args unspecified in func
if args == nil {
args = os.Args
}
// get & parse config
err := app.getConfig()
err := app.getConfig(args)
// re-init logger with configured log level
app.logger = makeZapLogger(app.config.logLevel)
@ -66,9 +71,6 @@ func Start() {
os.Exit(exitCode)
}
// get config
app.getConfig()
// run it
exitCode := 0
err = app.cmd.Run(context.Background())

View file

@ -2,7 +2,6 @@ package app
import (
"errors"
"os"
"github.com/peterbourgon/ff/v4"
)
@ -31,7 +30,7 @@ type config struct {
// getConfig returns the app's configuration from either command line args,
// or environment variables
func (app *app) getConfig() error {
func (app *app) getConfig(args []string) error {
// make config
cfg := &config{}
@ -93,7 +92,7 @@ func (app *app) getConfig() error {
// set cfg & parse
app.config = cfg
app.cmd = rootCmd
err := app.cmd.Parse(os.Args[1:], ff.WithEnvVarPrefix(environmentVarPrefix))
err := app.cmd.Parse(args[1:], ff.WithEnvVarPrefix(environmentVarPrefix))
if err != nil {
return err
}