github.com/chenbh/concourse/v6@v6.4.2/fly/commands/destroy_pipeline.go (about)

     1  package commands
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/chenbh/concourse/v6/fly/commands/internal/flaghelpers"
     7  	"github.com/chenbh/concourse/v6/fly/rc"
     8  	"github.com/chenbh/concourse/v6/go-concourse/concourse"
     9  	"github.com/vito/go-interact/interact"
    10  )
    11  
    12  type DestroyPipelineCommand struct {
    13  	Pipeline        flaghelpers.PipelineFlag `short:"p"  long:"pipeline" required:"true" description:"Pipeline to destroy"`
    14  	SkipInteractive bool                     `short:"n"  long:"non-interactive"          description:"Destroy the pipeline without confirmation"`
    15  
    16  	Team string `long:"team" description:"Name of the team to which the pipeline belongs, if different from the target default"`
    17  }
    18  
    19  func (command *DestroyPipelineCommand) Validate() error {
    20  	_, err := command.Pipeline.Validate()
    21  	return err
    22  }
    23  
    24  func (command *DestroyPipelineCommand) Execute(args []string) error {
    25  	err := command.Validate()
    26  	if err != nil {
    27  		return err
    28  
    29  	}
    30  	target, err := rc.LoadTarget(Fly.Target, Fly.Verbose)
    31  	if err != nil {
    32  		return err
    33  	}
    34  
    35  	err = target.Validate()
    36  	if err != nil {
    37  		return err
    38  	}
    39  
    40  	var team concourse.Team
    41  
    42  	if command.Team != "" {
    43  		team, err = target.FindTeam(command.Team)
    44  		if err != nil {
    45  			return err
    46  		}
    47  	} else {
    48  		team = target.Team()
    49  	}
    50  
    51  	pipelineName := string(command.Pipeline)
    52  	fmt.Printf("!!! this will remove all data for pipeline `%s`\n\n", pipelineName)
    53  
    54  	confirm := command.SkipInteractive
    55  	if !confirm {
    56  		err := interact.NewInteraction("are you sure?").Resolve(&confirm)
    57  		if err != nil || !confirm {
    58  			fmt.Println("bailing out")
    59  			return err
    60  		}
    61  	}
    62  
    63  	found, err := team.DeletePipeline(pipelineName)
    64  	if err != nil {
    65  		return err
    66  	}
    67  
    68  	if !found {
    69  		fmt.Printf("`%s` does not exist\n", pipelineName)
    70  	} else {
    71  		fmt.Printf("`%s` deleted\n", pipelineName)
    72  	}
    73  
    74  	return nil
    75  }