package cmd

import (
	"os/exec"

	log "github.com/sirupsen/logrus"
	"github.com/spf13/cobra"
)

// downloadCmd represents the download command
var downloadCmd = &cobra.Command{
	Use:   "download",
	Short: "Download TiDB binaries to the Control Machine",
	Long:  `Download TiDB and monitoring modules binaries to the Control Machine.`,
	Run:   download,
}

func init() {
	rootCmd.AddCommand(downloadCmd)
}

func download(cmd *cobra.Command, args []string) {
	preTask()

	go progressBar(downloadTaskCount)

	command := exec.Command("sh", "-c", "ansible-playbook local_prepare.yml")
	err := command.Run()
	if err != nil {
		printFailedLog()
		log.Fatalln("failed to download TiDB binaries.")
	}

	postTask()
}
