package commands

import (
	"os"
	"os/exec"

	"github.com/spf13/cobra"
)

var dockerAuthCmd = &cobra.Command{
	Use:   "docker-auth",
	Short: "authentication your local docker client for the Contiamo dev repo",
	RunE: func(cmd *cobra.Command, args []string) error {
		login := exec.CommandContext(
			cmd.Context(),
			"gcloud",
			"auth", "login",
		)
		login.Stderr = cmd.OutOrStderr()
		login.Stdout = cmd.OutOrStdout()
		login.Env = os.Environ()

		err := login.Run()
		if err != nil {
			return err
		}

		configure := exec.CommandContext(
			cmd.Context(),
			"gcloud",
			"auth", "configure-docker",
		)
		configure.Stderr = cmd.OutOrStderr()
		configure.Stdout = cmd.OutOrStdout()
		configure.Env = os.Environ()

		return configure.Run()
	},
}
