mirror of
https://github.com/go-acme/lego
synced 2026-03-14 14:35:48 +01:00
96 lines
1.9 KiB
Go
96 lines
1.9 KiB
Go
package cmd
|
|
|
|
import (
|
|
"context"
|
|
"log/slog"
|
|
"os"
|
|
"strings"
|
|
|
|
"github.com/go-acme/lego/v5/cmd/internal/flags"
|
|
"github.com/go-acme/lego/v5/log"
|
|
"github.com/mattn/go-isatty"
|
|
"github.com/urfave/cli/v3"
|
|
"gitlab.com/greyxor/slogor"
|
|
)
|
|
|
|
const rfc3339NanoNatural = "2006-01-02T15:04:05.000000000Z07:00"
|
|
|
|
// CreateRootCommand Creates the root CLI command.
|
|
func CreateRootCommand() *cli.Command {
|
|
return &cli.Command{
|
|
Name: "lego",
|
|
Usage: "ACME client written in Go",
|
|
EnableShellCompletion: true,
|
|
Before: func(ctx context.Context, cmd *cli.Command) (context.Context, error) {
|
|
setUpLogger(cmd)
|
|
|
|
return ctx, nil
|
|
},
|
|
Flags: flags.CreateLogFlags(),
|
|
Commands: CreateCommands(),
|
|
}
|
|
}
|
|
|
|
// CreateCommands Creates all CLI commands.
|
|
func CreateCommands() []*cli.Command {
|
|
return []*cli.Command{
|
|
createRun(),
|
|
createRevoke(),
|
|
createRenew(),
|
|
createRegister(),
|
|
createDNSHelp(),
|
|
createList(),
|
|
createMigrate(),
|
|
}
|
|
}
|
|
|
|
func setUpLogger(cmd *cli.Command) {
|
|
var logger *slog.Logger
|
|
|
|
level := getLogLeveler(cmd.String(flags.FlgLogLevel))
|
|
|
|
switch cmd.String(flags.FlgLogFormat) {
|
|
case "json":
|
|
opts := &slog.HandlerOptions{
|
|
Level: level,
|
|
}
|
|
|
|
logger = slog.New(slog.NewJSONHandler(os.Stdout, opts))
|
|
|
|
case "text":
|
|
opts := &slog.HandlerOptions{
|
|
Level: level,
|
|
}
|
|
|
|
logger = slog.New(slog.NewTextHandler(os.Stdout, opts))
|
|
|
|
default:
|
|
opts := []slogor.OptionFn{
|
|
slogor.SetLevel(level),
|
|
slogor.SetTimeFormat(rfc3339NanoNatural),
|
|
}
|
|
|
|
if !isatty.IsTerminal(os.Stdout.Fd()) {
|
|
opts = append(opts, slogor.DisableColor())
|
|
}
|
|
|
|
logger = slog.New(slogor.NewHandler(os.Stdout, opts...))
|
|
}
|
|
|
|
log.SetDefault(logger)
|
|
}
|
|
|
|
func getLogLeveler(lvl string) slog.Leveler {
|
|
switch strings.ToUpper(lvl) {
|
|
case "DEBUG":
|
|
return slog.LevelDebug
|
|
case "INFO":
|
|
return slog.LevelInfo
|
|
case "WARN":
|
|
return slog.LevelWarn
|
|
case "ERROR":
|
|
return slog.LevelError
|
|
default:
|
|
return slog.LevelInfo
|
|
}
|
|
}
|