init
parent
d1fca0aa05
commit
f17742409a
@ -0,0 +1,20 @@
|
||||
package cli
|
||||
|
||||
import "github.com/urfave/cli/v2"
|
||||
|
||||
var (
|
||||
commands = []*cli.Command{
|
||||
{
|
||||
Name: "ls",
|
||||
Usage: "List installed versions",
|
||||
UsageText: "g ls",
|
||||
Action: list,
|
||||
},
|
||||
{
|
||||
Name: "use",
|
||||
Usage: "Switch to specified version",
|
||||
UsageText: "g use <version>",
|
||||
Action: use,
|
||||
},
|
||||
}
|
||||
)
|
@ -0,0 +1,47 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"strings"
|
||||
|
||||
"github.com/Masterminds/semver"
|
||||
"github.com/k0kubun/go-ansi"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
func list(*cli.Context) (err error) {
|
||||
infos, err := ioutil.ReadDir(versionsDir)
|
||||
if err != nil || len(infos) <= 0 {
|
||||
fmt.Printf("No version installed yet\n\n")
|
||||
return nil
|
||||
}
|
||||
items := make([]*semver.Version, 0, len(infos))
|
||||
for i := range infos {
|
||||
if !infos[i].IsDir() {
|
||||
continue
|
||||
}
|
||||
vname := infos[i].Name()
|
||||
var idx int
|
||||
if strings.Contains(vname, "alpha") {
|
||||
idx = strings.Index(vname, "alpha")
|
||||
|
||||
} else if strings.Contains(vname, "beta") {
|
||||
idx = strings.Index(vname, "beta")
|
||||
|
||||
} else if strings.Contains(vname, "rc") {
|
||||
idx = strings.Index(vname, "rc")
|
||||
}
|
||||
if idx > 0 {
|
||||
vname = vname[:idx] + "-" + vname[idx:]
|
||||
}
|
||||
v, err := semver.NewVersion(vname)
|
||||
if err != nil || v == nil {
|
||||
continue
|
||||
}
|
||||
items = append(items, v)
|
||||
}
|
||||
|
||||
render(inuse(goroot), items, ansi.NewAnsiStdout())
|
||||
return nil
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
func use(ctx *cli.Context) (err error) {
|
||||
vname := ctx.Args().First()
|
||||
if vname == "" {
|
||||
return nil
|
||||
//return cli.ShowSubcommandHelp(ctx)
|
||||
}
|
||||
targetV := filepath.Join(versionsDir, vname)
|
||||
|
||||
if finfo, err := os.Stat(targetV); err != nil || !finfo.IsDir() {
|
||||
return cli.Exit(fmt.Sprintf("[g] The %q version does not exist, please install it first.", vname), 1)
|
||||
}
|
||||
|
||||
_ = os.Remove(goroot)
|
||||
|
||||
if err = mkSymlink(targetV, goroot); err != nil {
|
||||
return cli.Exit(errstring(err), 1)
|
||||
}
|
||||
if output, err := exec.Command(filepath.Join(goroot, "bin", "go"), "version").Output(); err == nil {
|
||||
fmt.Print(string(output))
|
||||
}
|
||||
return nil
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
module github.com/forget-the-bright/j
|
||||
|
||||
go 1.20
|
||||
|
||||
require (
|
||||
github.com/Masterminds/semver v1.5.0
|
||||
github.com/fatih/color v1.15.0
|
||||
github.com/k0kubun/go-ansi v0.0.0-20180517002512-3bf9e2903213
|
||||
github.com/urfave/cli/v2 v2.25.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
|
||||
github.com/mattn/go-colorable v0.1.13 // indirect
|
||||
github.com/mattn/go-isatty v0.0.17 // indirect
|
||||
github.com/russross/blackfriday/v2 v2.1.0 // indirect
|
||||
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
|
||||
golang.org/x/sys v0.6.0 // indirect
|
||||
)
|
@ -0,0 +1,22 @@
|
||||
github.com/Masterminds/semver v1.5.0 h1:H65muMkzWKEuNDnfl9d70GUjFniHKHRbFPGBuZ3QEww=
|
||||
github.com/Masterminds/semver v1.5.0/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y=
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
|
||||
github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs=
|
||||
github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw=
|
||||
github.com/k0kubun/go-ansi v0.0.0-20180517002512-3bf9e2903213 h1:qGQQKEcAR99REcMpsXCp3lJ03zYT1PkRd3kQGPn9GVg=
|
||||
github.com/k0kubun/go-ansi v0.0.0-20180517002512-3bf9e2903213/go.mod h1:vNUNkEQ1e29fT/6vq2aBdFsgNPmy8qMdSay1npru+Sw=
|
||||
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
|
||||
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
|
||||
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
|
||||
github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=
|
||||
github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
|
||||
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
|
||||
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
||||
github.com/urfave/cli/v2 v2.25.0 h1:ykdZKuQey2zq0yin/l7JOm9Mh+pg72ngYMeB0ABn6q8=
|
||||
github.com/urfave/cli/v2 v2.25.0/go.mod h1:GHupkWPMM0M/sj1a2b4wUrWBPzazNrIjouW6fmdJLxc=
|
||||
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
|
||||
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
|
||||
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
|
||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
@ -0,0 +1,41 @@
|
||||
package build
|
||||
|
||||
import "strings"
|
||||
|
||||
const (
|
||||
// ShortVersion 短版本号
|
||||
ShortVersion = "1.0.0"
|
||||
)
|
||||
|
||||
// The value of variables come form `gb build -ldflags '-X "build.Build=xxxxx" -X "build.CommitID=xxxx"' `
|
||||
var (
|
||||
// Build build time
|
||||
Build string
|
||||
// Branch current git branch
|
||||
Branch string
|
||||
// Commit git commit id
|
||||
Commit string
|
||||
)
|
||||
|
||||
// Version 生成版本信息
|
||||
func Version() string {
|
||||
var buf strings.Builder
|
||||
buf.WriteString(ShortVersion)
|
||||
|
||||
if Build != "" {
|
||||
buf.WriteByte('\n')
|
||||
buf.WriteString("build: ")
|
||||
buf.WriteString(Build)
|
||||
}
|
||||
if Branch != "" {
|
||||
buf.WriteByte('\n')
|
||||
buf.WriteString("branch: ")
|
||||
buf.WriteString(Branch)
|
||||
}
|
||||
if Commit != "" {
|
||||
buf.WriteByte('\n')
|
||||
buf.WriteString("commit: ")
|
||||
buf.WriteString(Commit)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
Loading…
Reference in New Issue