Skip to content

Commit a75672f

Browse files
Move osascript execution function to Utilities to avoid code duplication
1 parent fb4981a commit a75672f

File tree

5 files changed

+21
-25
lines changed

5 files changed

+21
-25
lines changed

Diff for: certificates/certificates.go

+5-6
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ import (
3030
"math/big"
3131
"net"
3232
"os"
33-
"os/exec"
3433
"strings"
3534
"time"
3635

36+
"github.com/arduino/arduino-create-agent/utilities"
3737
"github.com/arduino/go-paths-helper"
3838
log "github.com/sirupsen/logrus"
3939
)
@@ -286,18 +286,17 @@ func PromptInstallCertsSafari() bool {
286286
if GetDefaultBrowserName() != "Safari" {
287287
return false
288288
}
289-
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\"")
290-
pressed, _ := oscmd.Output()
291-
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")
292291
}
293292

294293
// PromptExpiredCerts prompts the user to update the HTTPS certificates if they are using Safari
295294
func PromptExpiredCerts(certDir *paths.Path) {
296295
if expired, err := isExpired(); err != nil {
297296
log.Errorf("cannot check if certificates are expired something went wrong: %s", err)
298297
} else if expired {
299-
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\"")
300-
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") {
301300
err := UninstallCertificates()
302301
if err != nil {
303302
log.Errorf("cannot uninstall certificates something went wrong: %s", err)

Diff for: certificates/install_darwin.go

+5-8
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,11 @@ const char *getDefaultBrowserName() {
154154
import "C"
155155
import (
156156
"errors"
157-
"os/exec"
158157
"unsafe"
159158

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

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

@@ -171,9 +171,8 @@ func InstallCertificate(cert *paths.Path) error {
171171
p := C.installCert(ccert)
172172
s := C.GoString(p)
173173
if len(s) != 0 {
174-
oscmd := exec.Command("osascript", "-e", "display dialog \""+s+"\" buttons \"OK\" with title \"Arduino Agent: Error installing certificates\"")
175-
_ = oscmd.Run()
176-
_ = UninstallCertificates()
174+
utilities.UserPrompt("display dialog \"" + s + "\" buttons \"OK\" with title \"Arduino Agent: Error installing certificates\"")
175+
UninstallCertificates()
177176
return errors.New(s)
178177
}
179178
return nil
@@ -186,8 +185,7 @@ func UninstallCertificates() error {
186185
p := C.uninstallCert()
187186
s := C.GoString(p)
188187
if len(s) != 0 {
189-
oscmd := exec.Command("osascript", "-e", "display dialog \""+s+"\" buttons \"OK\" with title \"Arduino Agent: Error uninstalling certificates\"")
190-
_ = oscmd.Run()
188+
utilities.UserPrompt("display dialog \"" + s + "\" buttons \"OK\" with title \"Arduino Agent: Error uninstalling certificates\"")
191189
return errors.New(s)
192190
}
193191
return nil
@@ -201,8 +199,7 @@ func GetExpirationDate() (string, error) {
201199
p := C.getExpirationDate(dateString)
202200
s := C.GoString(p)
203201
if len(s) != 0 {
204-
oscmd := exec.Command("osascript", "-e", "display dialog \""+s+"\" buttons \"OK\" with title \"Arduino Agent: Error retrieving expiration date\"")
205-
_ = oscmd.Run()
202+
utilities.UserPrompt("display dialog \"" + s + "\" buttons \"OK\" with title \"Arduino Agent: Error retrieving expiration date\"")
206203
return "", errors.New(s)
207204
}
208205
date := C.GoString(dateString)

Diff for: main.go

+2-8
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 {

Diff for: systray/systray_real.go

+2-3
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:

Diff for: utilities/utilities.go

+7
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)