cmd.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. "strings"
  10. "github.com/gogf/gf/v2"
  11. "github.com/gogf/gf/v2/frame/g"
  12. "github.com/gogf/gf/v2/os/gcmd"
  13. "github.com/gogf/gf/v2/util/gtag"
  14. "github.com/gogf/gf/cmd/gf/v2/internal/service"
  15. "github.com/gogf/gf/cmd/gf/v2/internal/utility/mlog"
  16. )
  17. var (
  18. GF = cGF{}
  19. )
  20. type cGF struct {
  21. g.Meta `name:"gf" ad:"{cGFAd}"`
  22. }
  23. const (
  24. cGFAd = `
  25. ADDITIONAL
  26. Use "gf COMMAND -h" for details about a command.
  27. `
  28. )
  29. func init() {
  30. gtag.Sets(g.MapStrStr{
  31. `cGFAd`: cGFAd,
  32. })
  33. }
  34. type cGFInput struct {
  35. g.Meta `name:"gf"`
  36. Yes bool `short:"y" name:"yes" brief:"all yes for all command without prompt ask" orphan:"true"`
  37. Version bool `short:"v" name:"version" brief:"show version information of current binary" orphan:"true"`
  38. Debug bool `short:"d" name:"debug" brief:"show internal detailed debugging information" orphan:"true"`
  39. }
  40. type cGFOutput struct{}
  41. func (c cGF) Index(ctx context.Context, in cGFInput) (out *cGFOutput, err error) {
  42. // Version.
  43. if in.Version {
  44. _, err = Version.Index(ctx, cVersionInput{})
  45. return
  46. }
  47. answer := "n"
  48. // No argument or option, do installation checks.
  49. if data, isInstalled := service.Install.IsInstalled(); !isInstalled {
  50. mlog.Print("hi, it seams it's the first time you installing gf cli.")
  51. answer = gcmd.Scanf("do you want to install gf(%s) binary to your system? [y/n]: ", gf.VERSION)
  52. } else if !data.IsSelf {
  53. mlog.Print("hi, you have installed gf cli.")
  54. answer = gcmd.Scanf("do you want to install gf(%s) binary to your system? [y/n]: ", gf.VERSION)
  55. }
  56. if strings.EqualFold(answer, "y") {
  57. if err = service.Install.Run(ctx); err != nil {
  58. return
  59. }
  60. gcmd.Scan("press `Enter` to exit...")
  61. return
  62. }
  63. // Print help content.
  64. gcmd.CommandFromCtx(ctx).Print()
  65. return
  66. }