refactor(renew): lazy client set up

This commit is contained in:
Fernandez Ludovic 2026-01-25 20:41:10 +01:00
commit 6325a46259
3 changed files with 26 additions and 22 deletions

View file

@ -12,6 +12,7 @@ import (
"os"
"slices"
"strings"
"sync"
"time"
"github.com/go-acme/lego/v5/acme/api"
@ -25,6 +26,8 @@ import (
"github.com/urfave/cli/v3"
)
type lzSetUp func() (*lego.Client, error)
func createRenew() *cli.Command {
return &cli.Command{
Name: "renew",
@ -80,16 +83,27 @@ func renew(ctx context.Context, cmd *cli.Command) error {
hook.EnvAccountEmail: account.Email,
}
lazyClient := sync.OnceValues(func() (*lego.Client, error) {
client, err := newClient(cmd, account, keyType)
if err != nil {
return nil, fmt.Errorf("new client: %w", err)
}
setupChallenges(cmd, client)
return client, nil
})
// CSR
if cmd.IsSet(flgCSR) {
return renewForCSR(ctx, cmd, account, keyType, certsStorage, meta)
return renewForCSR(ctx, cmd, lazyClient, certsStorage, meta)
}
// Domains
return renewForDomains(ctx, cmd, account, keyType, certsStorage, meta)
return renewForDomains(ctx, cmd, lazyClient, certsStorage, meta)
}
func renewForDomains(ctx context.Context, cmd *cli.Command, account *storage.Account, keyType certcrypto.KeyType, certsStorage *storage.CertificatesStorage, meta map[string]string) error {
func renewForDomains(ctx context.Context, cmd *cli.Command, lazyClient lzSetUp, certsStorage *storage.CertificatesStorage, meta map[string]string) error {
domains := cmd.StringSlice(flgDomains)
domain := domains[0]
@ -111,7 +125,7 @@ func renewForDomains(ctx context.Context, cmd *cli.Command, account *storage.Acc
var client *lego.Client
if !cmd.Bool(flgARIDisable) {
client, err = setupClient(cmd, account, keyType)
client, err = lazyClient()
if err != nil {
return fmt.Errorf("set up client: %w", err)
}
@ -150,7 +164,7 @@ func renewForDomains(ctx context.Context, cmd *cli.Command, account *storage.Acc
}
if client == nil {
client, err = setupClient(cmd, account, keyType)
client, err = lazyClient()
if err != nil {
return fmt.Errorf("set up client: %w", err)
}
@ -223,7 +237,7 @@ func renewForDomains(ctx context.Context, cmd *cli.Command, account *storage.Acc
return hook.Launch(ctx, cmd.String(flgDeployHook), cmd.Duration(flgDeployHookTimeout), meta)
}
func renewForCSR(ctx context.Context, cmd *cli.Command, account *storage.Account, keyType certcrypto.KeyType, certsStorage *storage.CertificatesStorage, meta map[string]string) error {
func renewForCSR(ctx context.Context, cmd *cli.Command, lazyClient lzSetUp, certsStorage *storage.CertificatesStorage, meta map[string]string) error {
csr, err := readCSRFile(cmd.String(flgCSR))
if err != nil {
return fmt.Errorf("could not read CSR file %q: %w", cmd.String(flgCSR), err)
@ -252,7 +266,7 @@ func renewForCSR(ctx context.Context, cmd *cli.Command, account *storage.Account
var client *lego.Client
if !cmd.Bool(flgARIDisable) {
client, err = setupClient(cmd, account, keyType)
client, err = lazyClient()
if err != nil {
return fmt.Errorf("set up client: %w", err)
}
@ -286,7 +300,7 @@ func renewForCSR(ctx context.Context, cmd *cli.Command, account *storage.Account
}
if client == nil {
client, err = setupClient(cmd, account, keyType)
client, err = lazyClient()
if err != nil {
return fmt.Errorf("set up client: %w", err)
}

View file

@ -54,11 +54,13 @@ func run(ctx context.Context, cmd *cli.Command) error {
return fmt.Errorf("set up account: %w", err)
}
client, err := setupClient(cmd, account, keyType)
client, err := newClient(cmd, account, keyType)
if err != nil {
return fmt.Errorf("set up client: %w", err)
return fmt.Errorf("new client: %w", err)
}
setupChallenges(cmd, client)
if account.Registration == nil {
var reg *registration.Resource

View file

@ -22,18 +22,6 @@ import (
"github.com/urfave/cli/v3"
)
// setupClient creates a new client with challenge settings.
func setupClient(cmd *cli.Command, account registration.User, keyType certcrypto.KeyType) (*lego.Client, error) {
client, err := newClient(cmd, account, keyType)
if err != nil {
return nil, fmt.Errorf("new client: %w", err)
}
setupChallenges(cmd, client)
return client, nil
}
func newClient(cmd *cli.Command, account registration.User, keyType certcrypto.KeyType) (*lego.Client, error) {
client, err := lego.NewClient(newClientConfig(cmd, account, keyType))
if err != nil {