Skip to content

Commit e8026c9

Browse files
committed
Move updatecheck to pkg
1 parent 7e93ac3 commit e8026c9

6 files changed

Lines changed: 113 additions & 96 deletions

File tree

cmd/http.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
lm "github.com/loophole/cli/internal/app/loophole/models"
1212
"github.com/loophole/cli/internal/pkg/communication"
1313
"github.com/loophole/cli/internal/pkg/token"
14+
"github.com/loophole/cli/internal/pkg/updatecheck"
1415

1516
"github.com/spf13/cobra"
1617
)
@@ -29,7 +30,7 @@ To expose port running on some local host e.g. 192.168.1.20 use 'loophole http <
2930
idToken := token.GetIdToken()
3031
communication.ApplicationStart(loggedIn, idToken)
3132

32-
loophole.CheckVersion()
33+
updatecheck.CheckVersion()
3334

3435
localEndpointSpecs.Host = "127.0.0.1"
3536
if len(args) > 1 {

cmd/path.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
lm "github.com/loophole/cli/internal/app/loophole/models"
1010
"github.com/loophole/cli/internal/pkg/communication"
1111
"github.com/loophole/cli/internal/pkg/token"
12+
"github.com/loophole/cli/internal/pkg/updatecheck"
1213
"github.com/spf13/cobra"
1314
)
1415

@@ -26,7 +27,7 @@ To expose local directory (e.g. /data/my-data) simply use 'loophole path /data/m
2627
idToken := token.GetIdToken()
2728
communication.ApplicationStart(loggedIn, idToken)
2829

29-
loophole.CheckVersion()
30+
updatecheck.CheckVersion()
3031

3132
dirEndpointSpecs.Path = args[0]
3233
quitChannel := make(chan bool)

cmd/webdav.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
lm "github.com/loophole/cli/internal/app/loophole/models"
1010
"github.com/loophole/cli/internal/pkg/communication"
1111
"github.com/loophole/cli/internal/pkg/token"
12+
"github.com/loophole/cli/internal/pkg/updatecheck"
1213

1314
"github.com/spf13/cobra"
1415
)
@@ -28,7 +29,7 @@ To expose local directory via webdav (e.g. /data/my-data) simply use 'loophole w
2829
idToken := token.GetIdToken()
2930
communication.ApplicationStart(loggedIn, idToken)
3031

31-
loophole.CheckVersion()
32+
updatecheck.CheckVersion()
3233

3334
webdavEndpointSpecs.Path = args[0]
3435
quitChannel := make(chan bool)

internal/app/loophole/loophole.go

Lines changed: 0 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,15 @@ import (
55
"io"
66
"net"
77
"net/http"
8-
"runtime"
98
"time"
109

11-
"github.com/blang/semver/v4"
1210
"github.com/loophole/cli/config"
1311
lm "github.com/loophole/cli/internal/app/loophole/models"
1412
"github.com/loophole/cli/internal/pkg/apiclient"
1513
"github.com/loophole/cli/internal/pkg/communication"
1614
"github.com/loophole/cli/internal/pkg/httpserver"
1715
"github.com/loophole/cli/internal/pkg/keys"
1816
"github.com/loophole/cli/internal/pkg/urlmaker"
19-
"github.com/mitchellh/go-homedir"
20-
"github.com/ncruces/zenity"
21-
"github.com/pkg/browser"
22-
"github.com/spf13/viper"
2317
"golang.org/x/crypto/ssh"
2418
)
2519

@@ -41,92 +35,6 @@ var remoteEndpoint = lm.Endpoint{
4135
Port: 80,
4236
}
4337

44-
func CheckVersion() {
45-
availableVersion, err := apiclient.GetLatestAvailableVersion()
46-
if err != nil {
47-
communication.Debug("There was a problem obtaining info response, skipping further checking")
48-
return
49-
}
50-
currentVersionParsed, err := semver.Make(config.Config.Version)
51-
if err != nil {
52-
communication.Debug(fmt.Sprintf("Cannot parse current version '%s' as semver version, skipping further checking", config.Config.Version))
53-
return
54-
}
55-
availableVersionParsed, err := semver.Make(availableVersion.Version)
56-
if err != nil {
57-
communication.Debug(fmt.Sprintf("Cannot parse available version '%s' as semver version, skipping further checking", availableVersion))
58-
return
59-
}
60-
if currentVersionParsed.LT(availableVersionParsed) {
61-
if config.Config.ClientMode == "cli" {
62-
communication.NewVersionAvailable(availableVersion.Version)
63-
} else {
64-
remind, _ := remindAgainCheck()
65-
if !remind {
66-
return
67-
}
68-
dlLink := getDownloadLink(availableVersion.Version)
69-
response, _ := zenity.Question(fmt.Sprintf("A new version is available for you at \n%s \nDo you want to open the link in your browser now?", dlLink), zenity.NoWrap(), zenity.Title("New version available!"))
70-
if response {
71-
browser.OpenURL(dlLink)
72-
}
73-
}
74-
}
75-
}
76-
77-
func getDownloadLink(availableVersion string) string {
78-
archiveExt := ".tar.gz"
79-
arch := runtime.GOARCH
80-
if arch == "windows" {
81-
archiveExt = ".zip"
82-
} else if arch == "darwin" {
83-
arch = "macos"
84-
}
85-
if arch == "amd64" {
86-
arch = "64bit"
87-
} else if arch == "386" {
88-
arch = "32bit"
89-
} else {
90-
communication.Error("There was an error detecting your system architecture.") //if arch is unexpected, only link to the release page
91-
return fmt.Sprintf("https://github.com/loophole/cli/releases/tag/%s", availableVersion)
92-
}
93-
res := fmt.Sprintf("https://github.com/loophole/cli/releases/download/%s/loophole-desktop_%s_%s_%s%s", availableVersion, availableVersion, runtime.GOOS, arch, archiveExt)
94-
fmt.Println(res)
95-
return res
96-
}
97-
98-
func remindAgainCheck() (bool, error) { //TODO: Error handling, moving this function to a more appropriate file (probably to config pkg)
99-
home, err := homedir.Dir()
100-
if err != nil {
101-
return true, err
102-
}
103-
104-
layout := "2006-02-01" //golangs arcane time format string
105-
viper.SetDefault("last-reminder", time.Time{}.Format(layout)) //zero value for time
106-
viper.SetConfigName("config") // name of config file (without extension)
107-
viper.SetConfigType("json") // REQUIRED if the config file does not have the extension in the name
108-
viper.AddConfigPath(fmt.Sprintf("%s/.loophole/", home))
109-
if err := viper.ReadInConfig(); err != nil {
110-
if _, ok := err.(viper.ConfigFileNotFoundError); ok { //create a config if none exist yet
111-
viper.WriteConfigAs(fmt.Sprintf("%s/.loophole/config.json", home))
112-
} else {
113-
return true, err
114-
}
115-
}
116-
117-
t, err := time.Parse(layout, fmt.Sprintf("%v", viper.Get("last-reminder")))
118-
if err != nil {
119-
return true, err
120-
}
121-
if (t.Year() < time.Now().Year()) || (t.YearDay() < time.Now().YearDay()) { //check if reminder has been done today
122-
viper.Set("last-reminder", time.Now().Format(layout))
123-
viper.WriteConfigAs("/home/work/.loophole/config.json")
124-
return true, nil
125-
}
126-
127-
return false, nil
128-
}
129-
13038
func handleClient(tunnelID string, client net.Conn, local net.Conn) {
13139
defer client.Close()
13240
chDone := make(chan bool)
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package updatecheck
2+
3+
import (
4+
"fmt"
5+
"runtime"
6+
"time"
7+
8+
"github.com/blang/semver/v4"
9+
"github.com/loophole/cli/config"
10+
"github.com/loophole/cli/internal/pkg/apiclient"
11+
"github.com/loophole/cli/internal/pkg/communication"
12+
"github.com/mitchellh/go-homedir"
13+
"github.com/ncruces/zenity"
14+
"github.com/pkg/browser"
15+
"github.com/spf13/viper"
16+
)
17+
18+
func CheckVersion() {
19+
availableVersion, err := apiclient.GetLatestAvailableVersion()
20+
if err != nil {
21+
communication.Debug("There was a problem obtaining info response, skipping further checking")
22+
return
23+
}
24+
currentVersionParsed, err := semver.Make(config.Config.Version)
25+
if err != nil {
26+
communication.Debug(fmt.Sprintf("Cannot parse current version '%s' as semver version, skipping further checking", config.Config.Version))
27+
return
28+
}
29+
availableVersionParsed, err := semver.Make(availableVersion.Version)
30+
if err != nil {
31+
communication.Debug(fmt.Sprintf("Cannot parse available version '%s' as semver version, skipping further checking", availableVersion))
32+
return
33+
}
34+
if currentVersionParsed.LT(availableVersionParsed) {
35+
if config.Config.ClientMode == "cli" {
36+
communication.NewVersionAvailable(availableVersion.Version)
37+
} else {
38+
remind, err := remindAgainCheck()
39+
if err != nil {
40+
communication.Error(err.Error()) //errors in retrieving a download link should be noted, but not interrupt the program
41+
}
42+
if !remind {
43+
return
44+
}
45+
dlLink := getDownloadLink(availableVersion.Version)
46+
response, _ := zenity.Question(fmt.Sprintf("A new version is available for you at \n%s \nDo you want to open the link in your browser now?", dlLink), zenity.NoWrap(), zenity.Title("New version available!"))
47+
if response {
48+
browser.OpenURL(dlLink)
49+
}
50+
}
51+
}
52+
}
53+
54+
func getDownloadLink(availableVersion string) string {
55+
archiveExt := ".tar.gz"
56+
arch := runtime.GOARCH
57+
if arch == "windows" {
58+
archiveExt = ".zip"
59+
} else if arch == "darwin" {
60+
arch = "macos"
61+
}
62+
if arch == "amd64" {
63+
arch = "64bit"
64+
} else if arch == "386" {
65+
arch = "32bit"
66+
} else {
67+
communication.Error("There was an error detecting your system architecture.") //if arch is unexpected, only link to the release page
68+
return fmt.Sprintf("https://github.com/loophole/cli/releases/tag/%s", availableVersion)
69+
}
70+
res := fmt.Sprintf("https://github.com/loophole/cli/releases/download/%s/loophole-desktop_%s_%s_%s%s", availableVersion, availableVersion, runtime.GOOS, arch, archiveExt)
71+
fmt.Println(res)
72+
return res
73+
}
74+
75+
func remindAgainCheck() (bool, error) {
76+
home, err := homedir.Dir()
77+
if err != nil {
78+
return true, err
79+
}
80+
81+
layout := "2006-02-01" //golangs arcane time format string
82+
viper.SetDefault("last-reminder", time.Time{}.Format(layout)) //zero value for time
83+
viper.SetConfigName("config") // name of config file (without extension)
84+
viper.SetConfigType("json") // REQUIRED if the config file does not have the extension in the name
85+
viper.AddConfigPath(fmt.Sprintf("%s/.loophole/", home))
86+
if err := viper.ReadInConfig(); err != nil {
87+
if _, ok := err.(viper.ConfigFileNotFoundError); ok { //create a config if none exist yet
88+
viper.WriteConfigAs(fmt.Sprintf("%s/.loophole/config.json", home))
89+
} else {
90+
return true, err
91+
}
92+
}
93+
94+
t, err := time.Parse(layout, fmt.Sprintf("%v", viper.Get("last-reminder")))
95+
if err != nil {
96+
return true, err
97+
}
98+
if (t.Year() < time.Now().Year()) || (t.YearDay() < time.Now().YearDay()) { //check if reminder has been done today
99+
viper.Set("last-reminder", time.Now().Format(layout))
100+
viper.WriteConfigAs("/home/work/.loophole/config.json")
101+
return true, nil
102+
}
103+
104+
return false, nil
105+
}

ui/ui.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/loophole/cli/internal/pkg/cache"
2222
"github.com/loophole/cli/internal/pkg/communication"
2323
"github.com/loophole/cli/internal/pkg/token"
24+
"github.com/loophole/cli/internal/pkg/updatecheck"
2425
)
2526

2627
var upgrader = websocket.Upgrader{} // use default options
@@ -259,7 +260,7 @@ func Display() {
259260
}
260261
defer ui.Close()
261262

262-
loophole.CheckVersion()
263+
updatecheck.CheckVersion()
263264
<-ui.Done()
264265
}
265266

0 commit comments

Comments
 (0)