diff --git a/cmd/revoke.go b/cmd/revoke.go index 42d24b6c..7b50557a 100644 --- a/cmd/revoke.go +++ b/cmd/revoke.go @@ -1,15 +1,39 @@ package cmd import ( - "fmt" "io/ioutil" "path" + "github.com/gianluca311/lego/cmd/utils" "github.com/spf13/cobra" ) func revokeHandler(cmd *cobra.Command, args []string) { + conf, _, client := utils.Setup(RootCmd) + err := utils.CheckFolder(conf.CertPath()) + if err != nil { + logger().Fatalf("Could not check/create path: %s", err.Error()) + } + + domains, err := RootCmd.PersistentFlags().GetStringSlice("domains") + if err != nil { + logger().Fatalln(err.Error()) + } + + for _, domain := range domains { + logger().Printf("Trying to revoke certificate for domain %s", domain) + + certPath := path.Join(conf.CertPath(), domain+".crt") + certBytes, err := ioutil.ReadFile(certPath) + + err = client.RevokeCertificate(certBytes) + if err != nil { + logger().Fatalf("Error while revoking the certificate for domain %s\n\t%s", domain, err.Error()) + } else { + logger().Print("Certificate was revoked.") + } + } } // revokeCmd represents the revoke command @@ -17,10 +41,7 @@ var revokeCmd = &cobra.Command{ Use: "revoke", Short: "Revoke a certificate", Long: ``, - Run: func(cmd *cobra.Command, args []string) { - // TODO: Work your own magic here - fmt.Println("revoke called") - }, + Run: revokeHandler, } func init() { diff --git a/cmd/run.go b/cmd/run.go index bef2b997..9fdbad03 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -51,19 +51,19 @@ func runHandler(cmd *cobra.Command, args []string) { utils.HandleTOS(RootCmd, client, acc) } - domains, err := RootCmd.PersistentFlags().GetStringSlice("domains") - if err != nil { - logger().Fatalln(err.Error()) - } - + domains, err := RootCmd.PersistentFlags().GetStringSlice("domains") + if err != nil { + logger().Fatalln(err.Error()) + } + if len(domains) == 0 { logger().Fatal("Please specify --domains or -d") } - nobundle, err := cmd.PersistentFlags().GetBool("no-bundle") - if err != nil { - logger().Fatalln(err.Error()) - } + nobundle, err := cmd.PersistentFlags().GetBool("no-bundle") + if err != nil { + logger().Fatalln(err.Error()) + } cert, failures := client.ObtainCertificate(domains, !nobundle, nil) if len(failures) > 0 { for k, v := range failures { @@ -76,12 +76,12 @@ func runHandler(cmd *cobra.Command, args []string) { os.Exit(1) } - err := utils.CheckFolder(conf.CertPath()) + err = utils.CheckFolder(conf.CertPath()) if err != nil { logger().Fatalf("Could not check/create path: %s", err.Error()) } - saveCertRes(cert, conf) + utils.SaveCertRes(cert, conf) } // runCmd represents the run command diff --git a/cmd/utils/account.go b/cmd/utils/account.go index 47127c4a..547d8e94 100644 --- a/cmd/utils/account.go +++ b/cmd/utils/account.go @@ -24,7 +24,7 @@ func NewAccount(email string, conf *Configuration) *Account { accKeysPath := conf.AccountKeysPath(email) // TODO: move to function in configuration? accKeyPath := accKeysPath + string(os.PathSeparator) + email + ".key" - if err := checkFolder(accKeysPath); err != nil { + if err := CheckFolder(accKeysPath); err != nil { logger().Fatalf("Could not check/create directory for account %s: %v", email, err) } diff --git a/cmd/utils/utils.go b/cmd/utils/utils.go index 3a4127c3..9bcea414 100644 --- a/cmd/utils/utils.go +++ b/cmd/utils/utils.go @@ -40,7 +40,7 @@ func CheckFolder(path string) error { return nil } -func saveCertRes(certRes acme.CertificateResource, conf *Configuration) { +func SaveCertRes(certRes acme.CertificateResource, conf *Configuration) { // We store the certificate, private key and metadata in different files // as web servers would not be able to work with a combined file. certOut := path.Join(conf.CertPath(), certRes.Domain+".crt")