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

94 lines
1.9 KiB
Go
Raw Normal View History

package app
import (
"context"
"errors"
2024-02-03 16:38:31 +00:00
"io"
"log"
"os"
"github.com/peterbourgon/ff/v4"
"github.com/peterbourgon/ff/v4/ffhelp"
)
const (
2024-02-04 16:05:04 +00:00
appVersion = "0.3.2"
)
// struct for receivers to use common app pieces
type app struct {
2024-02-03 16:38:31 +00:00
stdLogger *log.Logger
debugLogger *log.Logger
errLogger *log.Logger
cmd *ff.Command
config *config
}
// actual application start
2024-02-02 23:35:21 +00:00
func Start(args []string) {
2024-02-03 16:38:31 +00:00
// make app w/ logger
app := &app{
2024-02-03 16:38:31 +00:00
stdLogger: log.New(os.Stdout, "", 0),
debugLogger: log.New(io.Discard, "", 0), // discard debug logging by default
errLogger: log.New(os.Stderr, "", 0),
}
// log start
2024-02-03 16:38:31 +00:00
app.stdLogger.Printf("apc-p15-tool v%s", appVersion)
2024-01-27 16:35:36 +00:00
2024-02-02 23:35:21 +00:00
// get os.Args if args unspecified in func
if args == nil {
args = os.Args
}
// get & parse config
2024-02-02 23:35:21 +00:00
err := app.getConfig(args)
2024-01-27 16:35:36 +00:00
2024-02-03 16:38:31 +00:00
// if debug logging, make real debug logger
2024-02-03 19:05:21 +00:00
if app.config.debugLogging != nil && *app.config.debugLogging {
app.debugLogger = log.New(os.Stdout, "debug: ", 0)
}
// deal with config err (after logger re-init)
2024-01-27 16:35:36 +00:00
if err != nil {
exitCode := 0
if errors.Is(err, ff.ErrHelp) {
// help explicitly requested
2024-02-03 16:38:31 +00:00
app.stdLogger.Printf("\n%s\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
exitCode = 1
2024-02-03 16:38:31 +00:00
app.errLogger.Print(err)
app.stdLogger.Printf("\n%s\n", ffhelp.Command(app.cmd))
} else {
// any other error
exitCode = 1
2024-02-03 16:38:31 +00:00
app.errLogger.Print(err)
}
os.Exit(exitCode)
}
// run it
exitCode := 0
err = app.cmd.Run(context.Background())
if err != nil {
exitCode = 1
2024-02-03 16:38:31 +00:00
app.errLogger.Print(err)
// if extra args, show help
if errors.Is(err, ErrExtraArgs) {
app.stdLogger.Printf("\n%s\n", ffhelp.Command(app.cmd))
}
2024-01-27 16:35:36 +00:00
}
2024-02-03 16:38:31 +00:00
app.stdLogger.Print("apc-p15-tool done")
os.Exit(exitCode)
}