package commands

import (
	"os"
	"os/exec"

	"github.com/contiamo/dev/cli/config"
	"github.com/spf13/cobra"
)

var statusCmd = &cobra.Command{
	Use:     "status [<service>...]",
	Aliases: []string{"ps"},
	Short:   "Show the deployment status",
	Long: `This show the current deployment status of your localdev setup.
	
It lists all services, their running state and some other meta info. 
If you provide some one or more service names, the output will only include those services. 
All other arguments are also passed through to docker compose so you can technically also 
inject things like --format=json or --status=exited`,
	ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
		services, err := listServices(cmd, true)
		if err != nil {
			return nil, cobra.ShellCompDirectiveError
		}
		return services, cobra.ShellCompDirectiveDefault
	},
	RunE: func(cmd *cobra.Command, args []string) error {
		statusArgs := []string{
			"-f", "docker-compose.yml",
			"-f", "docker-compose-extra.yml",
			"ps",
		}

		statusArgs = append(statusArgs, args...)

		compose := exec.CommandContext(
			cmd.Context(),
			"docker-compose",
			statusArgs...,
		)
		compose.Dir = config.LocaldevRoot()
		compose.Stderr = cmd.OutOrStderr()
		compose.Stdout = cmd.OutOrStdout()
		compose.Env = os.Environ()

		return compose.Run()

	},
}
