cmd_version.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved.
  2. //
  3. // This Source Code Form is subject to the terms of the MIT License.
  4. // If a copy of the MIT was not distributed with this file,
  5. // You can obtain one at https://github.com/gogf/gf.
  6. package cmd
  7. import (
  8. "context"
  9. "fmt"
  10. "github.com/gogf/gf/v2"
  11. "github.com/gogf/gf/v2/errors/gerror"
  12. "github.com/gogf/gf/v2/frame/g"
  13. "github.com/gogf/gf/v2/os/gbuild"
  14. "github.com/gogf/gf/v2/os/gfile"
  15. "github.com/gogf/gf/v2/text/gregex"
  16. "github.com/gogf/gf/v2/text/gstr"
  17. "github.com/gogf/gf/cmd/gf/v2/internal/utility/mlog"
  18. )
  19. var (
  20. Version = cVersion{}
  21. )
  22. type cVersion struct {
  23. g.Meta `name:"version" brief:"show version information of current binary"`
  24. }
  25. type cVersionInput struct {
  26. g.Meta `name:"version"`
  27. }
  28. type cVersionOutput struct{}
  29. func (c cVersion) Index(ctx context.Context, in cVersionInput) (*cVersionOutput, error) {
  30. info := gbuild.Info()
  31. if info.Git == "" {
  32. info.Git = "none"
  33. }
  34. mlog.Printf(`GoFrame CLI Tool %s, https://goframe.org`, gf.VERSION)
  35. gfVersion, err := c.getGFVersionOfCurrentProject()
  36. if err != nil {
  37. gfVersion = err.Error()
  38. } else {
  39. gfVersion = gfVersion + " in current go.mod"
  40. }
  41. mlog.Printf(`GoFrame Version: %s`, gfVersion)
  42. mlog.Printf(`CLI Installed At: %s`, gfile.SelfPath())
  43. if info.GoFrame == "" {
  44. mlog.Print(`Current is a custom installed version, no installation information.`)
  45. return nil, nil
  46. }
  47. mlog.Print(gstr.Trim(fmt.Sprintf(`
  48. CLI Built Detail:
  49. Go Version: %s
  50. GF Version: %s
  51. Git Commit: %s
  52. Build Time: %s
  53. `, info.Golang, info.GoFrame, info.Git, info.Time)))
  54. return nil, nil
  55. }
  56. // getGFVersionOfCurrentProject checks and returns the GoFrame version current project using.
  57. func (c cVersion) getGFVersionOfCurrentProject() (string, error) {
  58. goModPath := gfile.Join(gfile.Pwd(), "go.mod")
  59. if gfile.Exists(goModPath) {
  60. lines := gstr.SplitAndTrim(gfile.GetContents(goModPath), "\n")
  61. for _, line := range lines {
  62. line = gstr.Trim(line)
  63. line = gstr.TrimLeftStr(line, "require ")
  64. line = gstr.Trim(line)
  65. // Version 1.
  66. match, err := gregex.MatchString(`^github\.com/gogf/gf\s+(.+)$`, line)
  67. if err != nil {
  68. return "", err
  69. }
  70. if len(match) <= 1 {
  71. // Version > 1.
  72. match, err = gregex.MatchString(`^github\.com/gogf/gf/v\d\s+(.+)$`, line)
  73. if err != nil {
  74. return "", err
  75. }
  76. }
  77. if len(match) > 1 {
  78. return gstr.Trim(match[1]), nil
  79. }
  80. }
  81. return "", gerror.New("cannot find goframe requirement in go.mod")
  82. } else {
  83. return "", gerror.New("cannot find go.mod")
  84. }
  85. }