cobra.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package cmd
  2. import (
  3. "encoding/base64"
  4. "errors"
  5. "fmt"
  6. "go-admin/cmd/app"
  7. "go-admin/cmd/consumer"
  8. "go-admin/cmd/gen"
  9. "go-admin/common/global"
  10. "go-admin/utils"
  11. "os"
  12. "time"
  13. "github.com/go-admin-team/go-admin-core/sdk/pkg"
  14. "github.com/spf13/cobra"
  15. "go-admin/cmd/alert"
  16. "go-admin/cmd/api"
  17. "go-admin/cmd/config"
  18. "go-admin/cmd/migrate"
  19. "go-admin/cmd/neo"
  20. "go-admin/cmd/version"
  21. )
  22. var rootCmd = &cobra.Command{
  23. Use: "go-admin",
  24. Short: "go-admin",
  25. SilenceUsage: true,
  26. Long: `go-admin`,
  27. Args: func(cmd *cobra.Command, args []string) error {
  28. if len(args) < 1 {
  29. tip()
  30. return errors.New(pkg.Red("requires at least one arg"))
  31. }
  32. return nil
  33. },
  34. PersistentPreRunE: func(*cobra.Command, []string) error { return nil },
  35. Run: func(cmd *cobra.Command, args []string) {
  36. tip()
  37. },
  38. }
  39. func tip() {
  40. usageStr := `欢迎使用 ` + pkg.Green(`go-admin `+global.Version) + ` 可以使用 ` + pkg.Red(`-h`) + ` 查看命令`
  41. usageStr1 := `也可以参考 https://doc.go-admin.dev/guide/ksks 的相关内容`
  42. fmt.Printf("%s\n", usageStr)
  43. fmt.Printf("%s\n", usageStr1)
  44. }
  45. func init() {
  46. rootCmd.AddCommand(api.StartCmd)
  47. rootCmd.AddCommand(migrate.StartCmd)
  48. rootCmd.AddCommand(version.StartCmd)
  49. rootCmd.AddCommand(config.StartCmd)
  50. rootCmd.AddCommand(app.StartCmd)
  51. rootCmd.AddCommand(gen.StartCmd)
  52. rootCmd.AddCommand(consumer.StartCmd)
  53. rootCmd.AddCommand(alert.StartCmd)
  54. rootCmd.AddCommand(neo.StartCmd)
  55. }
  56. // Execute : apply commands
  57. func Execute() {
  58. fmt.Println("timenow: ", time.Now())
  59. if !checkTime() {
  60. fmt.Printf("Trial expired.")
  61. os.Exit(-1)
  62. }
  63. if err := rootCmd.Execute(); err != nil {
  64. os.Exit(-1)
  65. }
  66. }
  67. func checkTime() bool {
  68. if utils.GetDefaultEnv("RAIKK", "n") == "y" {
  69. // 2023.12.30 +++> 1703904495000 +++> MTcwMzkwNDQ5NTAwMA==
  70. fmt.Println("check trial...")
  71. timePermission64s, err := base64.StdEncoding.DecodeString(utils.GetDefaultEnv("DRTTY", "MjAyMzEyMzA="))
  72. if err != nil {
  73. fmt.Println("endcoding err:", err)
  74. return false
  75. }
  76. timePermission, err := time.Parse("20060102", string(timePermission64s))
  77. if err != nil {
  78. fmt.Println("trans:", err)
  79. return false
  80. }
  81. currentTime := time.Now()
  82. if currentTime.After(timePermission) {
  83. return false
  84. }
  85. }
  86. return true
  87. }