Repository: go-acme/lego (language: go)

Issue:
Add lego cli flag to add to the user-agent
### Welcome

- [X] Yes, I've searched similar issues on GitHub and didn't find any.

### How do you use lego?

Binary

### Detailed Description

Hi!  I'm an engineer working on Let's Encrypt.  As you recently heard in #1656, we had a hard time identifying a large integration that's using the lego CLI in a bursty way -- it is looking like it's [bitnami](https://github.com/bitnami/vms/issues/43).

What I'd like is for a command line flag for anyone who is shipping the lego cli integrated into another application to be able to add to the user agent. That allows us to identify at least well-behaving integrations shelling out to this client.


Discussion / hints:
Hi, I understand.
Do you want to create a PR or do you want me to create a PR?
I’m happy to create a PR 

Fix patch:
diff --git a/cmd/accounts_storage.go b/cmd/accounts_storage.go
index 8be48e2520..1b4014eb8b 100644
--- a/cmd/accounts_storage.go
+++ b/cmd/accounts_storage.go
@@ -6,7 +6,6 @@ import (
 	"encoding/json"
 	"encoding/pem"
 	"errors"
-	"fmt"
 	"net/url"
 	"os"
 	"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.
 	config := lego.NewConfig(&Account{key: privateKey})
 	config.CADirURL = ctx.String("server")
-	config.UserAgent = fmt.Sprintf("lego-cli/%s", ctx.App.Version)
+	config.UserAgent = getUserAgent(ctx)
 
 	client, err := lego.NewClient(config)
 	if err != nil {
diff --git a/cmd/flags.go b/cmd/flags.go
index 88e5c610f1..8f9ff4462b 100644
--- a/cmd/flags.go
+++ b/cmd/flags.go
@@ -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.",
 			Value: 30,
 		},
+		&cli.StringFlag{
+			Name:  "user-agent",
+			Usage: "Add to the user-agent sent to the CA to identify an application embedding lego-cli",
+		},
 	}
 }
diff --git a/cmd/setup.go b/cmd/setup.go
index 94e3345fc9..b21b6d531a 100644
--- a/cmd/setup.go
+++ b/cmd/setup.go
@@ -41,7 +41,7 @@ func newClient(ctx *cli.Context, acc registration.User, keyType certcrypto.KeyTy
 		KeyType: keyType,
 		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") {
 		config.HTTPClient.Timeout = time.Duration(ctx.Int("http-timeout")) * time.Second
@@ -87,6 +87,10 @@ func getEmail(ctx *cli.Context) string {
 	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 {
 	if _, err := os.Stat(path); os.IsNotExist(err) {
 		return os.MkdirAll(path, 0o700)


Test patch: