Skip to content

Commit 52961e2

Browse files
Move osascript execution function to Utilities to avoid code duplication
1 parent 66ba136 commit 52961e2

File tree

5 files changed

+22
-25
lines changed

5 files changed

+22
-25
lines changed

certificates/certificates.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@ import (
3030
"math/big"
3131
"net"
3232
"os"
33-
"os/exec"
33+
"strings"
3434
"time"
3535

36+
"github.com/arduino/arduino-create-agent/utilities"
3637
"github.com/arduino/go-paths-helper"
3738
log "github.com/sirupsen/logrus"
3839
)
@@ -285,18 +286,17 @@ func PromptInstallCertsSafari() bool {
285286
if GetDefaultBrowserName() != "Safari" {
286287
return false
287288
}
288-
oscmd := exec.Command("osascript", "-e", "display dialog \"The Arduino Agent needs a local HTTPS certificate to work correctly with Safari.\nIf you use Safari, you need to install it.\" buttons {\"Do not install\", \"Install the certificate for Safari\"} default button 2 with title \"Arduino Agent: Install Certificates\"")
289-
pressed, _ := oscmd.Output()
290-
return strings.Contains(string(pressed), "button returned:Install the certificate for Safari")
289+
buttonPressed := utilities.UserPrompt("display dialog \"The Arduino Agent needs a local HTTPS certificate to work correctly with Safari.\nIf you use Safari, you need to install it.\" buttons {\"Do not install\", \"Install the certificate for Safari\"} default button 2 with title \"Arduino Agent: Install Certificates\"")
290+
return strings.Contains(string(buttonPressed), "button returned:Install the certificate for Safari")
291291
}
292292

293293
// PromptExpiredCerts prompts the user to update the HTTPS certificates if they are using Safari
294294
func PromptExpiredCerts(certDir *paths.Path) {
295295
if expired, err := isExpired(); err != nil {
296296
log.Errorf("cannot check if certificates are expired something went wrong: %s", err)
297297
} else if expired {
298-
oscmd := exec.Command("osascript", "-e", "display dialog \"The Arduino Agent needs a local HTTPS certificate to work correctly with Safari.\nYour certificate is expired or close to expiration. Do you want to update it?\" buttons {\"Do not update\", \"Update the certificate for Safari\"} default button 2 with title \"Arduino Agent: Update Certificates\"")
299-
if pressed, _ := oscmd.Output(); strings.Contains(string(pressed), "button returned:Update the certificate for Safari") {
298+
buttonPressed := utilities.UserPrompt("display dialog \"The Arduino Agent needs a local HTTPS certificate to work correctly with Safari.\nYour certificate is expired or close to expiration. Do you want to update it?\" buttons {\"Do not update\", \"Update the certificate for Safari\"} default button 2 with title \"Arduino Agent: Update Certificates\"")
299+
if strings.Contains(string(buttonPressed), "button returned:Update the certificate for Safari") {
300300
err := UninstallCertificates()
301301
if err != nil {
302302
log.Errorf("cannot uninstall certificates something went wrong: %s", err)

certificates/install_darwin.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -154,12 +154,12 @@ const char *getDefaultBrowserName() {
154154
import "C"
155155
import (
156156
"errors"
157-
"os/exec"
158157
"strings"
159158
"unsafe"
160159

161160
log "github.com/sirupsen/logrus"
162161

162+
"github.com/arduino/arduino-create-agent/utilities"
163163
"github.com/arduino/go-paths-helper"
164164
)
165165

@@ -172,9 +172,8 @@ func InstallCertificate(cert *paths.Path) error {
172172
p := C.installCert(ccert)
173173
s := C.GoString(p)
174174
if len(s) != 0 {
175-
oscmd := exec.Command("osascript", "-e", "display dialog \""+s+"\" buttons \"OK\" with title \"Arduino Agent: Error installing certificates\"")
176-
_ = oscmd.Run()
177-
_ = UninstallCertificates()
175+
utilities.UserPrompt("display dialog \"" + s + "\" buttons \"OK\" with title \"Arduino Agent: Error installing certificates\"")
176+
UninstallCertificates()
178177
return errors.New(s)
179178
}
180179
return nil
@@ -187,8 +186,7 @@ func UninstallCertificates() error {
187186
p := C.uninstallCert()
188187
s := C.GoString(p)
189188
if len(s) != 0 {
190-
oscmd := exec.Command("osascript", "-e", "display dialog \""+s+"\" buttons \"OK\" with title \"Arduino Agent: Error uninstalling certificates\"")
191-
_ = oscmd.Run()
189+
utilities.UserPrompt("display dialog \"" + s + "\" buttons \"OK\" with title \"Arduino Agent: Error uninstalling certificates\"")
192190
return errors.New(s)
193191
}
194192
return nil
@@ -202,8 +200,7 @@ func GetExpirationDate() (string, error) {
202200
p := C.getExpirationDate(dateString)
203201
s := C.GoString(p)
204202
if len(s) != 0 {
205-
oscmd := exec.Command("osascript", "-e", "display dialog \""+s+"\" buttons \"OK\" with title \"Arduino Agent: Error retrieving expiration date\"")
206-
_ = oscmd.Run()
203+
utilities.UserPrompt("display dialog \"" + s + "\" buttons \"OK\" with title \"Arduino Agent: Error retrieving expiration date\"")
207204
return "", errors.New(s)
208205
}
209206
date := C.GoString(dateString)

main.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
"html/template"
2626
"io"
2727
"os"
28-
"os/exec"
2928
"regexp"
3029
"runtime"
3130
"runtime/debug"
@@ -40,6 +39,7 @@ import (
4039
"github.com/arduino/arduino-create-agent/systray"
4140
"github.com/arduino/arduino-create-agent/tools"
4241
"github.com/arduino/arduino-create-agent/updater"
42+
"github.com/arduino/arduino-create-agent/utilities"
4343
v2 "github.com/arduino/arduino-create-agent/v2"
4444
paths "github.com/arduino/go-paths-helper"
4545
cors "github.com/gin-contrib/cors"
@@ -178,7 +178,7 @@ func loop() {
178178
// If we are updating manually from 1.2.7 to 1.3.0 we have to uninstall the old agent manually first.
179179
// This check will inform the user if he needs to run the uninstall first
180180
if runtime.GOOS == "darwin" && oldInstallExists() {
181-
printDialog("Old agent installation of the Arduino Create Agent found, please uninstall it before launching the new one")
181+
utilities.UserPrompt("display dialog \"Old agent installation of the Arduino Create Agent found, please uninstall it before launching the new one\" buttons \"OK\" with title \"Error\"")
182182
os.Exit(0)
183183
}
184184

@@ -498,12 +498,6 @@ func oldInstallExists() bool {
498498
return oldAgentPath.Join("ArduinoCreateAgent.app").Exist()
499499
}
500500

501-
// printDialog will print a GUI error dialog on macos
502-
func printDialog(dialogText string) {
503-
oscmd := exec.Command("osascript", "-e", "display dialog \""+dialogText+"\" buttons \"OK\" with title \"Error\"")
504-
_ = oscmd.Run()
505-
}
506-
507501
func parseIni(filename string) (args []string, err error) {
508502
cfg, err := ini.LoadSources(ini.LoadOptions{IgnoreInlineComment: false, AllowPythonMultilineValues: true}, filename)
509503
if err != nil {

systray/systray_real.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ package systray
2121

2222
import (
2323
"os"
24-
"os/exec"
2524
"runtime"
2625

2726
"fyne.io/systray"
2827
cert "github.com/arduino/arduino-create-agent/certificates"
2928
"github.com/arduino/arduino-create-agent/config"
3029
"github.com/arduino/arduino-create-agent/icon"
30+
"github.com/arduino/arduino-create-agent/utilities"
3131
"github.com/go-ini/ini"
3232
log "github.com/sirupsen/logrus"
3333
"github.com/skratchdot/open-golang/open"
@@ -133,8 +133,7 @@ func (s *Systray) start() {
133133
} else {
134134
infoMsg = infoMsg + "- Certificate installed: No\n- Certificate trusted: N/A\n- Certificate expiration date: N/A"
135135
}
136-
oscmd := exec.Command("osascript", "-e", "display dialog \""+infoMsg+"\" buttons \"OK\" with title \"Arduino Agent: certificates info\"")
137-
_ = oscmd.Run()
136+
utilities.UserPrompt("display dialog \"" + infoMsg + "\" buttons \"OK\" with title \"Arduino Agent: certificates info\"")
138137
case <-mPause.ClickedCh:
139138
s.Pause()
140139
case <-mQuit.ClickedCh:

utilities/utilities.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,3 +149,10 @@ func VerifyInput(input string, signature string) error {
149149
d := h.Sum(nil)
150150
return rsa.VerifyPKCS1v15(rsaKey, crypto.SHA256, d, sign)
151151
}
152+
153+
// UserPrompt executes an osascript and returns the pressed button
154+
func UserPrompt(dialog string) string {
155+
oscmd := exec.Command("osascript", "-e", dialog)
156+
pressedButton, _ := oscmd.Output()
157+
return string(pressedButton)
158+
}

0 commit comments

Comments
 (0)