apc-p15-tool/pkg/app/app.go

74 lines
1.4 KiB
Go
Raw Normal View History

package app
import (
"context"
"errors"
"os"
"github.com/peterbourgon/ff/v4"
"github.com/peterbourgon/ff/v4/ffhelp"
"go.uber.org/zap"
)
const (
appVersion = "0.1.0"
environmentVarPrefix = "APC_P15_TOOL"
)
// struct for receivers to use common app pieces
type app struct {
logger *zap.SugaredLogger
cmd *ff.Command
config *config
}
// actual application start
func Start() {
// make app w/ initial logger pre-config
initLogLevel := "debug"
app := &app{
logger: makeZapLogger(&initLogLevel),
}
// get config
app.getConfig()
// re-init logger with configured log level
app.logger = makeZapLogger(app.config.logLevel)
// log start
app.logger.Infof("apc-p15-tool v%s", appVersion)
2024-01-27 16:35:36 +00:00
// get config
app.getConfig()
2024-01-27 16:35:36 +00:00
// run it
exitCode := 0
err := app.cmd.ParseAndRun(context.Background(), os.Args[1:], ff.WithEnvVarPrefix(environmentVarPrefix))
2024-01-27 16:35:36 +00:00
if err != nil {
exitCode = 1
if errors.Is(err, ff.ErrHelp) {
// help explicitly requested
exitCode = 0
app.logger.Info("\n\n", ffhelp.Command(app.cmd))
} else if errors.Is(err, ff.ErrDuplicateFlag) ||
errors.Is(err, ff.ErrUnknownFlag) ||
2024-02-02 23:35:20 +00:00
errors.Is(err, ff.ErrNoExec) ||
errors.Is(err, ErrExtraArgs) {
// other error that suggests user needs to see help
app.logger.Error(err)
app.logger.Info("\n\n", ffhelp.Command(app.cmd))
} else {
// any other error
app.logger.Error(err)
}
2024-01-27 16:35:36 +00:00
}
app.logger.Info("apc-p15-tool done")
os.Exit(exitCode)
}