Skip to content

Commit 5c99c27

Browse files
committed
refactor(update): Simplify checkUpdate function and improve version checking logic
1 parent 127f07a commit 5c99c27

File tree

3 files changed

+45
-28
lines changed

3 files changed

+45
-28
lines changed

cmd/sling/sling_run.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ func processRun(c *g.CliSC) (ok bool, err error) {
201201
os.Setenv("SLING_CLI_ARGS", g.Marshal(os.Args[1:]))
202202

203203
// check for update, and print note
204-
go checkUpdate(false)
204+
go checkUpdate()
205205
defer printUpdateAvailable()
206206

207207
runReplication:

cmd/sling/sling_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,15 +188,15 @@ func TestCfgPath(t *testing.T) {
188188
}
189189

190190
func TestExtract(t *testing.T) {
191-
core.Version = "v1.0.43"
191+
// core.Version = "v1.0.43"
192192

193-
checkUpdate(true)
193+
checkUpdate()
194194
assert.NotEmpty(t, updateVersion)
195195

196196
printUpdateAvailable()
197197

198-
err := g.ExtractTarGz(g.UserHomeDir()+"/Downloads/sling/sling_1.0.44_darwin_all.tar.gz", g.UserHomeDir()+"/Downloads/sling")
199-
g.AssertNoError(t, err)
198+
// err := g.ExtractTarGz(g.UserHomeDir()+"/Downloads/sling/sling_1.0.44_darwin_all.tar.gz", g.UserHomeDir()+"/Downloads/sling")
199+
// g.AssertNoError(t, err)
200200
}
201201

202202
var argsContext = g.NewContext(context.Background())

cmd/sling/sling_update.go

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,14 @@ import (
1111
"github.com/flarco/g/net"
1212
"github.com/flarco/g/process"
1313
"github.com/kardianos/osext"
14+
"github.com/samber/lo"
1415
"github.com/slingdata-io/sling-cli/core"
1516
"github.com/slingdata-io/sling-cli/core/env"
16-
"github.com/spf13/cast"
17+
)
18+
19+
var (
20+
// detect if running dev or stable channel
21+
isDevChannel = strings.Contains(core.Version, "dev")
1722
)
1823

1924
// getDownloadURL returns the appropriate download URL based on the channel (dev or stable)
@@ -47,12 +52,9 @@ func updateCLI(c *g.CliSC) (ok bool, err error) {
4752
ok = true
4853
env.TelMap["downloaded"] = false
4954

50-
// detect if running dev or stable channel
51-
isDevChannel := strings.Contains(core.Version, "dev")
52-
5355
// get latest version number (skipped for dev channel)
5456
if !isDevChannel {
55-
checkUpdate(true)
57+
checkUpdate()
5658
if updateVersion == core.Version {
5759
g.Info("Already up-to-date!")
5860
return
@@ -192,13 +194,7 @@ func upgradeScoop() (err error) {
192194
return nil
193195
}
194196

195-
func checkUpdate(force bool) {
196-
if strings.Contains(core.Version, "dev") {
197-
return
198-
} else if time.Now().Second()%4 != 0 && !force {
199-
// a way to check/notify about a new version less frequently
200-
return
201-
}
197+
func checkUpdate() {
202198

203199
instruction := "Please run `sling update`"
204200
switch getSlingPackage() {
@@ -212,17 +208,38 @@ func checkUpdate(force bool) {
212208
instruction = "Please run `docker pull slingdata/sling` and recreate your container."
213209
}
214210

215-
const url = "https://api.github.com/repos/slingdata-io/sling-cli/releases"
216-
_, respB, _ := net.ClientDo("GET", url, nil, nil)
217-
arr := []map[string]any{}
218-
g.JSONUnmarshal(respB, &arr)
219-
if len(arr) > 0 && arr[0] != nil {
220-
updateVersion = strings.TrimPrefix(cast.ToString(arr[0]["tag_name"]), "v")
221-
isNew, err := g.CompareVersions(core.Version, updateVersion)
222-
if err != nil {
223-
g.DebugLow("Error comparing versions: %s", err.Error())
224-
} else if isNew {
225-
updateMessage = g.F("FYI there is a new sling version released (%s). %s", updateVersion, instruction)
211+
_, respB, _ := net.ClientDo(
212+
"POST", "https://version.slingdata.io",
213+
strings.NewReader(g.Marshal(g.M(
214+
"exec_id", os.Getenv("SLING_EXEC_ID"),
215+
"channel", lo.Ternary(isDevChannel, "dev", "stable"),
216+
"version_cli", core.Version,
217+
))),
218+
nil,
219+
5,
220+
)
221+
respMap := map[string]string{}
222+
g.JSONUnmarshal(respB, &respMap)
223+
if time.Now().Second()%4 != 0 && len(respMap) > 0 {
224+
updateVersion = respMap["version_latest"]
225+
if isDevChannel {
226+
if core.Version != updateVersion && updateVersion != "" {
227+
updateMessage = env.GreenString(g.F("FYI there is a new sling dev build released => %s. You can run `sling update` to download it.", updateVersion))
228+
}
229+
} else {
230+
isNew, err := g.CompareVersions(core.Version, updateVersion)
231+
if err != nil {
232+
g.Debug("Error comparing versions: %s", err.Error())
233+
} else if isNew {
234+
updateMessage = env.GreenString(g.F("FYI there is a new sling version released (%s). %s", updateVersion, instruction))
235+
}
236+
}
237+
if message := respMap["media"]; message != "" {
238+
if updateMessage != "" {
239+
updateMessage = updateMessage + "\n" + env.BlueString(message)
240+
} else {
241+
updateMessage = env.BlueString(message)
242+
}
226243
}
227244
}
228245
}

0 commit comments

Comments
 (0)