Skip to content

Commit e71d1f0

Browse files
committed
Renamed some functions for clarity / removed some globals
1 parent 99efa45 commit e71d1f0

File tree

3 files changed

+15
-17
lines changed

3 files changed

+15
-17
lines changed

main.go

+9-11
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,6 @@ var (
5454

5555
// the important folders of the agent
5656
var (
57-
src, _ = os.Executable()
58-
srcPath = paths.New(src) // The path of the agent's binary
59-
srcDir = srcPath.Parent() // The directory of the agent's binary
6057
usr, _ = user.Current()
6158
usrDir = paths.New(usr.HomeDir) // The user folder, on linux/macos /home/<usr>/
6259
agentDir = usrDir.Join(".arduino-create")
@@ -172,19 +169,18 @@ func main() {
172169
}
173170

174171
// If the executable is temporary, copy it to the full path, then restart
175-
if strings.Contains(srcPath.String(), "-temp") {
176-
newPath := updater.BinPath(srcPath.String())
177-
err := copyExe(srcPath.String(), newPath)
178-
if err != nil {
172+
if src, err := os.Executable(); err != nil {
173+
panic(err)
174+
} else if strings.Contains(src, "-temp") {
175+
newPath := updater.RemoveTempSuffixFromPath(src)
176+
if err := copyExe(src, newPath); err != nil {
179177
log.Println("Copy error: ", err)
180178
panic(err)
181179
}
182-
183180
Systray.RestartWith(newPath)
184181
} else {
185182
// Otherwise copy to a path with -temp suffix
186-
err := copyExe(srcPath.String(), updater.TempPath(srcPath.String()))
187-
if err != nil {
183+
if err := copyExe(src, updater.AddTempSuffixToPath(src)); err != nil {
188184
panic(err)
189185
}
190186
Systray.Start()
@@ -230,7 +226,9 @@ func loop() {
230226
log.Infof("using config from default: %s", configPath)
231227
// take the config from the old folder where the agent's binary sits
232228
} else {
233-
oldConfigPath := srcDir.Join("config.ini")
229+
// Fall back to the old config.ini location
230+
src, _ := os.Executable()
231+
oldConfigPath := paths.New(src).Parent().Join("config.ini")
234232
if oldConfigPath.Exist() {
235233
err := oldConfigPath.CopyTo(agentDir.Join("config.ini"))
236234
if err != nil {

update.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func updateHandler(c *gin.Context) {
6161
return
6262
}
6363

64-
path = updater.TempPath(path)
64+
path = updater.AddTempSuffixToPath(path)
6565

6666
c.JSON(200, gin.H{"success": "Please wait a moment while the agent reboots itself"})
6767

updater/updater.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ var errHashMismatch = errors.New("new file hash mismatch after patch")
6868
var errDiffURLUndefined = errors.New("DiffURL is not defined, I cannot fetch and apply patch, reverting to full bin")
6969
var up = update.New()
7070

71-
// TempPath generates a temporary path for the executable (adding "-temp")
72-
func TempPath(path string) string {
71+
// AddTempSuffixToPath adds the "-temp" suffix to the path to an executable file (a ".exe" extension is replaced with "-temp.exe")
72+
func AddTempSuffixToPath(path string) string {
7373
if filepath.Ext(path) == "exe" {
7474
path = strings.Replace(path, ".exe", "-temp.exe", -1)
7575
} else {
@@ -79,8 +79,8 @@ func TempPath(path string) string {
7979
return path
8080
}
8181

82-
// BinPath generates the proper path for a temporary executable (removing "-temp")
83-
func BinPath(path string) string {
82+
// RemoveTempSuffixFromPath removes "-temp" suffix from the path to an executable file (a "-temp.exe" extension is replaced with ".exe")
83+
func RemoveTempSuffixFromPath(path string) string {
8484
return strings.Replace(path, "-temp", "", -1)
8585
}
8686

@@ -234,7 +234,7 @@ func (u *Updater) update() error {
234234
return err
235235
}
236236

237-
path = TempPath(path)
237+
path = AddTempSuffixToPath(path)
238238

239239
old, err := os.Open(path)
240240
if err != nil {

0 commit comments

Comments
 (0)