package commands

import (
	"fmt"

	"github.com/concourse/concourse/fly/rc"
)

type PruneWorkerCommand struct {
	Worker string `short:"w"  long:"worker" required:"true" description:"Worker to prune"`
}

func (command *PruneWorkerCommand) Execute(args []string) error {
	workerName := command.Worker

	target, err := rc.LoadTarget(Fly.Target, Fly.Verbose)
	if err != nil {
		return err
	}

	err = target.Validate()
	if err != nil {
		return err
	}

	err = target.Client().PruneWorker(workerName)
	if err != nil {
		return err
	}

	fmt.Printf("pruned '%s'\n", workerName)

	return nil
}

package commands

import (
	"fmt"

	"github.com/concourse/concourse/fly/commands/internal/displayhelpers"
	"github.com/concourse/concourse/fly/rc"
)

type PruneWorkerCommand struct {
	Worker     string `short:"w"  long:"worker" description:"Worker to prune"`
	AllStalled bool   `short:"a" long:"all-stalled" description:"Prune all stalled workers"`
}

func (command *PruneWorkerCommand) Execute(args []string) error {
	if command.Worker == "" && !command.AllStalled {
		displayhelpers.Failf("Either a worker name or --all-stalled are required")
	}

	var workersNames []string

	if command.Worker != "" {
		workersNames = append(workersNames, command.Worker)
	}

	target, err := rc.LoadTarget(Fly.Target, Fly.Verbose)
	if err != nil {
		return err
	}

	err = target.Validate()
	if err != nil {
		return err
	}

	if command.AllStalled {
		workers, err := target.Client().ListWorkers()
		if err != nil {
			return err
		}
		for _, worker := range workers {
			if worker.State == "stalled" {
				workersNames = append(workersNames, worker.Name)
			}
		}
		if workersNames == nil {
			displayhelpers.Failf("No stalled worker found.")
		}
	}

	for _, workerName := range workersNames {
		err = target.Client().PruneWorker(workerName)
		if err != nil {
			return err
		}

		fmt.Printf("pruned '%s'\n", workerName)
	}
	return nil
}

Add option to prune all stalled workers instead of just one at the time.
Add option to prune all stalled workers instead of just one at the time.

Signed-off-by: Alessandro Degano <55cacd5b504daf5c7cafa0b6b8e4521b86a643c4@pix4d.com>
