Skip to content

Commit 4e6a378

Browse files
Avoid code duplication
1 parent ea2c6a2 commit 4e6a378

File tree

3 files changed

+16
-31
lines changed

3 files changed

+16
-31
lines changed

certificates/certificates.go

+12-7
Original file line numberDiff line numberDiff line change
@@ -302,14 +302,19 @@ func PromptExpiredCerts(certDir *paths.Path) {
302302
log.Errorf("cannot uninstall certificates something went wrong: %s", err)
303303
} else {
304304
DeleteCertificates(certDir)
305-
GenerateCertificates(certDir)
306-
err := InstallCertificate(certDir.Join("ca.cert.cer"))
307-
// if something goes wrong during the cert install we remove them, so the user is able to retry
308-
if err != nil {
309-
log.Errorf("cannot install certificates something went wrong: %s", err)
310-
DeleteCertificates(certDir)
311-
}
305+
GenerateAndInstallCertificates(certDir)
312306
}
313307
}
314308
}
315309
}
310+
311+
// GenerateAndInstallCertificates generates and installs the certificates
312+
func GenerateAndInstallCertificates(certDir *paths.Path) {
313+
GenerateCertificates(certDir)
314+
err := InstallCertificate(certDir.Join("ca.cert.cer"))
315+
// if something goes wrong during the cert install we remove them, so the user is able to retry
316+
if err != nil {
317+
log.Errorf("cannot install certificates something went wrong: %s", err)
318+
DeleteCertificates(certDir)
319+
}
320+
}

main.go

+2-16
Original file line numberDiff line numberDiff line change
@@ -237,14 +237,7 @@ func loop() {
237237
if err != nil {
238238
log.Panicf("config.ini cannot be parsed: %s", err)
239239
}
240-
certDir := config.GetCertificatesDir()
241-
cert.GenerateCertificates(certDir)
242-
err := cert.InstallCertificate(certDir.Join("ca.cert.cer"))
243-
// if something goes wrong during the cert install we remove them, so the user is able to retry
244-
if err != nil {
245-
log.Errorf("cannot install certificates something went wrong: %s", err)
246-
cert.DeleteCertificates(certDir)
247-
}
240+
cert.GenerateAndInstallCertificates(config.GetCertificatesDir())
248241
} else {
249242
err = config.SetInstallCertsIni(configPath.String(), "false")
250243
if err != nil {
@@ -384,14 +377,7 @@ func loop() {
384377
} else if cert.PromptInstallCertsSafari() {
385378
// installing the certificates from scratch at this point should only happen if
386379
// something went wrong during previous installation attempts
387-
certDir := config.GetCertificatesDir()
388-
cert.GenerateCertificates(certDir)
389-
err := cert.InstallCertificate(certDir.Join("ca.cert.cer"))
390-
// if something goes wrong during the cert install we remove them, so the user is able to retry
391-
if err != nil {
392-
log.Errorf("cannot install certificates something went wrong: %s", err)
393-
cert.DeleteCertificates(certDir)
394-
}
380+
cert.GenerateAndInstallCertificates(config.GetCertificatesDir())
395381
} else {
396382
err = config.SetInstallCertsIni(configPath.String(), "false")
397383
if err != nil {

systray/systray_real.go

+2-8
Original file line numberDiff line numberDiff line change
@@ -109,14 +109,8 @@ func (s *Systray) start() {
109109
}
110110
pressedButton := utilities.UserPrompt("display dialog \"" + infoMsg + "\" buttons " + buttons + " with title \"Arduino Agent: manage HTTPS certificates\"")
111111
if strings.Contains(pressedButton, "Install certificate for Safari") {
112-
cert.GenerateCertificates(certDir)
113-
err := cert.InstallCertificate(certDir.Join("ca.cert.cer"))
114-
// if something goes wrong during the cert install we remove them, so the user is able to retry
115-
if err != nil {
116-
log.Errorf("cannot install certificates something went wrong: %s", err)
117-
cert.DeleteCertificates(certDir)
118-
}
119-
err = config.SetInstallCertsIni(s.currentConfigFilePath.String(), "true")
112+
cert.GenerateAndInstallCertificates(certDir)
113+
err := config.SetInstallCertsIni(s.currentConfigFilePath.String(), "true")
120114
if err != nil {
121115
log.Errorf("cannot set installCerts value in config.ini: %s", err)
122116
}

0 commit comments

Comments
 (0)