gfcmd.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 gfcmd
  7. import (
  8. _ "github.com/gogf/gf/cmd/gf/v2/internal/packed"
  9. "context"
  10. "github.com/gogf/gf/cmd/gf/v2/internal/cmd"
  11. "github.com/gogf/gf/cmd/gf/v2/internal/utility/allyes"
  12. "github.com/gogf/gf/cmd/gf/v2/internal/utility/mlog"
  13. "github.com/gogf/gf/v2/frame/g"
  14. "github.com/gogf/gf/v2/os/gcfg"
  15. "github.com/gogf/gf/v2/os/gcmd"
  16. "github.com/gogf/gf/v2/os/gfile"
  17. "github.com/gogf/gf/v2/text/gstr"
  18. )
  19. const (
  20. cliFolderName = `hack`
  21. )
  22. // Command manages the CLI command of `gf`.
  23. // This struct can be globally accessible and extended with custom struct.
  24. type Command struct {
  25. *gcmd.Command
  26. }
  27. // Run starts running the command according the command line arguments and options.
  28. func (c *Command) Run(ctx context.Context) {
  29. defer func() {
  30. if exception := recover(); exception != nil {
  31. if err, ok := exception.(error); ok {
  32. mlog.Print(err.Error())
  33. } else {
  34. panic(exception)
  35. }
  36. }
  37. }()
  38. // CLI configuration, using the `hack/config.yaml` in priority.
  39. if path, _ := gfile.Search(cliFolderName); path != "" {
  40. if adapter, ok := g.Cfg().GetAdapter().(*gcfg.AdapterFile); ok {
  41. if err := adapter.SetPath(path); err != nil {
  42. mlog.Fatal(err)
  43. }
  44. }
  45. }
  46. // zsh alias "git fetch" conflicts checks.
  47. handleZshAlias()
  48. // -y option checks.
  49. allyes.Init()
  50. // just run.
  51. if err := c.RunWithError(ctx); err != nil {
  52. // Exit with error message and exit code 1.
  53. // It is very important to exit the command process with code 1.
  54. mlog.Fatalf(`%+v`, err)
  55. }
  56. }
  57. // GetCommand retrieves and returns the root command of CLI `gf`.
  58. func GetCommand(ctx context.Context) (*Command, error) {
  59. root, err := gcmd.NewFromObject(cmd.GF)
  60. if err != nil {
  61. panic(err)
  62. }
  63. err = root.AddObject(
  64. cmd.Up,
  65. cmd.Env,
  66. cmd.Fix,
  67. cmd.Run,
  68. cmd.Gen,
  69. cmd.Tpl,
  70. cmd.Init,
  71. cmd.Pack,
  72. cmd.Build,
  73. cmd.Docker,
  74. cmd.Install,
  75. cmd.Version,
  76. )
  77. if err != nil {
  78. return nil, err
  79. }
  80. command := &Command{
  81. root,
  82. }
  83. return command, nil
  84. }
  85. // zsh alias "git fetch" conflicts checks.
  86. func handleZshAlias() {
  87. if home, err := gfile.Home(); err == nil {
  88. zshPath := gfile.Join(home, ".zshrc")
  89. if gfile.Exists(zshPath) {
  90. var (
  91. aliasCommand = `alias gf=gf`
  92. content = gfile.GetContents(zshPath)
  93. )
  94. if !gstr.Contains(content, aliasCommand) {
  95. _ = gfile.PutContentsAppend(zshPath, "\n"+aliasCommand+"\n")
  96. }
  97. }
  98. }
  99. }