Skip to content

Commit fd8b606

Browse files
committed
refactor: extract TempPath and BinPath funcs
1 parent 7f3e25a commit fd8b606

File tree

3 files changed

+22
-22
lines changed

3 files changed

+22
-22
lines changed

Diff for: main.go

+3-9
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919

2020
"github.com/arduino/arduino-create-agent/systray"
2121
"github.com/arduino/arduino-create-agent/tools"
22+
"github.com/arduino/arduino-create-agent/updater"
2223
"github.com/arduino/arduino-create-agent/utilities"
2324
v2 "github.com/arduino/arduino-create-agent/v2"
2425
"github.com/gin-gonic/gin"
@@ -130,22 +131,15 @@ func main() {
130131

131132
// If the executable is temporary, copy it to the full path, then restart
132133
if strings.Contains(path, "-temp") {
133-
correctPath := strings.Replace(path, "-temp", "", -1)
134-
err := copyExe(path, correctPath)
134+
err := copyExe(path, updater.BinPath(path))
135135
if err != nil {
136136
panic(err)
137137
}
138138

139139
Systray.Restart()
140140
} else {
141141
// Otherwise copy to a path with -temp suffix
142-
correctPath := path
143-
if filepath.Ext(path) == "exe" {
144-
path = strings.Replace(path, ".exe", "-temp.exe", -1)
145-
} else {
146-
path = path + "-temp"
147-
}
148-
err := copyExe(path, correctPath)
142+
err := copyExe(path, updater.TempPath(path))
149143
if err != nil {
150144
panic(err)
151145
}

Diff for: update.go

+1-8
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,6 @@
3030
package main
3131

3232
import (
33-
"path/filepath"
34-
"strings"
35-
3633
"github.com/arduino/arduino-create-agent/updater"
3734
"github.com/gin-gonic/gin"
3835
"github.com/kardianos/osext"
@@ -63,11 +60,7 @@ func updateHandler(c *gin.Context) {
6360
return
6461
}
6562

66-
if filepath.Ext(path) == "exe" {
67-
path = strings.Replace(path, ".exe", "-temp.exe", -1)
68-
} else {
69-
path = path + "-temp"
70-
}
63+
path = updater.TempPath(path)
7164

7265
c.JSON(200, gin.H{"success": "Please wait a moment while the agent reboots itself"})
7366
Systray.Update(path)

Diff for: updater/updater.go

+18-5
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,22 @@ const devValidTime = 7 * 24 * time.Hour
5757
var errHashMismatch = errors.New("new file hash mismatch after patch")
5858
var up = update.New()
5959

60+
// TempPath generates a temporary path for the executable
61+
func TempPath(path string) string {
62+
if filepath.Ext(path) == "exe" {
63+
path = strings.Replace(path, ".exe", "-temp.exe", -1)
64+
} else {
65+
path = path + "-temp"
66+
}
67+
68+
return path
69+
}
70+
71+
// TempPath generates the proper path for a temporary executable
72+
func BinPath(path string) string {
73+
return strings.Replace(path, "-temp", "", -1)
74+
}
75+
6076
// Updater is the configuration and runtime data for doing an update.
6177
//
6278
// Note that ApiURL, BinURL and DiffURL should have the same value if all files are available at the same location.
@@ -203,11 +219,8 @@ func (u *Updater) update() error {
203219
if err != nil {
204220
return err
205221
}
206-
if filepath.Ext(path) == "exe" {
207-
path = strings.Replace(path, ".exe", "-temp.exe", -1)
208-
} else {
209-
path = path + "-temp"
210-
}
222+
223+
path = TempPath(path)
211224

212225
old, err := os.Open(path)
213226
if err != nil {

0 commit comments

Comments
 (0)