-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
204 lines (174 loc) · 5.21 KB
/
main.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package main
import (
"flag"
"fmt"
"io"
"log"
"net/http"
"os"
"strings"
"time"
"github.com/charmbracelet/bubbles/table"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/cheggaaa/pb/v3"
"github.com/cli/go-gh/v2/pkg/api"
units "github.com/docker/go-units"
)
var sizes = []string{"B", "kB", "MB", "GB", "TB", "PB", "EB"}
var repoName, tagName string
type Release struct {
URL string `json:"url,omitempty"`
AssetsURL string `json:"assets_url,omitempty"`
UploadURL string `json:"upload_url,omitempty"`
HTMLURL string `json:"html_url,omitempty"`
ID int `json:"id,omitempty"`
NodeID string `json:"node_id,omitempty"`
TagName string `json:"tag_name,omitempty"`
TargetCommitish string `json:"target_commitish,omitempty"`
Name string `json:"name,omitempty"`
Draft bool `json:"draft,omitempty"`
Prerelease bool `json:"prerelease,omitempty"`
CreatedAt time.Time `json:"created_at,omitempty"`
PublishedAt time.Time `json:"published_at,omitempty"`
// Assets []Assets `json:"assets,omitempty"`
Assets []struct {
URL string `json:"url,omitempty"`
ID int `json:"id,omitempty"`
NodeID string `json:"node_id,omitempty"`
Name string `json:"name,omitempty"`
Label string `json:"label,omitempty"`
ContentType string `json:"content_type,omitempty"`
State string `json:"state,omitempty"`
Size int `json:"size,omitempty"`
DownloadCount int `json:"download_count,omitempty"`
CreatedAt time.Time `json:"created_at,omitempty"`
UpdatedAt time.Time `json:"updated_at,omitempty"`
BrowserDownloadURL string `json:"browser_download_url,omitempty"`
}
TarballURL string `json:"tarball_url,omitempty"`
ZipballURL string `json:"zipball_url,omitempty"`
}
func getReleaseInfo(repoName, tagName string) Release {
client, err := api.DefaultRESTClient()
if err != nil {
fmt.Println(err)
}
response := Release{}
var url string
if tagName == "latest" {
url = fmt.Sprintf("repos/%s/releases/latest", repoName)
} else {
url = fmt.Sprintf("repos/%s/releases/tags/%s", repoName, tagName)
}
err = client.Get(url, &response)
if err != nil {
fmt.Println(err)
}
return response
}
func renderTable() table.Model {
release := getReleaseInfo(repoName, tagName)
columns := []table.Column{
{Title: "Asset Name", Width: 50},
{Title: "Size", Width: 20},
}
var rows []table.Row
for _, assetname := range release.Assets {
rows = append(rows, table.Row{assetname.Name, units.HumanSize(float64(assetname.Size))})
}
t := table.New(
table.WithColumns(columns),
table.WithRows(rows),
table.WithFocused(true),
)
t.SetStyles(createTableStyles())
return t
}
var baseStyle = lipgloss.NewStyle().
BorderStyle(lipgloss.NormalBorder()).
BorderForeground(lipgloss.Color("240"))
type model struct {
table table.Model
rlz Release
}
func (m model) Init() tea.Cmd {
return nil
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "esc":
if m.table.Focused() {
m.table.Blur()
} else {
m.table.Focus()
}
case "q", "ctrl+c":
return m, tea.Quit
case "d", "enter":
selectedAsset := m.rlz.Assets[m.table.Cursor()]
go func() {
err := downloadFile(selectedAsset.BrowserDownloadURL)
if err != nil {
log.Fatalf("error downloading file: %v\n", err)
}
}()
}
}
m.table, cmd = m.table.Update(msg)
return m, cmd
}
func (m model) View() string {
return baseStyle.Render(m.table.View()) + "\n"
}
func createTableStyles() table.Styles {
s := table.DefaultStyles()
s.Header = s.Header.
BorderStyle(lipgloss.NormalBorder()).
BorderForeground(lipgloss.Color("240")).
BorderBottom(true).
Bold(false)
s.Selected = s.Selected.
Foreground(lipgloss.Color("229")).
Background(lipgloss.Color("57")).
Bold(false)
return s
}
func main() {
flag.StringVar(&repoName, "repo", "kubernetes-sigs/cluster-api", "GitHub repository name")
flag.StringVar(&repoName, "R", "kubernetes-sigs/cluster-api", "GitHub repository name")
flag.StringVar(&tagName, "tag", "latest", "GitHub release tag")
flag.StringVar(&tagName, "t", "latest", "GitHub release tag")
flag.Parse()
m := model{renderTable(), getReleaseInfo(repoName, tagName)}
if _, err := tea.NewProgram(m, tea.WithAltScreen()).Run(); err != nil {
fmt.Println("Error running program:", err)
}
}
func downloadFile(url string) error {
out, err := os.Create(url[strings.LastIndex(url, "/")+1:])
if err != nil {
return err
}
defer out.Close()
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
// Create progress bar
tmp := `{{string . "prefix"}}{{counters . }} {{bar . "[" "=" ">" "-" "]"}} {{percent . }} {{etime . }}{{string . "suffix"}}`
bar := pb.Full.Start64(resp.ContentLength)
bar.Set(pb.Bytes, true)
bar.SetTemplate(pb.ProgressBarTemplate(tmp))
reader := bar.NewProxyReader(resp.Body)
_, err = io.Copy(out, reader)
if err != nil {
return err
}
bar.Finish()
return nil
}