diff --git a/certs/certbot.go b/certs/certbot.go new file mode 100644 index 0000000..56802cb --- /dev/null +++ b/certs/certbot.go @@ -0,0 +1,28 @@ +package certs + +import ( + "fmt" + "os/exec" +) + +// CertbotCreateCertificate runs certbot to create a certificate +func CertbotCreateCertificate(domain, email string) error { + cmd := exec.Command("certbot", "certonly", "--non-interactive", "--agree-tos", "--email", email, "-d", domain) + output, err := cmd.CombinedOutput() + if err != nil { + return fmt.Errorf("failed to create certificate: %s", string(output)) + } + fmt.Println("Certificate created successfully:", string(output)) + return nil +} + +// CertbotRenewCertificate runs certbot to renew a certificate +func CertbotRenewCertificate() error { + cmd := exec.Command("certbot", "renew") + output, err := cmd.CombinedOutput() + if err != nil { + return fmt.Errorf("failed to renew certificate: %s", string(output)) + } + fmt.Println("Certificate renewed successfully:", string(output)) + return nil +} diff --git a/cmd/certs.go b/cmd/certs.go index b3e60c8..1a42dbe 100644 --- a/cmd/certs.go +++ b/cmd/certs.go @@ -11,44 +11,78 @@ import ( func CertsCommand() *cli.Command { return &cli.Command{ Name: "certs", - Usage: "Generate self-signed certificates for testing purposes", - Flags: []cli.Flag{ - &cli.StringFlag{ - Name: "cert", - Usage: "Path to save the certificate PEM file", - Value: "cert.pem", + Usage: "Manage SSL certificates", + Subcommands: []*cli.Command{ + { + Name: "create", + Usage: "Create a new certificate", + Flags: []cli.Flag{ + &cli.StringFlag{ + Name: "domain", + Usage: "Domain name for the certificate", + Required: true, + }, + &cli.StringFlag{ + Name: "email", + Usage: "Email for Let's Encrypt notifications", + Required: true, + }, + }, + Action: func(c *cli.Context) error { + domain := c.String("domain") + email := c.String("email") + return certs.CertbotCreateCertificate(domain, email) + }, }, - &cli.StringFlag{ - Name: "key", - Usage: "Path to save the private key PEM file", - Value: "key.pem", + { + Name: "renew", + Usage: "Renew existing certificates", + Action: func(c *cli.Context) error { + return certs.CertbotRenewCertificate() + }, }, - }, - Action: func(c *cli.Context) error { - cert, privKey, err := certs.GenerateSelfSignedCert() // Use the correct function - if err != nil { - return fmt.Errorf("failed to generate certificate: %w", err) - } + { + Name: "local", + Usage: "Generate self-signed certificates for testing purposes", + Flags: []cli.Flag{ + &cli.StringFlag{ + Name: "cert", + Usage: "Path to save the certificate PEM file", + Value: "cert.pem", + }, + &cli.StringFlag{ + Name: "key", + Usage: "Path to save the private key PEM file", + Value: "key.pem", + }, + }, + Action: func(c *cli.Context) error { + cert, privKey, err := certs.GenerateSelfSignedCert() // Use the correct function + if err != nil { + return fmt.Errorf("failed to generate certificate: %w", err) + } - // Export the cert and key to PEM format - certPEM, keyPEM, err := certs.ExportPEM(cert, privKey) - if err != nil { - return fmt.Errorf("failed to export certificate and key: %w", err) - } + // Export the cert and key to PEM format + certPEM, keyPEM, err := certs.ExportPEM(cert, privKey) + if err != nil { + return fmt.Errorf("failed to export certificate and key: %w", err) + } - // Save the PEM files - certOutput := c.String("cert") - keyOutput := c.String("key") + // Save the PEM files + certOutput := c.String("cert") + keyOutput := c.String("key") - if err := os.WriteFile(certOutput, certPEM, 0644); err != nil { - return fmt.Errorf("failed to write certificate file: %w", err) - } - if err := os.WriteFile(keyOutput, keyPEM, 0600); err != nil { - return fmt.Errorf("failed to write private key file: %w", err) - } + if err := os.WriteFile(certOutput, certPEM, 0644); err != nil { + return fmt.Errorf("failed to write certificate file: %w", err) + } + if err := os.WriteFile(keyOutput, keyPEM, 0600); err != nil { + return fmt.Errorf("failed to write private key file: %w", err) + } - fmt.Printf("Certificate and key have been successfully generated and saved to %s and %s\n", certOutput, keyOutput) - return nil + fmt.Printf("Certificate and key have been successfully generated and saved to %s and %s\n", certOutput, keyOutput) + return nil + }, + }, }, } }