Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize the information display of gf -v #3145

Merged
merged 7 commits into from Nov 16, 2023
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
96 changes: 76 additions & 20 deletions cmd/gf/internal/cmd/cmd_version.go
Expand Up @@ -9,12 +9,16 @@ package cmd
import (
"context"
"fmt"
"runtime"
"strings"
"time"

"github.com/gogf/gf/v2"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gbuild"
"github.com/gogf/gf/v2/os/gfile"
"github.com/gogf/gf/v2/os/gproc"
"github.com/gogf/gf/v2/text/gregex"
"github.com/gogf/gf/v2/text/gstr"

Expand All @@ -27,6 +31,7 @@ var (

type cVersion struct {
g.Meta `name:"version" brief:"show version information of current binary"`
detail string
hailaz marked this conversation as resolved.
Show resolved Hide resolved
}

type cVersionInput struct {
Expand All @@ -36,34 +41,85 @@ type cVersionInput struct {
type cVersionOutput struct{}

func (c cVersion) Index(ctx context.Context, in cVersionInput) (*cVersionOutput, error) {
info := gbuild.Info()
if info.Git == "" {
info.Git = "none"
}
mlog.Printf(`GoFrame CLI Tool %s, https://goframe.org`, gf.VERSION)
gfVersion, err := c.getGFVersionOfCurrentProject()
if err != nil {
gfVersion = err.Error()
c.detail = fmt.Sprintf("%s", gf.VERSION)

c.appendLine(0, "Welcome to GoFrame!")

c.appendLine(0, "Env Detail:")
goVersion, ok := getGoVersion()
if ok {
c.appendLine(1, fmt.Sprintf("Go Version: %s", goVersion))
c.appendLine(1, fmt.Sprintf("GF Version(go.mod): %s", getGoFrameVersion()))
} else {
gfVersion = gfVersion + " in current go.mod"
v, err := c.getGFVersionOfCurrentProject()
if err == nil {
c.appendLine(1, fmt.Sprintf("GF Version(go.mod): %s", v))
} else {
c.appendLine(1, fmt.Sprintf("GF Version(go.mod): %s", err.Error()))
}
}
mlog.Printf(`GoFrame Version: %s`, gfVersion)
mlog.Printf(`CLI Installed At: %s`, gfile.SelfPath())

c.appendLine(0, "CLI Detail:")
c.appendLine(1, fmt.Sprintf("Installed At: %s", gfile.SelfPath()))
info := gbuild.Info()
if info.GoFrame == "" {
mlog.Print(`Current is a custom installed version, no installation information.`)
return nil, nil
c.appendLine(1, fmt.Sprintf("Builded Go Version: %s", runtime.Version()))
c.appendLine(1, fmt.Sprintf("Builded GF Version: %s", gf.VERSION))
hailaz marked this conversation as resolved.
Show resolved Hide resolved
} else {
if info.Git == "" {
info.Git = "none"
}
c.appendLine(1, fmt.Sprintf("Builded Go Version: %s", info.Golang))
c.appendLine(1, fmt.Sprintf("Builded GF Version: %s", info.GoFrame))
c.appendLine(1, fmt.Sprintf("Git Commit: %s", info.Git))
c.appendLine(1, fmt.Sprintf("Builded Time: %s", info.Time))
}

mlog.Print(gstr.Trim(fmt.Sprintf(`
CLI Built Detail:
Go Version: %s
GF Version: %s
Git Commit: %s
Build Time: %s
`, info.Golang, info.GoFrame, info.Git, info.Time)))
c.appendLine(0, "Others Detail:")
c.appendLine(1, "Docs: https://goframe.org")
c.appendLine(1, fmt.Sprintf("Now Time: %s", time.Now().Format(time.RFC3339)))
hailaz marked this conversation as resolved.
Show resolved Hide resolved
c.print(" ")

return nil, nil
}

// appendLine description
func (c *cVersion) appendLine(level int, line string) {
c.detail += "\n" + strings.Repeat("\t", level) + line
}

// print description
func (c *cVersion) print(indent string) {
c.detail = strings.ReplaceAll(c.detail, "\t", indent)
mlog.Print(c.detail)
}

// getGoFrameVersion returns the goframe version of current project using.
func getGoFrameVersion() (gfVersion string) {
pkgInfo, err := gproc.ShellExec(context.Background(), `go list -f "{{if (not .Main)}}{{.Path}}@{{.Version}}{{end}}" -m all`)
if err != nil {
return ""
}
pkgList := gstr.Split(pkgInfo, "\n")
for _, v := range pkgList {
if strings.HasPrefix(v, "github.com/gogf/gf") {
gfVersion += fmt.Sprintf("\n\t\t%s", v)
}
}
return
}

// getGoVersion returns the go version
func getGoVersion() (goVersion string, ok bool) {
goVersion, err := gproc.ShellExec(context.Background(), "go version")
if err != nil {
return "", false
}
goVersion = gstr.TrimLeftStr(goVersion, "go version ")
goVersion = gstr.TrimRightStr(goVersion, "\n")
return goVersion, true
}

// getGFVersionOfCurrentProject checks and returns the GoFrame version current project using.
func (c cVersion) getGFVersionOfCurrentProject() (string, error) {
goModPath := gfile.Join(gfile.Pwd(), "go.mod")
Expand Down