Skip to content

Commit 32e43cd

Browse files
author
Brian Strauch
committed
automatic github updates
1 parent 11340cc commit 32e43cd

5 files changed

Lines changed: 97 additions & 0 deletions

File tree

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module spotify
33
go 1.16
44

55
require (
6+
github.com/inconshreveable/go-update v0.0.0-20160112193335-8152e7eb6ccf
67
github.com/pkg/browser v0.0.0-20210115035449-ce105d075bb4
78
github.com/spf13/cobra v1.1.3
89
github.com/spf13/viper v1.7.1

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO
9393
github.com/hashicorp/mdns v1.0.0/go.mod h1:tL+uN++7HEJ6SQLQ2/p+z2pH24WQKWjBPkE0mNTz8vQ=
9494
github.com/hashicorp/memberlist v0.1.3/go.mod h1:ajVTdAv/9Im8oMAAj5G31PhhMCZJV2pPBoIllUwCN7I=
9595
github.com/hashicorp/serf v0.8.2/go.mod h1:6hOLApaqBFA1NXqRQAsxw9QxuDEvNxSQRwA/JwenrHc=
96+
github.com/inconshreveable/go-update v0.0.0-20160112193335-8152e7eb6ccf h1:WfD7VjIE6z8dIvMsI4/s+1qr5EL+zoIGev1BQj1eoJ8=
97+
github.com/inconshreveable/go-update v0.0.0-20160112193335-8152e7eb6ccf/go.mod h1:hyb9oH7vZsitZCiBt0ZvifOrB+qc8PS5IiilCIb87rg=
9698
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
9799
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
98100
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=

internal/strings.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const (
88
NoActiveDeviceSpotifyErr = "Player command failed: No active device found"
99
NoNextErr = "No track after this one"
1010
NoPreviousErr = "No track before this one"
11+
NoReleaseAvailable = "No release available for the given OS and architecture"
1112
NotLoggedInErr = "You are not logged in. Run 'spotify login' before using this command"
1213
TokenExpiredErr = "API token is expired"
1314
)

internal/update/update.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package update
2+
3+
import (
4+
"encoding/json"
5+
"errors"
6+
"fmt"
7+
"io/ioutil"
8+
"net/http"
9+
"runtime"
10+
"spotify/internal"
11+
"strings"
12+
13+
"github.com/inconshreveable/go-update"
14+
"github.com/spf13/cobra"
15+
)
16+
17+
func NewCommand() *cobra.Command {
18+
return &cobra.Command{
19+
Use: "update",
20+
Short: "Update to the latest version.",
21+
RunE: func(cmd *cobra.Command, _ []string) error {
22+
version, err := updateFromGitHub("brianstrauch/spotify-cli")
23+
if err != nil {
24+
return err
25+
}
26+
27+
cmd.Printf("Updated CLI to %s!\n", version)
28+
return nil
29+
},
30+
}
31+
}
32+
33+
type Asset struct {
34+
Name string `json:"name"`
35+
BrowserDownloadURL string `json:"browser_download_url"`
36+
}
37+
38+
type Release struct {
39+
TagName string `json:"tag_name"`
40+
Assets []Asset `json:"assets"`
41+
}
42+
43+
func updateFromGitHub(repo string) (string, error) {
44+
release, err := getLatestReleaseInfo(repo)
45+
if err != nil {
46+
return "", err
47+
}
48+
49+
// TODO: Prevent installing same release
50+
51+
for _, asset := range release.Assets {
52+
if strings.Contains(asset.Name, runtime.GOOS) && strings.Contains(asset.Name, runtime.GOARCH) {
53+
res, err := http.Get(asset.BrowserDownloadURL)
54+
if err != nil {
55+
return "", err
56+
}
57+
defer res.Body.Close()
58+
59+
// TODO: Checksum
60+
if err := update.Apply(res.Body, update.Options{}); err != nil {
61+
return "", err
62+
}
63+
64+
return release.TagName, nil
65+
}
66+
}
67+
68+
return "", errors.New(internal.NoReleaseAvailable)
69+
}
70+
71+
func getLatestReleaseInfo(repo string) (*Release, error) {
72+
url := fmt.Sprintf("https://api.github.com/repos/%s/releases/latest", repo)
73+
74+
res, err := http.Get(url)
75+
if err != nil {
76+
return nil, err
77+
}
78+
defer res.Body.Close()
79+
80+
data, err := ioutil.ReadAll(res.Body)
81+
if err != err {
82+
return nil, err
83+
}
84+
85+
release := new(Release)
86+
if err := json.Unmarshal(data, release); err != nil {
87+
return nil, err
88+
}
89+
90+
return release, nil
91+
}

main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"spotify/internal/save"
1212
"spotify/internal/status"
1313
"spotify/internal/unsave"
14+
"spotify/internal/update"
1415

1516
"github.com/spf13/cobra"
1617
"github.com/spf13/viper"
@@ -46,6 +47,7 @@ func main() {
4647
root.AddCommand(save.NewCommand())
4748
root.AddCommand(status.NewCommand())
4849
root.AddCommand(unsave.NewCommand())
50+
root.AddCommand(update.NewCommand())
4951

5052
// Hide help command and rename help flag
5153
root.SetHelpCommand(&cobra.Command{Hidden: true})

0 commit comments

Comments
 (0)