package command

import (
	"fmt"
	"strings"

	"github.com/juju/errors"
	"github.com/pingcap/schrodinger/manager/client"
	"github.com/spf13/cobra"
)

func mustClientFromCmd(cmd *cobra.Command) *client.Client {
	schURL, err := cmd.Flags().GetString("schrodinger")
	if err != nil {
		ExitWithError(ExitBadArgs, err)
	}

	return client.NewClient(client.Config{
		Addr: schURL,
	})
}

func checkDumpAndLoadArgs(args []string, format, dataDir, filePrefix string) {
	if len(args) > 0 {
		ev := "too many arguments"
		for _, s := range args {
			if strings.HasSuffix(strings.ToLower(s), "json") {
				ev += fmt.Sprintf(`, did you mean --format=%s`, s)
			}
		}

		ExitWithError(ExitBadArgs, errors.New(ev))
	}

	if len(format) == 0 {
		ExitWithError(ExitBadArgs, errors.New("data format not provided"))
	}

	if len(dataDir) == 0 {
		ExitWithError(ExitBadArgs, errors.New("dump directory not provided"))
	}

	if len(filePrefix) == 0 {
		ExitWithError(ExitBadArgs, errors.New("dump directory not provided"))
	}
}
