cobra.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package cmd
  2. import (
  3. "Medical_OAuth/common/global"
  4. "errors"
  5. "fmt"
  6. "os"
  7. "github.com/spf13/cobra"
  8. "Medical_OAuth/cmd/api"
  9. "Medical_OAuth/cmd/config"
  10. "Medical_OAuth/cmd/migrate"
  11. "gogs.baozhida.cn/zoie/OAuth-core/pkg"
  12. )
  13. var rootCmd = &cobra.Command{
  14. Use: "Medical_OAuth",
  15. Short: "Medical_OAuth",
  16. SilenceUsage: true,
  17. Long: `OAuth`,
  18. Args: func(cmd *cobra.Command, args []string) error {
  19. if len(args) < 1 {
  20. tip()
  21. return errors.New(pkg.Red("requires at least one arg"))
  22. }
  23. return nil
  24. },
  25. PersistentPreRunE: func(*cobra.Command, []string) error { return nil },
  26. Run: func(cmd *cobra.Command, args []string) {
  27. tip()
  28. },
  29. }
  30. func tip() {
  31. usageStr := `欢迎使用 ` + pkg.Green(`OAuth `+global.Version) + ` 可以使用 ` + pkg.Red(`-h`) + ` 查看命令`
  32. fmt.Printf("%s\n", usageStr)
  33. }
  34. func init() {
  35. rootCmd.AddCommand(api.StartCmd)
  36. rootCmd.AddCommand(config.StartCmd)
  37. rootCmd.AddCommand(migrate.StartCmd)
  38. }
  39. // Execute : apply commands
  40. func Execute() {
  41. if err := rootCmd.Execute(); err != nil {
  42. os.Exit(-1)
  43. }
  44. }