Skip to content

Commit 1d6ac56

Browse files
committed
Moved updater business logic in updater package
1 parent 542663f commit 1d6ac56

File tree

2 files changed

+26
-30
lines changed

2 files changed

+26
-30
lines changed

update.go

+2-26
Original file line numberDiff line numberDiff line change
@@ -30,40 +30,16 @@
3030
package main
3131

3232
import (
33-
"os"
34-
3533
"github.com/arduino/arduino-create-agent/updater"
3634
"github.com/gin-gonic/gin"
3735
)
3836

3937
func updateHandler(c *gin.Context) {
40-
41-
path, err := os.Executable()
42-
43-
if err != nil {
44-
c.JSON(500, gin.H{"error": err.Error()})
45-
return
46-
}
47-
48-
var up = &updater.Updater{
49-
CurrentVersion: version,
50-
APIURL: *updateURL,
51-
BinURL: *updateURL,
52-
DiffURL: "",
53-
Dir: "update/",
54-
CmdName: *appName,
55-
}
56-
57-
err = up.BackgroundRun()
58-
38+
restartPath, err := updater.CheckForUpdates(version, *updateURL, *updateURL, *appName)
5939
if err != nil {
6040
c.JSON(500, gin.H{"error": err.Error()})
6141
return
6242
}
63-
64-
path = updater.AddTempSuffixToPath(path)
65-
6643
c.JSON(200, gin.H{"success": "Please wait a moment while the agent reboots itself"})
67-
68-
Systray.RestartWith(path)
44+
Systray.RestartWith(restartPath)
6945
}

updater/updater.go

+24-4
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,32 @@ func Start(src string) string {
8080
}
8181

8282
// Otherwise copy to a path with -temp suffix
83-
if err := copyExe(src, AddTempSuffixToPath(src)); err != nil {
83+
if err := copyExe(src, addTempSuffixToPath(src)); err != nil {
8484
panic(err)
8585
}
8686
return ""
8787
}
8888

89+
func CheckForUpdates(currentVersion string, updateAPIURL, updateBinURL string, cmdName string) (string, error) {
90+
path, err := os.Executable()
91+
if err != nil {
92+
return "", err
93+
}
94+
var up = &Updater{
95+
CurrentVersion: currentVersion,
96+
APIURL: updateAPIURL,
97+
BinURL: updateBinURL,
98+
DiffURL: "",
99+
Dir: "update/",
100+
CmdName: cmdName,
101+
}
102+
103+
if err := up.BackgroundRun(); err != nil {
104+
return "", err
105+
}
106+
return addTempSuffixToPath(path), nil
107+
}
108+
89109
func copyExe(from, to string) error {
90110
data, err := os.ReadFile(from)
91111
if err != nil {
@@ -100,8 +120,8 @@ func copyExe(from, to string) error {
100120
return nil
101121
}
102122

103-
// AddTempSuffixToPath adds the "-temp" suffix to the path to an executable file (a ".exe" extension is replaced with "-temp.exe")
104-
func AddTempSuffixToPath(path string) string {
123+
// addTempSuffixToPath adds the "-temp" suffix to the path to an executable file (a ".exe" extension is replaced with "-temp.exe")
124+
func addTempSuffixToPath(path string) string {
105125
if filepath.Ext(path) == "exe" {
106126
path = strings.Replace(path, ".exe", "-temp.exe", -1)
107127
} else {
@@ -266,7 +286,7 @@ func (u *Updater) update() error {
266286
return err
267287
}
268288

269-
path = AddTempSuffixToPath(path)
289+
path = addTempSuffixToPath(path)
270290

271291
old, err := os.Open(path)
272292
if err != nil {

0 commit comments

Comments
 (0)