package cmd

import (
	"cold-delivery/common/global"
	"errors"
	"fmt"
	"os"

	"github.com/spf13/cobra"

	"cold-delivery/cmd/api"
	"cold-delivery/cmd/config"
	"cold-delivery/cmd/migrate"
	"gogs.baozhida.cn/zoie/OAuth-core/pkg"
)

var rootCmd = &cobra.Command{
	Use:          "cold-delivery",
	Short:        "cold-delivery",
	SilenceUsage: true,
	Long:         `OAuth`,
	Args: func(cmd *cobra.Command, args []string) error {
		if len(args) < 1 {
			tip()
			return errors.New(pkg.Red("requires at least one arg"))
		}
		return nil
	},
	PersistentPreRunE: func(*cobra.Command, []string) error { return nil },
	Run: func(cmd *cobra.Command, args []string) {
		tip()
	},
}

func tip() {
	usageStr := `欢迎使用 ` + pkg.Green(`OAuth `+global.Version) + ` 可以使用 ` + pkg.Red(`-h`) + ` 查看命令`
	fmt.Printf("%s\n", usageStr)
}

func init() {
	rootCmd.AddCommand(api.StartCmd)
	rootCmd.AddCommand(config.StartCmd)
	rootCmd.AddCommand(migrate.StartCmd)
}

// Execute : apply commands
func Execute() {
	if err := rootCmd.Execute(); err != nil {
		os.Exit(-1)
	}
}
