2023-01-29 10:54:56 +00:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
|
2024-07-03 14:12:13 +00:00
|
|
|
_ "github.com/urfave/cli-docs/v3" // required for `go generate` to work
|
|
|
|
"github.com/urfave/cli/v3"
|
2023-01-29 10:54:56 +00:00
|
|
|
|
2024-07-03 14:12:13 +00:00
|
|
|
"gh.tarampamp.am/error-pages/internal/appmeta"
|
2023-02-23 17:49:45 +00:00
|
|
|
"gh.tarampamp.am/error-pages/internal/cli/build"
|
|
|
|
"gh.tarampamp.am/error-pages/internal/cli/healthcheck"
|
2024-07-03 14:12:13 +00:00
|
|
|
"gh.tarampamp.am/error-pages/internal/cli/perftest"
|
2023-02-23 17:49:45 +00:00
|
|
|
"gh.tarampamp.am/error-pages/internal/cli/serve"
|
|
|
|
"gh.tarampamp.am/error-pages/internal/logger"
|
2023-01-29 10:54:56 +00:00
|
|
|
)
|
|
|
|
|
2024-07-03 14:12:13 +00:00
|
|
|
//go:generate go run update_readme.go
|
2023-01-29 10:54:56 +00:00
|
|
|
|
2024-07-03 14:12:13 +00:00
|
|
|
// NewApp creates a new console application.
|
|
|
|
func NewApp(appName string) *cli.Command { //nolint:funlen
|
|
|
|
var (
|
|
|
|
logLevelFlag = cli.StringFlag{
|
|
|
|
Name: "log-level",
|
|
|
|
Value: logger.InfoLevel.String(),
|
|
|
|
Usage: "Logging level (" + strings.Join(logger.LevelStrings(), "/") + ")",
|
|
|
|
Sources: cli.EnvVars("LOG_LEVEL"),
|
|
|
|
OnlyOnce: true,
|
|
|
|
Config: cli.StringConfig{TrimSpace: true},
|
|
|
|
Validator: func(s string) error {
|
|
|
|
if _, err := logger.ParseLevel(s); err != nil {
|
2023-01-29 10:54:56 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-07-03 14:12:13 +00:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
logFormatFlag = cli.StringFlag{
|
|
|
|
Name: "log-format",
|
|
|
|
Value: logger.ConsoleFormat.String(),
|
|
|
|
Usage: "Logging format (" + strings.Join(logger.FormatStrings(), "/") + ")",
|
|
|
|
Sources: cli.EnvVars("LOG_FORMAT"),
|
|
|
|
OnlyOnce: true,
|
|
|
|
Config: cli.StringConfig{TrimSpace: true},
|
|
|
|
Validator: func(s string) error {
|
|
|
|
if _, err := logger.ParseFormat(s); err != nil {
|
2023-01-29 10:54:56 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-07-03 14:12:13 +00:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
// create a "default" logger (will be swapped later with customized)
|
|
|
|
var log, _ = logger.New(logger.InfoLevel, logger.ConsoleFormat) // error will never occur
|
|
|
|
|
|
|
|
return &cli.Command{
|
|
|
|
Usage: appName,
|
|
|
|
Suggest: true,
|
|
|
|
Before: func(ctx context.Context, c *cli.Command) error {
|
|
|
|
var (
|
|
|
|
logLevel, _ = logger.ParseLevel(c.String(logLevelFlag.Name)) // error ignored because the flag validates itself
|
|
|
|
logFormat, _ = logger.ParseFormat(c.String(logFormatFlag.Name)) // --//--
|
|
|
|
)
|
|
|
|
|
|
|
|
configured, err := logger.New(logLevel, logFormat) // create a new logger instance
|
2023-01-29 10:54:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-07-03 14:12:13 +00:00
|
|
|
*log = *configured // swap the "default" logger with customized
|
2023-01-29 10:54:56 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
Commands: []*cli.Command{
|
|
|
|
serve.NewCommand(log),
|
2024-07-03 14:12:13 +00:00
|
|
|
build.NewCommand(log),
|
|
|
|
healthcheck.NewCommand(log, healthcheck.NewHTTPHealthChecker()),
|
|
|
|
perftest.NewCommand(),
|
2023-01-29 10:54:56 +00:00
|
|
|
},
|
2024-07-03 14:12:13 +00:00
|
|
|
Version: fmt.Sprintf("%s (%s)", appmeta.Version(), runtime.Version()),
|
2023-01-29 10:54:56 +00:00
|
|
|
Flags: []cli.Flag{ // global flags
|
2024-07-03 14:12:13 +00:00
|
|
|
&logLevelFlag,
|
|
|
|
&logFormatFlag,
|
2023-01-29 10:54:56 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|