Skip to content

Commit f2b3c4d

Browse files
authored
feat: add ability to update certbot email when obtaining ssl certificates (#68)
1 parent 6363b7f commit f2b3c4d

File tree

1 file changed

+74
-8
lines changed

1 file changed

+74
-8
lines changed

pkg/network/certbot.go

+74-8
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/pterm/pterm"
77
"os"
88
"os/exec"
9+
"strings"
910
)
1011

1112
// Function to get SSL/TLS certificates using Certbot
@@ -31,27 +32,92 @@ func GetCertificates(domainName string) bool {
3132
result, _ := prompt.Show()
3233

3334
if result == "no" {
35+
pterm.Println()
3436
return false
3537
}
3638

3739
pterm.Println()
38-
pterm.Println(pterm.Yellow("Leave email empty if you don't want to receive notifications from Let's Encrypt about your SSL/TLS certificates."))
40+
certbotSpinner, _ := pterm.DefaultSpinner.Start("Checking for Certbot email...")
3941

40-
pterm.Println()
41-
email, _ := pterm.DefaultInteractiveTextInput.Show("Email address")
42-
pterm.Println()
42+
out, err := exec.Command("certbot", "show_account").Output()
43+
44+
if err != nil {
45+
pterm.Println()
46+
pterm.Error.Println(fmt.Sprintf("Failed to retrieve Certbot account data: %v", err))
47+
os.Exit(1)
48+
}
49+
50+
certbotAccountData := string(out)
51+
var email string
52+
53+
if strings.Contains(certbotAccountData, "Email contact: none") {
54+
pterm.Println()
55+
certbotSpinner.Info("Certbot email currently set to none.")
56+
57+
pterm.Println()
58+
pterm.Println(pterm.Cyan("Set your Certbot email to receive notifications from Let's Encrypt about your SSL/TLS certificates."))
59+
60+
pterm.Println()
61+
pterm.Println(pterm.Yellow("Leave email empty if you don't want to receive notifications."))
62+
63+
pterm.Println()
64+
email, _ = pterm.DefaultInteractiveTextInput.Show("Email address")
65+
66+
err := exec.Command("certbot", "update_account", "--email", email, "--no-eff-email").Run()
67+
if err != nil {
68+
pterm.Println()
69+
pterm.Error.Println(fmt.Sprintf("Failed to set Certbot email: %v", err))
70+
os.Exit(1)
71+
}
72+
} else {
73+
_, currentEmail, _ := strings.Cut(certbotAccountData, "Email contact: ")
74+
pterm.Println()
75+
certbotSpinner.Info(fmt.Sprintf("Email used with Certbot account: %s", currentEmail))
76+
77+
prompt := pterm.InteractiveContinuePrinter{
78+
DefaultValueIndex: 0,
79+
DefaultText: "Do you want to remove or update your Certbot email?",
80+
TextStyle: &ThemeDefault.PrimaryStyle,
81+
Options: []string{"yes", "no"},
82+
OptionsStyle: &ThemeDefault.SuccessMessageStyle,
83+
SuffixStyle: &ThemeDefault.SecondaryStyle,
84+
Delimiter: ": ",
85+
}
86+
87+
result, _ := prompt.Show()
88+
89+
if result == "yes" {
90+
pterm.Println()
91+
pterm.Println(pterm.Cyan("Set your Certbot email to receive notifications from Let's Encrypt about your SSL/TLS certificates."))
4392

44-
spinner, _ := pterm.DefaultSpinner.Start("Checking SSL/TLS certificates...")
93+
pterm.Println()
94+
pterm.Println(pterm.Yellow("Leave email empty if you don't want to receive notifications."))
95+
96+
pterm.Println()
97+
email, _ = pterm.DefaultInteractiveTextInput.Show("Email address")
98+
99+
err := exec.Command("certbot", "update_account", "--email", email, "--no-eff-email").Run()
100+
if err != nil {
101+
pterm.Println()
102+
pterm.Error.Println(fmt.Sprintf("Failed to update Certbot email: %v", err))
103+
os.Exit(1)
104+
}
105+
}
106+
}
107+
108+
pterm.Println()
109+
certificateSpinner, _ := pterm.DefaultSpinner.Start("Checking SSL/TLS certificates...")
45110

46111
// Check if certificates already exist
47112
if files.FileExists(fmt.Sprintf("%s/%s/%s", CertificateDirPath, domainName, FullchainFile)) &&
48113
files.FileExists(fmt.Sprintf("%s/%s/%s", CertificateDirPath, domainName, PrivkeyFile)) &&
49114
files.FileExists(fmt.Sprintf("%s/%s/%s", CertificateDirPath, domainName, ChainFile)) {
50-
spinner.Info("SSL/TLS certificates already exist.")
115+
certificateSpinner.Info("SSL/TLS certificates already exist.")
116+
pterm.Println()
51117
return true
52118
}
53119

54-
spinner.UpdateText("Obtaining SSL/TLS certificates...")
120+
certificateSpinner.UpdateText("Obtaining SSL/TLS certificates...")
55121
if email == "" {
56122
cmd := exec.Command("certbot", "certonly", "--webroot", "-w", fmt.Sprintf("%s/%s", WWWDirPath, domainName), "-d", domainName, "--agree-tos", "--no-eff-email", "-q", "--register-unsafely-without-email")
57123
err := cmd.Run()
@@ -68,6 +134,6 @@ func GetCertificates(domainName string) bool {
68134
}
69135
}
70136

71-
spinner.Success("SSL/TLS certificates obtained successfully.")
137+
certificateSpinner.Success("SSL/TLS certificates obtained successfully.")
72138
return true
73139
}

0 commit comments

Comments
 (0)