feat: add a --user-agent flag to lego-cli (#1664)

This commit is contained in:
Matthew McPherrin 2022-06-29 14:56:56 -04:00 committed by GitHub
parent 525ceda150
commit 0e614c1d0b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 3 deletions

View file

@ -6,7 +6,6 @@ import (
"encoding/json" "encoding/json"
"encoding/pem" "encoding/pem"
"errors" "errors"
"fmt"
"net/url" "net/url"
"os" "os"
"path/filepath" "path/filepath"
@ -227,7 +226,7 @@ func tryRecoverRegistration(ctx *cli.Context, privateKey crypto.PrivateKey) (*re
// couldn't load account but got a key. Try to look the account up. // couldn't load account but got a key. Try to look the account up.
config := lego.NewConfig(&Account{key: privateKey}) config := lego.NewConfig(&Account{key: privateKey})
config.CADirURL = ctx.String("server") config.CADirURL = ctx.String("server")
config.UserAgent = fmt.Sprintf("lego-cli/%s", ctx.App.Version) config.UserAgent = getUserAgent(ctx)
client, err := lego.NewClient(config) client, err := lego.NewClient(config)
if err != nil { if err != nil {

View file

@ -132,5 +132,9 @@ func CreateFlags(defaultPath string) []cli.Flag {
Usage: "Set the certificate timeout value to a specific value in seconds. Only used when obtaining certificates.", Usage: "Set the certificate timeout value to a specific value in seconds. Only used when obtaining certificates.",
Value: 30, Value: 30,
}, },
&cli.StringFlag{
Name: "user-agent",
Usage: "Add to the user-agent sent to the CA to identify an application embedding lego-cli",
},
} }
} }

View file

@ -41,7 +41,7 @@ func newClient(ctx *cli.Context, acc registration.User, keyType certcrypto.KeyTy
KeyType: keyType, KeyType: keyType,
Timeout: time.Duration(ctx.Int("cert.timeout")) * time.Second, Timeout: time.Duration(ctx.Int("cert.timeout")) * time.Second,
} }
config.UserAgent = fmt.Sprintf("lego-cli/%s", ctx.App.Version) config.UserAgent = getUserAgent(ctx)
if ctx.IsSet("http-timeout") { if ctx.IsSet("http-timeout") {
config.HTTPClient.Timeout = time.Duration(ctx.Int("http-timeout")) * time.Second config.HTTPClient.Timeout = time.Duration(ctx.Int("http-timeout")) * time.Second
@ -87,6 +87,10 @@ func getEmail(ctx *cli.Context) string {
return email return email
} }
func getUserAgent(ctx *cli.Context) string {
return strings.TrimSpace(fmt.Sprintf("%s lego-cli/%s", ctx.String("user-agent"), ctx.App.Version))
}
func createNonExistingFolder(path string) error { func createNonExistingFolder(path string) error {
if _, err := os.Stat(path); os.IsNotExist(err) { if _, err := os.Stat(path); os.IsNotExist(err) {
return os.MkdirAll(path, 0o700) return os.MkdirAll(path, 0o700)