diff --git a/cmd/install_only/main.go b/cmd/install_only/main.go new file mode 100644 index 0000000..f646f08 --- /dev/null +++ b/cmd/install_only/main.go @@ -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) +} diff --git a/cmd/main.go b/cmd/tool/main.go similarity index 59% rename from cmd/main.go rename to cmd/tool/main.go index 94e0688..bee8065 100644 --- a/cmd/main.go +++ b/cmd/tool/main.go @@ -2,6 +2,7 @@ package main import "apc-p15-tool/pkg/app" +// main command line tool func main() { - app.Start() + app.Start(nil) } diff --git a/pkg/app/app.go b/pkg/app/app.go index fa1d682..6054cf2 100644 --- a/pkg/app/app.go +++ b/pkg/app/app.go @@ -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()) diff --git a/pkg/app/config.go b/pkg/app/config.go index c8d5dab..40feb41 100644 --- a/pkg/app/config.go +++ b/pkg/app/config.go @@ -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 }