Skip to content

Commit 5ba151d

Browse files
committed
Moved updater business logic in updater package
1 parent 3d7780f commit 5ba151d

File tree

2 files changed

+36
-29
lines changed

2 files changed

+36
-29
lines changed

main.go

+2-27
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
_ "embed"
2323
"encoding/json"
2424
"flag"
25-
"io/ioutil"
2625
"os"
2726
"runtime"
2827
"runtime/debug"
@@ -149,39 +148,15 @@ func main() {
149148
ConfigDir: configDir,
150149
}
151150

152-
// If the executable is temporary, copy it to the full path, then restart
153151
if src, err := os.Executable(); err != nil {
154152
panic(err)
155-
} else if strings.Contains(src, "-temp") {
156-
newPath := updater.RemoveTempSuffixFromPath(src)
157-
if err := copyExe(src, newPath); err != nil {
158-
log.Println("Copy error: ", err)
159-
panic(err)
160-
}
161-
Systray.RestartWith(newPath)
153+
} else if restartPath := updater.Start(src); restartPath != "" {
154+
Systray.RestartWith(restartPath)
162155
} else {
163-
// Otherwise copy to a path with -temp suffix
164-
if err := copyExe(src, updater.AddTempSuffixToPath(src)); err != nil {
165-
panic(err)
166-
}
167156
Systray.Start()
168157
}
169158
}
170159

171-
func copyExe(from, to string) error {
172-
data, err := ioutil.ReadFile(from)
173-
if err != nil {
174-
log.Println("Cannot read file: ", from)
175-
return err
176-
}
177-
err = ioutil.WriteFile(to, data, 0755)
178-
if err != nil {
179-
log.Println("Cannot write file: ", to)
180-
return err
181-
}
182-
return nil
183-
}
184-
185160
func loop() {
186161
if *hibernate {
187162
return

updater/updater.go

+34-2
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,38 @@ 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+
func Start(src string) string {
72+
// If the executable is temporary, copy it to the full path, then restart
73+
if strings.Contains(src, "-temp") {
74+
newPath := removeTempSuffixFromPath(src)
75+
if err := copyExe(src, newPath); err != nil {
76+
log.Println("Copy error: ", err)
77+
panic(err)
78+
}
79+
return newPath
80+
}
81+
82+
// Otherwise copy to a path with -temp suffix
83+
if err := copyExe(src, AddTempSuffixToPath(src)); err != nil {
84+
panic(err)
85+
}
86+
return ""
87+
}
88+
89+
func copyExe(from, to string) error {
90+
data, err := os.ReadFile(from)
91+
if err != nil {
92+
log.Println("Cannot read file: ", from)
93+
return err
94+
}
95+
err = os.WriteFile(to, data, 0755)
96+
if err != nil {
97+
log.Println("Cannot write file: ", to)
98+
return err
99+
}
100+
return nil
101+
}
102+
71103
// AddTempSuffixToPath adds the "-temp" suffix to the path to an executable file (a ".exe" extension is replaced with "-temp.exe")
72104
func AddTempSuffixToPath(path string) string {
73105
if filepath.Ext(path) == "exe" {
@@ -79,8 +111,8 @@ func AddTempSuffixToPath(path string) string {
79111
return path
80112
}
81113

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 {
114+
// removeTempSuffixFromPath removes "-temp" suffix from the path to an executable file (a "-temp.exe" extension is replaced with ".exe")
115+
func removeTempSuffixFromPath(path string) string {
84116
return strings.Replace(path, "-temp", "", -1)
85117
}
86118

0 commit comments

Comments
 (0)