From 89eff6fc7b6791b4817053d46e165442f733942f Mon Sep 17 00:00:00 2001 From: Silas Rech Date: Sat, 8 Sep 2018 14:58:17 +0200 Subject: [PATCH] Add filename parameter (#612) --- cli.go | 4 ++++ cli_handlers.go | 19 ++++++++++++++++--- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/cli.go b/cli.go index 53c83d83..9e0d427a 100644 --- a/cli.go +++ b/cli.go @@ -109,6 +109,10 @@ func main() { Name: "email, m", Usage: "Email used for registration and recovery contact.", }, + cli.StringFlag{ + Name: "filename", + Usage: "Filename of the generated certificate", + }, cli.BoolFlag{ Name: "accept-tos, a", Usage: "By setting this flag to true you indicate that you accept the current Let's Encrypt terms of service.", diff --git a/cli_handlers.go b/cli_handlers.go index 19b8dc8e..7541bb00 100644 --- a/cli_handlers.go +++ b/cli_handlers.go @@ -11,6 +11,7 @@ import ( "net/http" "os" "path" + "path/filepath" "strings" "time" @@ -151,8 +152,15 @@ func setup(c *cli.Context) (*Configuration, *Account, *acme.Client) { } func saveCertRes(certRes *acme.CertificateResource, conf *Configuration) { - // make sure no funny chars are in the cert names (like wildcards ;)) - domainName := strings.Replace(certRes.Domain, "*", "_", -1) + var domainName string + + // Check filename cli parameter + if conf.context.GlobalString("filename") == "" { + // Make sure no funny chars are in the cert names (like wildcards ;)) + domainName = strings.Replace(certRes.Domain, "*", "_", -1) + } else { + domainName = conf.context.GlobalString("filename") + } // We store the certificate, private key and metadata in different files // as web servers would not be able to work with a combined file. @@ -162,7 +170,12 @@ func saveCertRes(certRes *acme.CertificateResource, conf *Configuration) { metaOut := path.Join(conf.CertPath(), domainName+".json") issuerOut := path.Join(conf.CertPath(), domainName+".issuer.crt") - err := ioutil.WriteFile(certOut, certRes.Certificate, 0600) + err := checkFolder(filepath.Dir(certOut)) + if err != nil { + log.Fatalf("Could not check/create path: %v", err) + } + + err = ioutil.WriteFile(certOut, certRes.Certificate, 0600) if err != nil { log.Fatalf("Unable to save Certificate for domain %s\n\t%v", certRes.Domain, err) }