Skip to content

Commit

Permalink
Add feature: 'g ls/ls-remote'输出的go版本列表中突出显示当前版本。voidint#17
Browse files Browse the repository at this point in the history
  • Loading branch information
voidint committed Jun 15, 2019
1 parent d81dc38 commit 2d1d738
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 12 deletions.
25 changes: 25 additions & 0 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@ package cli

import (
"fmt"
"io"
"os"
"path/filepath"
"sort"
"strings"

"github.com/Masterminds/semver"
"github.com/fatih/color"
"github.com/urfave/cli"
"github.com/voidint/g/build"
)
Expand Down Expand Up @@ -81,3 +86,23 @@ func init() {
{{.Copyright}}{{end}}
`, build.ShortVersion)
}

// inuse 返回当前的go版本号
func inuse(goroot string) (version string) {
p, _ := os.Readlink(goroot)
return filepath.Base(p)
}

// render 渲染go版本列表
func render(curV string, items []*semver.Version, out io.Writer) {
sort.Sort(semver.Collection(items))

for i := range items {
v := strings.TrimSuffix(strings.TrimSuffix(items[i].String(), ".0"), ".0")
if v == curV {
color.New(color.FgGreen).Fprintf(out, "* %s\n", v)
} else {
fmt.Fprintf(out, " %s\n", v)
}
}
}
8 changes: 2 additions & 6 deletions cli/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ package cli
import (
"fmt"
"io/ioutil"
"sort"
"strings"
"os"

"github.com/Masterminds/semver"
"github.com/urfave/cli"
Expand All @@ -27,10 +26,7 @@ func list(ctx *cli.Context) (err error) {
}
items = append(items, v)
}
sort.Sort(semver.Collection(items))

for i := range items {
fmt.Println(strings.TrimSuffix(strings.TrimSuffix(items[i].String(), ".0"), ".0"))
}
render(inuse(goroot), items, os.Stdout)
return nil
}
7 changes: 1 addition & 6 deletions cli/ls_remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package cli
import (
"fmt"
"os"
"sort"
"strings"

"github.com/Masterminds/semver"
"github.com/urfave/cli"
Expand Down Expand Up @@ -53,10 +51,7 @@ func listRemote(ctx *cli.Context) (err error) {
}
items = append(items, v)
}
sort.Sort(semver.Collection(items))

for i := range items {
fmt.Println(strings.TrimSuffix(strings.TrimSuffix(items[i].String(), ".0"), ".0"))
}
render(inuse(goroot), items, os.Stdout)
return nil
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ require (
github.com/Masterminds/semver v1.4.2
github.com/PuerkitoBio/goquery v1.5.0
github.com/dsnet/compress v0.0.1 // indirect
github.com/fatih/color v1.7.0
github.com/golang/snappy v0.0.1 // indirect
github.com/gopherjs/gopherjs v0.0.0-20190430165422-3e4dfb77656c // indirect
github.com/mattn/go-colorable v0.1.2 // indirect
github.com/mholt/archiver v3.1.1+incompatible
github.com/nwaples/rardecode v1.0.0 // indirect
github.com/pierrec/lz4 v2.0.5+incompatible // indirect
Expand Down
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ github.com/andybalholm/cascadia v1.0.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9Pq
github.com/dsnet/compress v0.0.1 h1:PlZu0n3Tuv04TzpfPbrnI0HW/YwodEXDS+oPKahKF0Q=
github.com/dsnet/compress v0.0.1/go.mod h1:Aw8dCMJ7RioblQeTqt88akK31OvO8Dhf5JflhBbQEHo=
github.com/dsnet/golib v0.0.0-20171103203638-1ea166775780/go.mod h1:Lj+Z9rebOhdfkVLjJ8T6VcRQv3SXugXy999NBtR9aFY=
github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4=
github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
Expand All @@ -15,6 +17,10 @@ github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/klauspost/compress v1.4.1/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A=
github.com/klauspost/cpuid v1.2.0/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek=
github.com/mattn/go-colorable v0.1.2 h1:/bC9yWikZXAL9uJdulbSfyVNIR3n3trXl+v8+1sx8mU=
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
github.com/mattn/go-isatty v0.0.8 h1:HLtExJ+uU2HOZ+wI0Tt5DtUDrx8yhUqDcp7fYERX4CE=
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mholt/archiver v3.1.1+incompatible h1:1dCVxuqs0dJseYEhi5pl7MYPH9zDa1wBi7mF09cbNkU=
github.com/mholt/archiver v3.1.1+incompatible/go.mod h1:Dh2dOXnSdiLxRiPoVfIr/fI1TwETms9B8CTWfeh7ROU=
github.com/nwaples/rardecode v1.0.0 h1:r7vGuS5akxOnR4JQSkko62RJ1ReCMXxQRPtxsiFMBOs=
Expand Down Expand Up @@ -43,6 +49,7 @@ golang.org/x/net v0.0.0-20190501004415-9ce7a6920f09 h1:KaQtG+aDELoNmXYas3TVkGNYR
golang.org/x/net v0.0.0-20190501004415-9ce7a6920f09/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190429190828-d89cdac9e872/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand Down

0 comments on commit 2d1d738

Please sign in to comment.