Skip to content

Commit 3dda1c9

Browse files
committed
feat: add list command
1 parent 3207aa1 commit 3dda1c9

File tree

7 files changed

+108
-14
lines changed

7 files changed

+108
-14
lines changed

go.mod

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ require (
66
github.com/arduino/go-paths-helper v1.14.0
77
github.com/arduino/go-windows-runas v1.0.1
88
github.com/codeclysm/extract/v4 v4.0.0
9+
github.com/jedib0t/go-pretty/v6 v6.6.8
910
github.com/leonelquinteros/gotext v1.7.2
1011
github.com/schollz/progressbar/v3 v3.18.0
1112
github.com/sirupsen/logrus v1.9.3
1213
github.com/spf13/cobra v1.9.1
13-
github.com/stretchr/testify v1.9.0
14+
github.com/stretchr/testify v1.10.0
1415
go.bug.st/cleanup v1.0.0
16+
go.bug.st/f v0.4.0
1517
)
1618

1719
require (
@@ -20,12 +22,14 @@ require (
2022
github.com/inconshreveable/mousetrap v1.1.0 // indirect
2123
github.com/juju/errors v1.0.0 // indirect
2224
github.com/klauspost/compress v1.18.0 // indirect
25+
github.com/mattn/go-runewidth v0.0.16 // indirect
2326
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect
2427
github.com/pmezard/go-difflib v1.0.0 // indirect
2528
github.com/rivo/uniseg v0.4.7 // indirect
2629
github.com/spf13/pflag v1.0.7 // indirect
2730
github.com/ulikunitz/xz v0.5.12 // indirect
2831
golang.org/x/sys v0.36.0 // indirect
2932
golang.org/x/term v0.35.0 // indirect
33+
golang.org/x/text v0.22.0 // indirect
3034
gopkg.in/yaml.v3 v3.0.1 // indirect
3135
)

go.sum

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ github.com/h2non/filetype v1.1.3 h1:FKkx9QbD7HR/zjK1Ia5XiBsq9zdLi5Kf3zGyFTAFkGg=
1414
github.com/h2non/filetype v1.1.3/go.mod h1:319b3zT68BvV+WRj7cwy856M2ehB3HqNOt6sy1HndBY=
1515
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
1616
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
17+
github.com/jedib0t/go-pretty/v6 v6.6.8 h1:JnnzQeRz2bACBobIaa/r+nqjvws4yEhcmaZ4n1QzsEc=
18+
github.com/jedib0t/go-pretty/v6 v6.6.8/go.mod h1:YwC5CE4fJ1HFUDeivSV1r//AmANFHyqczZk+U6BDALU=
1719
github.com/juju/errors v1.0.0 h1:yiq7kjCLll1BiaRuNY53MGI0+EQ3rF6GB+wvboZDefM=
1820
github.com/juju/errors v1.0.0/go.mod h1:B5x9thDqx0wIMH3+aLIMP9HjItInYWObRovoCFM5Qe8=
1921
github.com/klauspost/compress v1.18.0 h1:c/Cqfb0r+Yi+JtIEq73FWXVkRonBlf0CRNYc8Zttxdo=
@@ -30,6 +32,7 @@ github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db h1:62I3jR2Em
3032
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db/go.mod h1:l0dey0ia/Uv7NcFFVbCLtqEBQbrT4OCwCSKTEv6enCw=
3133
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
3234
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
35+
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
3336
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
3437
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
3538
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
@@ -45,17 +48,21 @@ github.com/spf13/pflag v1.0.7/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An
4548
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
4649
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
4750
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
48-
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
49-
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
51+
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
52+
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
5053
github.com/ulikunitz/xz v0.5.12 h1:37Nm15o69RwBkXM0J6A5OlE67RZTfzUxTj8fB3dfcsc=
5154
github.com/ulikunitz/xz v0.5.12/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
5255
go.bug.st/cleanup v1.0.0 h1:XVj1HZxkBXeq3gMT7ijWUpHyIC1j8XAoNSyQ06CskgA=
5356
go.bug.st/cleanup v1.0.0/go.mod h1:EqVmTg2IBk4znLbPD28xne3abjsJftMdqqJEjhn70bk=
57+
go.bug.st/f v0.4.0 h1:Vstqb950nMA+PhAlRxUw8QL1ntHy/gXHNyyzjkQLJ10=
58+
go.bug.st/f v0.4.0/go.mod h1:bMo23205ll7UW63KwO1ut5RdlJ9JK8RyEEr88CmOF5Y=
5459
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
5560
golang.org/x/sys v0.36.0 h1:KVRy2GtZBrk1cBYA7MKu5bEZFxQk4NIDV6RLVcC8o0k=
5661
golang.org/x/sys v0.36.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
5762
golang.org/x/term v0.35.0 h1:bZBVKBudEyhRcajGcNc3jIfWPqV4y/Kt2XcoigOWtDQ=
5863
golang.org/x/term v0.35.0/go.mod h1:TPGtkTLesOwf2DE8CgVYiZinHAOuy5AYUYT1lENIZnA=
64+
golang.org/x/text v0.22.0 h1:bofq7m3/HAFvbF51jz3Q9wLg3jkvSPuiZu/pD1XwgtM=
65+
golang.org/x/text v0.22.0/go.mod h1:YRoo4H8PVmsu+E3Ou7cqLVH8oXWIHVoX0jqUWALQhfY=
5966
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
6067
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
6168
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=

list.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package main
2+
3+
import (
4+
"github.com/jedib0t/go-pretty/v6/table"
5+
"github.com/spf13/cobra"
6+
7+
"github.com/arduino/arduino-flasher-cli/feedback"
8+
"github.com/arduino/arduino-flasher-cli/i18n"
9+
"github.com/arduino/arduino-flasher-cli/tablestyle"
10+
"github.com/arduino/arduino-flasher-cli/updater"
11+
)
12+
13+
func newListCmd() *cobra.Command {
14+
cmd := &cobra.Command{
15+
Use: "list",
16+
Short: "List the available Linux images",
17+
Args: cobra.NoArgs,
18+
Run: func(cmd *cobra.Command, args []string) {
19+
runListCommand()
20+
},
21+
}
22+
return cmd
23+
}
24+
25+
func runListCommand() {
26+
client := updater.NewClient("debian-im/Stable")
27+
28+
manifest, err := client.GetInfoManifest()
29+
if err != nil {
30+
feedback.Fatal(i18n.Tr("error retrieving the manifest: %v", err), feedback.ErrBadArgument)
31+
}
32+
33+
feedback.PrintResult(listResult{Latest: manifest.Latest, Releases: manifest.Releases})
34+
}
35+
36+
type listResult struct {
37+
Latest updater.Release `json:"latest"`
38+
Releases []updater.Release `json:"releases"`
39+
}
40+
41+
// Data implements Result interface
42+
func (lr listResult) Data() interface{} {
43+
return lr
44+
}
45+
46+
// String implements Result interface
47+
func (lr listResult) String() string {
48+
t := table.NewWriter()
49+
t.SetStyle(tablestyle.CustomCleanStyle)
50+
t.AppendHeader(table.Row{"VERSION", "LATEST"})
51+
52+
for i := len(lr.Releases) - 1; i >= 0; i-- {
53+
row := table.Row{lr.Releases[i].Version}
54+
if lr.Releases[i].Version == lr.Latest.Version {
55+
row = append(row, "✓")
56+
}
57+
t.AppendRow(row)
58+
}
59+
return t.Render()
60+
}

main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ func main() {
5151
rootCmd.AddCommand(
5252
newFlashCmd(),
5353
newInstallDriversCmd(),
54+
newListCmd(),
5455
&cobra.Command{
5556
Use: "version",
5657
Short: "Print the version number of Arduino Flasher CLI",

tablestyle/tablestyle.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package tablestyle
2+
3+
import (
4+
"github.com/jedib0t/go-pretty/v6/table"
5+
"github.com/jedib0t/go-pretty/v6/text"
6+
)
7+
8+
var (
9+
CustomCleanStyle = table.Style{
10+
Name: "CustomClean",
11+
Box: table.BoxStyle{PaddingRight: " "},
12+
Format: table.FormatOptions{
13+
Footer: text.FormatUpper,
14+
Header: text.FormatUpper,
15+
Row: text.FormatDefault,
16+
RowAlign: text.AlignCenter,
17+
},
18+
Options: table.Options{
19+
DrawBorder: false,
20+
SeparateColumns: true,
21+
SeparateFooter: false,
22+
SeparateHeader: false,
23+
SeparateRows: false,
24+
},
25+
}
26+
)

updater/flasher.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package updater
1818
import (
1919
"context"
2020
"fmt"
21-
"net/url"
2221
"runtime"
2322
"strings"
2423

@@ -31,14 +30,7 @@ import (
3130

3231
func Flash(ctx context.Context, imagePath *paths.Path, version string, forceYes bool) error {
3332
if !imagePath.Exist() {
34-
updateURL := "https://downloads.arduino.cc"
35-
36-
parsedURL, err := url.Parse(updateURL)
37-
if err != nil {
38-
return fmt.Errorf("invalid UPDATE_URL: %v", err)
39-
}
40-
41-
client := NewClient(parsedURL, "debian-im/Stable")
33+
client := NewClient("debian-im/Stable")
4234

4335
tempImagePath, err := DownloadAndExtract(client, version, func(target string) (bool, error) {
4436
feedback.Printf("Found Debian image version: %s", target)

updater/http_client.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,12 @@ import (
2323
"io"
2424
"net/http"
2525
"net/url"
26+
27+
"go.bug.st/f"
2628
)
2729

30+
var updateURL = f.Must(url.Parse("https://downloads.arduino.cc"))
31+
2832
// Client holds the base URL, command name, allows custom HTTP client, and optional headers.
2933
type Client struct {
3034
BaseURL *url.URL
@@ -56,9 +60,9 @@ func WithHTTPClient(client HTTPDoer) Option {
5660
}
5761

5862
// NewClient creates a new Client with optional configuration.
59-
func NewClient(baseURL *url.URL, cmdName string, opts ...Option) *Client {
63+
func NewClient(cmdName string, opts ...Option) *Client {
6064
c := &Client{
61-
BaseURL: baseURL,
65+
BaseURL: updateURL,
6266
CmdName: cmdName,
6367
HTTPClient: http.DefaultClient,
6468
Headers: nil,

0 commit comments

Comments
 (0)