-
-
Notifications
You must be signed in to change notification settings - Fork 107
/
update.go
132 lines (112 loc) · 3.17 KB
/
update.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package cmd
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/tj/go-update"
"github.com/tj/go-update/stores/github"
)
// updateCmd represents the update command
var updateCmd = &cobra.Command{
Use: "update",
Short: "Updates bit to the latest or specified version",
Long: `bit update
bit update v0.7.4 (note: v is required)`,
Run: func(cmd *cobra.Command, args []string) {
targetVersion := ""
if len(args) == 1 {
targetVersion = args[0]
}
if !strings.HasPrefix(targetVersion, "v") {
targetVersion = "v" + targetVersion
}
currentVersion := GetVersion()
if !strings.HasPrefix(currentVersion, "v") {
currentVersion = "v" + currentVersion
}
log.Debug().Msg(currentVersion + " -> " + targetVersion)
// open-source edition
p := &update.Manager{
Command: "bit",
Store: &github.Store{
Owner: "chriswalz",
Repo: "bit",
Version: currentVersion[1:],
},
}
// fetch latest or specified release
release, err := getLatestOrSpecified(p, targetVersion[1:])
if err != nil {
fmt.Println(errors.Wrap(err, "fetching latest or specified release").Error())
return
}
// no updates
if release == nil || currentVersion == release.Version {
fmt.Println("No updates available, you're up to date!")
return
}
// find the tarball for this system
a := release.FindTarball(runtime.GOOS, runtime.GOARCH)
if a == nil {
fmt.Println(fmt.Errorf("failed to find a binary for %s %s", runtime.GOOS, runtime.GOARCH))
return
}
// download tarball to a tmp dir
tarball, err := a.Download()
if err != nil {
fmt.Println(errors.Wrap(err, "downloading tarball"))
return
}
// determine path
path, err := exec.LookPath("bit")
if err != nil {
fmt.Println(errors.Wrap(err, "looking up executable path"))
return
}
// if path is a symlink - get resolved path
fi, err := os.Lstat(path)
if err == nil && fi.Mode()&os.ModeSymlink == os.ModeSymlink {
// Bit path is a symlink
fmt.Println("bit is symlinked. If you used homebrew try:\nbrew upgrade bit-git")
// path = resolvedSymlink
return
}
log.Debug().Msg("bit is not symlinked")
dst := filepath.Dir(path)
// install it
if err := p.InstallTo(tarball, dst); err != nil {
fmt.Println(errors.Wrap(err, "installing"))
return
}
fmt.Println("Bit is supported through donations. Consider donating here: ❤ https://github.com/sponsors/chriswalz ")
fmt.Printf("Updated bit %s to %s in %s\n", currentVersion, release.Version, dst)
},
Args: cobra.MaximumNArgs(1),
}
func init() {
BitCmd.AddCommand(updateCmd)
}
// getLatestOrSpecified returns the latest or specified release.
func getLatestOrSpecified(s update.Store, version string) (*update.Release, error) {
if version == "" {
return getLatest(s)
}
return s.GetRelease(version)
}
// getLatest returns the latest release, error, or nil when there is none.
func getLatest(s update.Store) (*update.Release, error) {
releases, err := s.LatestReleases()
if err != nil {
return nil, errors.Wrap(err, "fetching releases")
}
if len(releases) == 0 {
return nil, nil
}
return releases[0], nil
}