cobra.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package cmd
  2. import (
  3. "errors"
  4. "fmt"
  5. "os"
  6. "github.com/spf13/cobra"
  7. "device-manage/cmd/api"
  8. "device-manage/cmd/config"
  9. "device-manage/cmd/migrate"
  10. "device-manage/cmd/version"
  11. "device-manage/common/global"
  12. "device-manage/tools"
  13. )
  14. var rootCmd = &cobra.Command{
  15. Use: "device-manage",
  16. Short: "device-manage",
  17. SilenceUsage: true,
  18. Long: `device-manage`,
  19. Args: func(cmd *cobra.Command, args []string) error {
  20. if len(args) < 1 {
  21. tip()
  22. return errors.New(tools.Red("requires at least one arg"))
  23. }
  24. return nil
  25. },
  26. PersistentPreRunE: func(*cobra.Command, []string) error { return nil },
  27. Run: func(cmd *cobra.Command, args []string) {
  28. tip()
  29. },
  30. }
  31. func tip() {
  32. usageStr := `欢迎使用 ` + tools.Green(`device-manage `+global.Version) + ` 可以使用 ` + tools.Red(`-h`) + ` 查看命令`
  33. usageStr1 := `也可以参考 http://doc.zhangwj.com/device-manage-site/guide/ksks.html 里边的【启动】章节`
  34. fmt.Printf("%s\n", usageStr)
  35. fmt.Printf("%s\n", usageStr1)
  36. }
  37. func init() {
  38. rootCmd.AddCommand(api.StartCmd)
  39. rootCmd.AddCommand(migrate.StartCmd)
  40. rootCmd.AddCommand(version.StartCmd)
  41. rootCmd.AddCommand(config.StartCmd)
  42. }
  43. //Execute : apply commands
  44. func Execute() {
  45. if err := rootCmd.Execute(); err != nil {
  46. os.Exit(-1)
  47. }
  48. }