6
6
"github.com/pterm/pterm"
7
7
"os"
8
8
"os/exec"
9
+ "strings"
9
10
)
10
11
11
12
// Function to get SSL/TLS certificates using Certbot
@@ -31,27 +32,92 @@ func GetCertificates(domainName string) bool {
31
32
result , _ := prompt .Show ()
32
33
33
34
if result == "no" {
35
+ pterm .Println ()
34
36
return false
35
37
}
36
38
37
39
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..." )
39
41
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." ))
43
92
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..." )
45
110
46
111
// Check if certificates already exist
47
112
if files .FileExists (fmt .Sprintf ("%s/%s/%s" , CertificateDirPath , domainName , FullchainFile )) &&
48
113
files .FileExists (fmt .Sprintf ("%s/%s/%s" , CertificateDirPath , domainName , PrivkeyFile )) &&
49
114
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 ()
51
117
return true
52
118
}
53
119
54
- spinner .UpdateText ("Obtaining SSL/TLS certificates..." )
120
+ certificateSpinner .UpdateText ("Obtaining SSL/TLS certificates..." )
55
121
if email == "" {
56
122
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" )
57
123
err := cmd .Run ()
@@ -68,6 +134,6 @@ func GetCertificates(domainName string) bool {
68
134
}
69
135
}
70
136
71
- spinner .Success ("SSL/TLS certificates obtained successfully." )
137
+ certificateSpinner .Success ("SSL/TLS certificates obtained successfully." )
72
138
return true
73
139
}
0 commit comments