From 9ff5338937c77f94d7243bdd940d830b00d7f0ed Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Thu, 22 Jan 2026 19:20:08 +0100 Subject: [PATCH] chore: remove global before --- cmd/cmd_list.go | 9 +++++++-- cmd/cmd_renew.go | 2 ++ cmd/cmd_revoke.go | 9 +++++++-- cmd/cmd_run.go | 2 ++ cmd/{cmd_before.go => flags_validation.go} | 13 +++++++++---- cmd/lego/main.go | 1 - 6 files changed, 27 insertions(+), 9 deletions(-) rename cmd/{cmd_before.go => flags_validation.go} (73%) diff --git a/cmd/cmd_list.go b/cmd/cmd_list.go index f66dc6e7a..ffa8c956c 100644 --- a/cmd/cmd_list.go +++ b/cmd/cmd_list.go @@ -20,8 +20,13 @@ const ( func createList() *cli.Command { return &cli.Command{ - Name: "list", - Usage: "Display certificates and accounts information.", + Name: "list", + Usage: "Display certificates and accounts information.", + Before: func(ctx context.Context, cmd *cli.Command) (context.Context, error) { + validatePathFlag(cmd) + + return ctx, nil + }, Action: list, Flags: []cli.Flag{ &cli.BoolFlag{ diff --git a/cmd/cmd_renew.go b/cmd/cmd_renew.go index c477a9bd1..0b715b6af 100644 --- a/cmd/cmd_renew.go +++ b/cmd/cmd_renew.go @@ -42,6 +42,8 @@ func createRenew() *cli.Command { Usage: "Renew a certificate", Action: renew, Before: func(ctx context.Context, cmd *cli.Command) (context.Context, error) { + validateFlags(cmd) + // we require either domains or csr, but not both hasDomains := len(cmd.StringSlice(flgDomains)) > 0 diff --git a/cmd/cmd_revoke.go b/cmd/cmd_revoke.go index 13eff52d4..af41f3c03 100644 --- a/cmd/cmd_revoke.go +++ b/cmd/cmd_revoke.go @@ -17,8 +17,13 @@ const ( func createRevoke() *cli.Command { return &cli.Command{ - Name: "revoke", - Usage: "Revoke a certificate", + Name: "revoke", + Usage: "Revoke a certificate", + Before: func(ctx context.Context, cmd *cli.Command) (context.Context, error) { + validateFlags(cmd) + + return ctx, nil + }, Action: revoke, Flags: []cli.Flag{ &cli.BoolFlag{ diff --git a/cmd/cmd_run.go b/cmd/cmd_run.go index 4b89fdc9f..75c34e13d 100644 --- a/cmd/cmd_run.go +++ b/cmd/cmd_run.go @@ -35,6 +35,8 @@ func createRun() *cli.Command { Name: "run", Usage: "Register an account, then create and install a certificate", Before: func(ctx context.Context, cmd *cli.Command) (context.Context, error) { + validateFlags(cmd) + // we require either domains or csr, but not both hasDomains := len(cmd.StringSlice(flgDomains)) > 0 diff --git a/cmd/cmd_before.go b/cmd/flags_validation.go similarity index 73% rename from cmd/cmd_before.go rename to cmd/flags_validation.go index dde95f184..17cd17357 100644 --- a/cmd/cmd_before.go +++ b/cmd/flags_validation.go @@ -1,7 +1,6 @@ package cmd import ( - "context" "fmt" "log/slog" @@ -9,11 +8,19 @@ import ( "github.com/urfave/cli/v3" ) -func Before(ctx context.Context, cmd *cli.Command) (context.Context, error) { +// FIXME convert to flag requirement? +func validatePathFlag(cmd *cli.Command) { if cmd.String(flgPath) == "" { log.Fatal(fmt.Sprintf("Could not determine the current working directory. Please pass --%s.", flgPath)) } + // FIXME is command list need an existing path? +} + +// FIXME rename + remove fatal? +func validateFlags(cmd *cli.Command) { + validatePathFlag(cmd) + err := createNonExistingFolder(cmd.String(flgPath)) if err != nil { log.Fatal("Could not check/create the path.", @@ -26,6 +33,4 @@ func Before(ctx context.Context, cmd *cli.Command) (context.Context, error) { if cmd.String(flgServer) == "" { log.Fatal(fmt.Sprintf("Could not determine the current working server. Please pass --%s.", flgServer)) } - - return ctx, nil } diff --git a/cmd/lego/main.go b/cmd/lego/main.go index 8219d5bf6..96fccecb0 100644 --- a/cmd/lego/main.go +++ b/cmd/lego/main.go @@ -20,7 +20,6 @@ func main() { Version: getVersion(), EnableShellCompletion: true, Flags: cmd.CreateFlags(""), - Before: cmd.Before, Commands: cmd.CreateCommands(), }