Skip to content

Commit 620dcf5

Browse files
author
Quentin Perez
committed
Add file for check if an update is available
1 parent b8bf0f6 commit 620dcf5

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

pkg/cli/main.go

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"fmt"
99
"io/ioutil"
1010
"os"
11+
"path/filepath"
12+
"time"
1113

1214
"github.com/Sirupsen/logrus"
1315
flag "github.com/docker/docker/pkg/mflag"
@@ -31,14 +33,14 @@ var (
3133

3234
// Start is the entrypoint
3335
func Start(rawArgs []string, streams *commands.Streams) (int, error) {
36+
checkVersion()
3437
if streams == nil {
3538
streams = &commands.Streams{
3639
Stdin: os.Stdin,
3740
Stdout: os.Stdout,
3841
Stderr: os.Stderr,
3942
}
4043
}
41-
4244
flag.CommandLine.Parse(rawArgs)
4345

4446
config, cfgErr := config.GetConfig()
@@ -159,3 +161,38 @@ func initLogging(debug bool, verbose bool, streams *commands.Streams) {
159161
logrus.SetLevel(logrus.WarnLevel)
160162
}
161163
}
164+
165+
func checkVersion() {
166+
homeDir, err := config.GetHomeDir()
167+
if err != nil {
168+
return
169+
}
170+
updateFiles := []string{"/var/run/.scw-update", "/tmp/.scw-update", filepath.Join(homeDir, ".scw-update")}
171+
updateFile := ""
172+
173+
callAPI := false
174+
for _, file := range updateFiles {
175+
if stat, err := os.Stat(file); err == nil {
176+
updateFile = file
177+
callAPI = stat.ModTime().Before(time.Now().AddDate(0, 0, -1))
178+
break
179+
}
180+
}
181+
if updateFile == "" {
182+
for _, file := range updateFiles {
183+
if scwupdate, err := os.OpenFile(file, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0600); err == nil {
184+
scwupdate.Close()
185+
updateFile = file
186+
callAPI = true
187+
break
188+
}
189+
}
190+
}
191+
if callAPI {
192+
scwupdate, err := os.OpenFile(updateFile, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0600)
193+
if err != nil {
194+
return
195+
}
196+
scwupdate.Close()
197+
}
198+
}

0 commit comments

Comments
 (0)