Skip to content

Commit 542663f

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

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
"os/user"
2827
"runtime"
@@ -168,39 +167,15 @@ func main() {
168167
AdditionalConfig: *additionalConfig,
169168
}
170169

171-
// If the executable is temporary, copy it to the full path, then restart
172170
if src, err := os.Executable(); err != nil {
173171
panic(err)
174-
} else if strings.Contains(src, "-temp") {
175-
newPath := updater.RemoveTempSuffixFromPath(src)
176-
if err := copyExe(src, newPath); err != nil {
177-
log.Println("Copy error: ", err)
178-
panic(err)
179-
}
180-
Systray.RestartWith(newPath)
172+
} else if restartPath := updater.Start(src); restartPath != "" {
173+
Systray.RestartWith(restartPath)
181174
} else {
182-
// Otherwise copy to a path with -temp suffix
183-
if err := copyExe(src, updater.AddTempSuffixToPath(src)); err != nil {
184-
panic(err)
185-
}
186175
Systray.Start()
187176
}
188177
}
189178

190-
func copyExe(from, to string) error {
191-
data, err := ioutil.ReadFile(from)
192-
if err != nil {
193-
log.Println("Cannot read file: ", from)
194-
return err
195-
}
196-
err = ioutil.WriteFile(to, data, 0755)
197-
if err != nil {
198-
log.Println("Cannot write file: ", to)
199-
return err
200-
}
201-
return nil
202-
}
203-
204179
func loop() {
205180
if *hibernate {
206181
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)