/
update.go
167 lines (139 loc) · 3.9 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package update
import (
"fmt"
"io"
"net/http"
"os"
"os/user"
"runtime"
"strings"
"github.com/emmahsax/go-git-helper/internal/executor"
"github.com/emmahsax/go-git-helper/internal/utils"
"github.com/spf13/cobra"
"github.com/tidwall/gjson"
)
type Update struct {
Debug bool
Executor executor.ExecutorInterface
Owner string
Repository string
}
var (
asset = "git-helper_" + runtime.GOOS + "_" + runtime.GOARCH
newPath = "/usr/local/bin/git-helper" // This is for linux and mac based systems only
)
func NewCommand(packageOwner, packageRepository string) *cobra.Command {
var (
debug bool
)
cmd := &cobra.Command{
Use: "update",
Short: "Updates Git Helper with the newest version on GitHub",
Args: cobra.ExactArgs(0),
DisableFlagsInUseLine: true,
RunE: func(cmd *cobra.Command, args []string) error {
newUpdate(packageOwner, packageRepository, debug, executor.NewExecutor(debug)).execute()
return nil
},
}
cmd.Flags().BoolVar(&debug, "debug", false, "enables debug mode")
return cmd
}
func newUpdate(owner, repository string, debug bool, executor executor.ExecutorInterface) *Update {
return &Update{
Debug: debug,
Executor: executor,
Owner: owner,
Repository: repository,
}
}
func (u *Update) execute() {
u.downloadGitHelper()
u.moveGitHelper()
u.setPermissions()
u.outputNewVersion()
}
func (u *Update) downloadGitHelper() {
fmt.Println("Installing latest git-helper version")
releaseURL := fmt.Sprintf("https://api.github.com/repos/%s/%s/releases/latest", u.Owner, u.Repository)
downloadURL := u.getDownloadURL(u.fetchReleaseBody(releaseURL))
binaryName := strings.Split(downloadURL, "/")[len(strings.Split(downloadURL, "/"))-1]
u.downloadAndSaveBinary(downloadURL, binaryName)
}
func (u *Update) fetchReleaseBody(releaseURL string) []byte {
resp, err := http.Get(releaseURL)
if err != nil {
utils.HandleError(err, u.Debug, nil)
return []byte{}
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
utils.HandleError(err, u.Debug, nil)
return []byte{}
}
return body
}
func (u *Update) getDownloadURL(body []byte) string {
var downloadURL string
switch runtime.GOOS {
case "darwin":
downloadURL = gjson.Get(string(body), "assets.#(name==\""+asset+"\").browser_download_url").String()
case "linux":
downloadURL = gjson.Get(string(body), "assets.#(name==\""+asset+"\").browser_download_url").String()
default:
fmt.Println("Unsupported operating system:", runtime.GOOS)
}
return downloadURL
}
func (u *Update) downloadAndSaveBinary(downloadURL, binaryName string) {
resp, err := http.Get(downloadURL)
if err != nil {
utils.HandleError(err, u.Debug, nil)
}
defer resp.Body.Close()
out, err := os.Create(binaryName)
if err != nil {
utils.HandleError(err, u.Debug, nil)
}
defer out.Close()
_, err = io.Copy(out, resp.Body)
if err != nil {
utils.HandleError(err, u.Debug, nil)
}
}
func (u *Update) moveGitHelper() {
output, err := u.Executor.Exec("actionAndOutput", "sudo", "mv", "./"+asset, newPath)
if err != nil {
utils.HandleError(err, u.Debug, nil)
return
}
fmt.Printf("%s", string(output))
}
func (u *Update) setPermissions() {
currentUser, err := user.Current()
if err != nil {
utils.HandleError(err, u.Debug, nil)
return
}
output, err := u.Executor.Exec("actionAndOutput", "sudo", "chown", currentUser.Username+":staff", newPath)
if err != nil {
utils.HandleError(err, u.Debug, nil)
return
}
fmt.Printf("%s", string(output))
output, err = u.Executor.Exec("actionAndOutput", "sudo", "chmod", "+x", newPath)
if err != nil {
utils.HandleError(err, u.Debug, nil)
return
}
fmt.Printf("%s", string(output))
}
func (u *Update) outputNewVersion() {
output, err := u.Executor.Exec("actionAndOutput", "git-helper", "version")
if err != nil {
utils.HandleError(err, u.Debug, nil)
return
}
fmt.Printf("Installed %s", string(output))
}