From 2f16c0fa5c309cc1521125e540193c78fcc1d3d6 Mon Sep 17 00:00:00 2001 From: xenolf Date: Mon, 7 Dec 2015 16:52:59 +0100 Subject: [PATCH] Make sure the CLI returns proper non-zero exit codes on errors. --- cli_handlers.go | 46 +++++++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/cli_handlers.go b/cli_handlers.go index 09a87c84..7f44e88c 100644 --- a/cli_handlers.go +++ b/cli_handlers.go @@ -23,7 +23,7 @@ func checkFolder(path string) error { func setup(c *cli.Context) (*Configuration, *Account, *acme.Client) { err := checkFolder(c.GlobalString("path")) if err != nil { - logger().Fatalf("Cound not check/create path: %v", err) + logger().Fatalf("Cound not check/create path: %s", err.Error()) } conf := NewConfiguration(c) @@ -36,7 +36,7 @@ func setup(c *cli.Context) (*Configuration, *Account, *acme.Client) { client, err := acme.NewClient(c.GlobalString("server"), acc, conf.RsaBits(), conf.OptPort()) if err != nil { - logger().Fatal("Could not create client:", err) + logger().Fatalf("Could not create client: %s", err.Error()) } return conf, acc, client @@ -51,22 +51,22 @@ func saveCertRes(certRes acme.CertificateResource, conf *Configuration) { err := ioutil.WriteFile(certOut, certRes.Certificate, 0600) if err != nil { - logger().Printf("Unable to save Certificate for domain %s\n\t%v", certRes.Domain, err) + logger().Fatalf("Unable to save Certificate for domain %s\n\t%s", certRes.Domain, err.Error()) } err = ioutil.WriteFile(privOut, certRes.PrivateKey, 0600) if err != nil { - logger().Printf("Unable to save PrivateKey for domain %s\n\t%v", certRes.Domain, err) + logger().Fatalf("Unable to save PrivateKey for domain %s\n\t%s", certRes.Domain, err.Error()) } jsonBytes, err := json.MarshalIndent(certRes, "", "\t") if err != nil { - logger().Printf("Unable to marshal CertResource for domain %s\n\t%v", certRes.Domain, err) + logger().Fatalf("Unable to marshal CertResource for domain %s\n\t%s", certRes.Domain, err.Error()) } err = ioutil.WriteFile(metaOut, jsonBytes, 0600) if err != nil { - logger().Printf("Unable to save CertResource for domain %s\n\t%v", certRes.Domain, err) + logger().Fatalf("Unable to save CertResource for domain %s\n\t%s", certRes.Domain, err.Error()) } } @@ -75,7 +75,7 @@ func run(c *cli.Context) { if acc.Registration == nil { reg, err := client.Register() if err != nil { - logger().Fatalf("Could not complete registration\n\t%v", err) + logger().Fatalf("Could not complete registration\n\t%s", err.Error()) } acc.Registration = reg @@ -100,7 +100,7 @@ func run(c *cli.Context) { logger().Println("Do you accept the TOS? Y/n") text, err := reader.ReadString('\n') if err != nil { - logger().Fatalf("Could not read from console -> %v", err) + logger().Fatalf("Could not read from console -> %s", err.Error()) } text = strings.Trim(text, "\r\n") @@ -112,7 +112,7 @@ func run(c *cli.Context) { if text == "Y" || text == "y" || text == "" { err = client.AgreeToTOS() if err != nil { - logger().Fatalf("Could not agree to tos -> %v", err) + logger().Fatalf("Could not agree to tos -> %s", err) } acc.Save() break @@ -129,13 +129,18 @@ func run(c *cli.Context) { cert, failures := client.ObtainSANCertificate(c.GlobalStringSlice("domains"), true) if len(failures) > 0 { for k, v := range failures { - logger().Printf("[%s] Could not obtain certificates\n\t%v", k, v) + logger().Printf("[%s] Could not obtain certificates\n\t%s", k, v.Error()) } + + // Make sure to return a non-zero exit code if ObtainSANCertificate + // returned at least one error. Due to us not returning partial + // certificate we can just exit here instead of at the end. + os.Exit(1) } err := checkFolder(conf.CertPath()) if err != nil { - logger().Fatalf("Cound not check/create path: %v", err) + logger().Fatalf("Cound not check/create path: %s", err.Error()) } saveCertRes(cert, conf) @@ -147,7 +152,7 @@ func revoke(c *cli.Context) { err := checkFolder(conf.CertPath()) if err != nil { - logger().Fatalf("Cound not check/create path: %v", err) + logger().Fatalf("Cound not check/create path: %s", err.Error()) } for _, domain := range c.GlobalStringSlice("domains") { @@ -158,7 +163,7 @@ func revoke(c *cli.Context) { err = client.RevokeCertificate(certBytes) if err != nil { - logger().Printf("Error while revoking the certificate for domain %s\n\t%v", domain, err) + logger().Fatalf("Error while revoking the certificate for domain %s\n\t%s", domain, err.Error()) } else { logger().Print("Certificate was revoked.") } @@ -178,8 +183,7 @@ func renew(c *cli.Context) { certBytes, err := ioutil.ReadFile(certPath) if err != nil { - logger().Printf("Error while loading the certificate for domain %s\n\t%v", domain, err) - return + logger().Fatalf("Error while loading the certificate for domain %s\n\t%s", domain, err.Error()) } if c.IsSet("days") { @@ -195,21 +199,18 @@ func renew(c *cli.Context) { keyBytes, err := ioutil.ReadFile(privPath) if err != nil { - logger().Printf("Error while loading the private key for domain %s\n\t%v", domain, err) - return + logger().Fatalf("Error while loading the private key for domain %s\n\t%s", domain, err.Error()) } metaBytes, err := ioutil.ReadFile(metaPath) if err != nil { - logger().Printf("Error while loading the meta data for domain %s\n\t%v", domain, err) - return + logger().Fatalf("Error while loading the meta data for domain %s\n\t%s", domain, err.Error()) } var certRes acme.CertificateResource err = json.Unmarshal(metaBytes, &certRes) if err != nil { - logger().Printf("Error while marshalling the meta data for domain %s\n\t%v", domain, err) - return + logger().Fatalf("Error while marshalling the meta data for domain %s\n\t%s", domain, err.Error()) } certRes.PrivateKey = keyBytes @@ -217,8 +218,7 @@ func renew(c *cli.Context) { newCert, err := client.RenewCertificate(certRes, true, true) if err != nil { - logger().Printf("%v", err) - return + logger().Fatalf("%s", err.Error()) } saveCertRes(newCert, conf)