Skip to content

Commit

Permalink
feat: added --output option for ls and ls-remote subcommands. voidint…
Browse files Browse the repository at this point in the history
  • Loading branch information
voidint committed Nov 20, 2023
1 parent 553df0e commit ca101da
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 23 deletions.
49 changes: 41 additions & 8 deletions cli/cli.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cli

import (
"encoding/json"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -123,17 +124,49 @@ func installed() (versions map[string]bool) {
return
}

type versionResp struct {
Version string `json:"version"`
Default bool `json:"default"`
Installed bool `json:"installed"`
Packages []version.Package `json:"packages"`
}

const (
rawMode = 0
jsonMode = 1
)

// render 渲染go版本列表
func render(installed map[string]bool, items []*version.Version, out io.Writer) {
for _, v := range items {
if inused, found := installed[v.Name()]; found {
if inused {
color.New(color.FgGreen).Fprintf(out, "* %s\n", v.Name())
func render(mode uint8, installed map[string]bool, items []*version.Version, out io.Writer) {
switch mode {
case jsonMode:
vs := make([]versionResp, 0, len(items))

for _, item := range items {
v := versionResp{
Version: item.Name(),
Packages: item.Packages(),
}
if inused, found := installed[item.Name()]; found {
v.Default = inused
v.Installed = found
}
vs = append(vs, v)
}

_ = json.NewEncoder(out).Encode(&vs)

default:
for _, item := range items {
if inused, found := installed[item.Name()]; found {
if inused {
color.New(color.FgGreen).Fprintf(out, "* %s\n", item.Name())
} else {
color.New(color.FgGreen).Fprintf(out, " %s\n", item.Name())
}
} else {
color.New(color.FgGreen).Fprintf(out, " %s\n", v.Name())
fmt.Fprintf(out, " %s\n", item.Name())
}
} else {
fmt.Fprintf(out, " %s\n", v.Name())
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func Test_render(t *testing.T) {
}
sort.Sort(version.Collection(items))

render(map[string]bool{"1.8.1": true}, items, &buf)
render(rawMode, map[string]bool{"1.8.1": true}, items, &buf)
assert.Equal(t, " 1.7\n* 1.8.1\n 1.10beta2\n 1.19beta1\n 1.21rc4\n 1.21.0\n", buf.String())
})
}
Expand Down
18 changes: 16 additions & 2 deletions cli/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,28 @@ var (
Aliases: []string{"l"},
Usage: "List installed versions",
UsageText: "g ls",
Action: list,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "output",
Aliases: []string{"o"},
Usage: "Output format. One of: (raw, json).",
},
},
Action: list,
},
{
Name: "ls-remote",
Aliases: []string{"lr", "lsr"},
Usage: "List remote versions available for install",
UsageText: "g ls-remote [stable|archived|unstable]",
Action: listRemote,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "output",
Aliases: []string{"o"},
Usage: "Output format. One of: (raw, json).",
},
},
Action: listRemote,
},
{
Name: "use",
Expand Down
12 changes: 10 additions & 2 deletions cli/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/voidint/g/version"
)

func list(*cli.Context) (err error) {
func list(ctx *cli.Context) (err error) {
dirs, err := os.ReadDir(versionsDir)
if err != nil || len(dirs) <= 0 {
fmt.Printf("No version installed yet\n\n")
Expand All @@ -30,6 +30,14 @@ func list(*cli.Context) (err error) {
sort.Sort(version.Collection(items))
}

render(installed(), items, ansi.NewAnsiStdout())
var renderMode uint8
switch ctx.String("output") {
case "json":
renderMode = jsonMode
default:
renderMode = rawMode
}

render(renderMode, installed(), items, ansi.NewAnsiStdout())
return nil
}
10 changes: 9 additions & 1 deletion cli/ls_remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ func listRemote(ctx *cli.Context) (err error) {
return cli.Exit(errstring(err), 1)
}

render(installed(), vs, ansi.NewAnsiStdout())
var renderMode uint8
switch ctx.String("output") {
case "json":
renderMode = jsonMode
default:
renderMode = rawMode
}

render(renderMode, installed(), vs, ansi.NewAnsiStdout())
return nil
}
18 changes: 9 additions & 9 deletions version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,15 @@ func (v *Version) FindPackages(kind, goos, goarch string) (pkgs []*Package, err

// Package go版本安装包
type Package struct {
FileName string
URL string
Kind string
OS string
Arch string
Size string
Checksum string
ChecksumURL string
Algorithm string // checksum algorithm
FileName string `json:"filename"`
URL string `json:"url"`
Kind string `json:"kind"`
OS string `json:"os"`
Arch string `json:"arch"`
Size string `json:"size"`
Checksum string `json:"checksum"`
ChecksumURL string `json:"-"`
Algorithm string `json:"algorithm"` // checksum algorithm
}

const (
Expand Down

0 comments on commit ca101da

Please sign in to comment.