Skip to content

Commit

Permalink
chore: migrate to github.com/urfave/cli/v2 (#1590)
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed Feb 13, 2022
1 parent 6d474eb commit 7d9176b
Show file tree
Hide file tree
Showing 17 changed files with 186 additions and 176 deletions.
8 changes: 4 additions & 4 deletions cmd/accounts_storage.go
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/go-acme/lego/v4/lego"
"github.com/go-acme/lego/v4/log"
"github.com/go-acme/lego/v4/registration"
"github.com/urfave/cli"
"github.com/urfave/cli/v2"
)

const (
Expand Down Expand Up @@ -73,12 +73,12 @@ func NewAccountsStorage(ctx *cli.Context) *AccountsStorage {
// TODO: move to account struct? Currently MUST pass email.
email := getEmail(ctx)

serverURL, err := url.Parse(ctx.GlobalString("server"))
serverURL, err := url.Parse(ctx.String("server"))
if err != nil {
log.Fatal(err)
}

rootPath := filepath.Join(ctx.GlobalString("path"), baseAccountsRootFolderName)
rootPath := filepath.Join(ctx.String("path"), baseAccountsRootFolderName)
serverPath := strings.NewReplacer(":", "_", "/", string(os.PathSeparator)).Replace(serverURL.Host)
accountsPath := filepath.Join(rootPath, serverPath)
rootUserPath := filepath.Join(accountsPath, email)
Expand Down Expand Up @@ -226,7 +226,7 @@ func loadPrivateKey(file string) (crypto.PrivateKey, error) {
func tryRecoverRegistration(ctx *cli.Context, privateKey crypto.PrivateKey) (*registration.Resource, error) {
// couldn't load account but got a key. Try to look the account up.
config := lego.NewConfig(&Account{key: privateKey})
config.CADirURL = ctx.GlobalString("server")
config.CADirURL = ctx.String("server")
config.UserAgent = fmt.Sprintf("lego-cli/%s", ctx.App.Version)

client, err := lego.NewClient(config)
Expand Down
14 changes: 7 additions & 7 deletions cmd/certs_storage.go
Expand Up @@ -17,7 +17,7 @@ import (
"github.com/go-acme/lego/v4/certcrypto"
"github.com/go-acme/lego/v4/certificate"
"github.com/go-acme/lego/v4/log"
"github.com/urfave/cli"
"github.com/urfave/cli/v2"
"golang.org/x/net/idna"
"software.sslmate.com/src/go-pkcs12"
)
Expand Down Expand Up @@ -53,12 +53,12 @@ type CertificatesStorage struct {
// NewCertificatesStorage create a new certificates storage.
func NewCertificatesStorage(ctx *cli.Context) *CertificatesStorage {
return &CertificatesStorage{
rootPath: filepath.Join(ctx.GlobalString("path"), baseCertificatesFolderName),
archivePath: filepath.Join(ctx.GlobalString("path"), baseArchivesFolderName),
pem: ctx.GlobalBool("pem"),
pfx: ctx.GlobalBool("pfx"),
pfxPassword: ctx.GlobalString("pfx.pass"),
filename: ctx.GlobalString("filename"),
rootPath: filepath.Join(ctx.String("path"), baseCertificatesFolderName),
archivePath: filepath.Join(ctx.String("path"), baseArchivesFolderName),
pem: ctx.Bool("pem"),
pfx: ctx.Bool("pfx"),
pfxPassword: ctx.String("pfx.pass"),
filename: ctx.String("filename"),
}
}

Expand Down
6 changes: 3 additions & 3 deletions cmd/cmd.go
@@ -1,10 +1,10 @@
package cmd

import "github.com/urfave/cli"
import "github.com/urfave/cli/v2"

// CreateCommands Creates all CLI commands.
func CreateCommands() []cli.Command {
return []cli.Command{
func CreateCommands() []*cli.Command {
return []*cli.Command{
createRun(),
createRevoke(),
createRenew(),
Expand Down
8 changes: 4 additions & 4 deletions cmd/cmd_before.go
Expand Up @@ -2,20 +2,20 @@ package cmd

import (
"github.com/go-acme/lego/v4/log"
"github.com/urfave/cli"
"github.com/urfave/cli/v2"
)

func Before(ctx *cli.Context) error {
if ctx.GlobalString("path") == "" {
if ctx.String("path") == "" {
log.Fatal("Could not determine current working directory. Please pass --path.")
}

err := createNonExistingFolder(ctx.GlobalString("path"))
err := createNonExistingFolder(ctx.String("path"))
if err != nil {
log.Fatalf("Could not check/create path: %v", err)
}

if ctx.GlobalString("server") == "" {
if ctx.String("server") == "" {
log.Fatal("Could not determine current working server. Please pass --server.")
}

Expand Down
13 changes: 7 additions & 6 deletions cmd/cmd_dnshelp.go
Expand Up @@ -7,18 +7,19 @@ import (
"strings"
"text/tabwriter"

"github.com/urfave/cli"
"github.com/urfave/cli/v2"
)

func createDNSHelp() cli.Command {
return cli.Command{
func createDNSHelp() *cli.Command {
return &cli.Command{
Name: "dnshelp",
Usage: "Shows additional help for the '--dns' global option",
Action: dnsHelp,
Flags: []cli.Flag{
cli.StringFlag{
Name: "code, c",
Usage: fmt.Sprintf("DNS code: %s", allDNSCodes()),
&cli.StringFlag{
Name: "code",
Aliases: []string{"c"},
Usage: fmt.Sprintf("DNS code: %s", allDNSCodes()),
},
},
}
Expand Down
22 changes: 12 additions & 10 deletions cmd/cmd_list.go
Expand Up @@ -9,22 +9,24 @@ import (
"strings"

"github.com/go-acme/lego/v4/certcrypto"
"github.com/urfave/cli"
"github.com/urfave/cli/v2"
)

func createList() cli.Command {
return cli.Command{
func createList() *cli.Command {
return &cli.Command{
Name: "list",
Usage: "Display certificates and accounts information.",
Action: list,
Flags: []cli.Flag{
cli.BoolFlag{
Name: "accounts, a",
Usage: "Display accounts.",
&cli.BoolFlag{
Name: "accounts",
Aliases: []string{"a"},
Usage: "Display accounts.",
},
cli.BoolFlag{
Name: "names, n",
Usage: "Display certificate common names only.",
&cli.BoolFlag{
Name: "names",
Aliases: []string{"n"},
Usage: "Display certificate common names only.",
},
},
}
Expand Down Expand Up @@ -92,7 +94,7 @@ func listCertificates(ctx *cli.Context) error {

func listAccount(ctx *cli.Context) error {
// fake email, needed by NewAccountsStorage
if err := ctx.GlobalSet("email", "unknown"); err != nil {
if err := ctx.Set("email", "unknown"); err != nil {
return err
}

Expand Down
30 changes: 15 additions & 15 deletions cmd/cmd_renew.go
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/go-acme/lego/v4/certificate"
"github.com/go-acme/lego/v4/lego"
"github.com/go-acme/lego/v4/log"
"github.com/urfave/cli"
"github.com/urfave/cli/v2"
)

const (
Expand All @@ -19,15 +19,15 @@ const (
renewEnvCertKeyPath = "LEGO_CERT_KEY_PATH"
)

func createRenew() cli.Command {
return cli.Command{
func createRenew() *cli.Command {
return &cli.Command{
Name: "renew",
Usage: "Renew a certificate",
Action: renew,
Before: func(ctx *cli.Context) error {
// we require either domains or csr, but not both
hasDomains := len(ctx.GlobalStringSlice("domains")) > 0
hasCsr := len(ctx.GlobalString("csr")) > 0
hasDomains := len(ctx.StringSlice("domains")) > 0
hasCsr := len(ctx.String("csr")) > 0
if hasDomains && hasCsr {
log.Fatal("Please specify either --domains/-d or --csr/-c, but not both")
}
Expand All @@ -37,32 +37,32 @@ func createRenew() cli.Command {
return nil
},
Flags: []cli.Flag{
cli.IntFlag{
&cli.IntFlag{
Name: "days",
Value: 30,
Usage: "The number of days left on a certificate to renew it.",
},
cli.BoolFlag{
&cli.BoolFlag{
Name: "reuse-key",
Usage: "Used to indicate you want to reuse your current private key for the new certificate.",
},
cli.BoolFlag{
&cli.BoolFlag{
Name: "no-bundle",
Usage: "Do not create a certificate bundle by adding the issuers certificate to the new certificate.",
},
cli.BoolFlag{
&cli.BoolFlag{
Name: "must-staple",
Usage: "Include the OCSP must staple TLS extension in the CSR and generated certificate. Only works if the CSR is generated by lego.",
},
cli.StringFlag{
&cli.StringFlag{
Name: "renew-hook",
Usage: "Define a hook. The hook is executed only when the certificates are effectively renewed.",
},
cli.StringFlag{
&cli.StringFlag{
Name: "preferred-chain",
Usage: "If the CA offers multiple certificate chains, prefer the chain with an issuer matching this Subject Common Name. If no match, the default offered chain will be used.",
},
cli.StringFlag{
&cli.StringFlag{
Name: "always-deactivate-authorizations",
Usage: "Force the authorizations to be relinquished even if the certificate request was successful.",
},
Expand All @@ -85,7 +85,7 @@ func renew(ctx *cli.Context) error {
meta := map[string]string{renewEnvAccountEmail: account.Email}

// CSR
if ctx.GlobalIsSet("csr") {
if ctx.IsSet("csr") {
return renewForCSR(ctx, client, certsStorage, bundle, meta)
}

Expand All @@ -94,7 +94,7 @@ func renew(ctx *cli.Context) error {
}

func renewForDomains(ctx *cli.Context, client *lego.Client, certsStorage *CertificatesStorage, bundle bool, meta map[string]string) error {
domains := ctx.GlobalStringSlice("domains")
domains := ctx.StringSlice("domains")
domain := domains[0]

// load the cert resource from files.
Expand Down Expand Up @@ -153,7 +153,7 @@ func renewForDomains(ctx *cli.Context, client *lego.Client, certsStorage *Certif
}

func renewForCSR(ctx *cli.Context, client *lego.Client, certsStorage *CertificatesStorage, bundle bool, meta map[string]string) error {
csr, err := readCSRFile(ctx.GlobalString("csr"))
csr, err := readCSRFile(ctx.String("csr"))
if err != nil {
log.Fatal(err)
}
Expand Down
17 changes: 9 additions & 8 deletions cmd/cmd_revoke.go
Expand Up @@ -3,20 +3,21 @@ package cmd
import (
"github.com/go-acme/lego/v4/acme"
"github.com/go-acme/lego/v4/log"
"github.com/urfave/cli"
"github.com/urfave/cli/v2"
)

func createRevoke() cli.Command {
return cli.Command{
func createRevoke() *cli.Command {
return &cli.Command{
Name: "revoke",
Usage: "Revoke a certificate",
Action: revoke,
Flags: []cli.Flag{
cli.BoolFlag{
Name: "keep, k",
Usage: "Keep the certificates after the revocation instead of archiving them.",
&cli.BoolFlag{
Name: "keep",
Aliases: []string{"k"},
Usage: "Keep the certificates after the revocation instead of archiving them.",
},
cli.UintFlag{
&cli.UintFlag{
Name: "reason",
Usage: "Identifies the reason for the certificate revocation. See https://www.rfc-editor.org/rfc/rfc5280.html#section-5.3.1. 0(unspecified),1(keyCompromise),2(cACompromise),3(affiliationChanged),4(superseded),5(cessationOfOperation),6(certificateHold),8(removeFromCRL),9(privilegeWithdrawn),10(aACompromise)",
Value: acme.CRLReasonUnspecified,
Expand All @@ -35,7 +36,7 @@ func revoke(ctx *cli.Context) error {
certsStorage := NewCertificatesStorage(ctx)
certsStorage.CreateRootFolder()

for _, domain := range ctx.GlobalStringSlice("domains") {
for _, domain := range ctx.StringSlice("domains") {
log.Printf("Trying to revoke certificate for domain %s", domain)

certBytes, err := certsStorage.ReadFile(domain, ".crt")
Expand Down
32 changes: 16 additions & 16 deletions cmd/cmd_run.go
Expand Up @@ -10,17 +10,17 @@ import (
"github.com/go-acme/lego/v4/lego"
"github.com/go-acme/lego/v4/log"
"github.com/go-acme/lego/v4/registration"
"github.com/urfave/cli"
"github.com/urfave/cli/v2"
)

func createRun() cli.Command {
return cli.Command{
func createRun() *cli.Command {
return &cli.Command{
Name: "run",
Usage: "Register an account, then create and install a certificate",
Before: func(ctx *cli.Context) error {
// we require either domains or csr, but not both
hasDomains := len(ctx.GlobalStringSlice("domains")) > 0
hasCsr := len(ctx.GlobalString("csr")) > 0
hasDomains := len(ctx.StringSlice("domains")) > 0
hasCsr := len(ctx.String("csr")) > 0
if hasDomains && hasCsr {
log.Fatal("Please specify either --domains/-d or --csr/-c, but not both")
}
Expand All @@ -31,23 +31,23 @@ func createRun() cli.Command {
},
Action: run,
Flags: []cli.Flag{
cli.BoolFlag{
&cli.BoolFlag{
Name: "no-bundle",
Usage: "Do not create a certificate bundle by adding the issuers certificate to the new certificate.",
},
cli.BoolFlag{
&cli.BoolFlag{
Name: "must-staple",
Usage: "Include the OCSP must staple TLS extension in the CSR and generated certificate. Only works if the CSR is generated by lego.",
},
cli.StringFlag{
&cli.StringFlag{
Name: "run-hook",
Usage: "Define a hook. The hook is executed when the certificates are effectively created.",
},
cli.StringFlag{
&cli.StringFlag{
Name: "preferred-chain",
Usage: "If the CA offers multiple certificate chains, prefer the chain with an issuer matching this Subject Common Name. If no match, the default offered chain will be used.",
},
cli.StringFlag{
&cli.StringFlag{
Name: "always-deactivate-authorizations",
Usage: "Force the authorizations to be relinquished even if the certificate request was successful.",
},
Expand Down Expand Up @@ -110,7 +110,7 @@ func run(ctx *cli.Context) error {

func handleTOS(ctx *cli.Context, client *lego.Client) bool {
// Check for a global accept override
if ctx.GlobalBool("accept-tos") {
if ctx.Bool("accept-tos") {
return true
}

Expand Down Expand Up @@ -142,9 +142,9 @@ func register(ctx *cli.Context, client *lego.Client) (*registration.Resource, er
log.Fatal("You did not accept the TOS. Unable to proceed.")
}

if ctx.GlobalBool("eab") {
kid := ctx.GlobalString("kid")
hmacEncoded := ctx.GlobalString("hmac")
if ctx.Bool("eab") {
kid := ctx.String("kid")
hmacEncoded := ctx.String("hmac")

if kid == "" || hmacEncoded == "" {
log.Fatalf("Requires arguments --kid and --hmac.")
Expand All @@ -163,7 +163,7 @@ func register(ctx *cli.Context, client *lego.Client) (*registration.Resource, er
func obtainCertificate(ctx *cli.Context, client *lego.Client) (*certificate.Resource, error) {
bundle := !ctx.Bool("no-bundle")

domains := ctx.GlobalStringSlice("domains")
domains := ctx.StringSlice("domains")
if len(domains) > 0 {
// obtain a certificate, generating a new private key
request := certificate.ObtainRequest{
Expand All @@ -177,7 +177,7 @@ func obtainCertificate(ctx *cli.Context, client *lego.Client) (*certificate.Reso
}

// read the CSR
csr, err := readCSRFile(ctx.GlobalString("csr"))
csr, err := readCSRFile(ctx.String("csr"))
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 7d9176b

Please sign in to comment.