github.com/pf-qiu/concourse/v6@v6.7.3-0.20201207032516-1f455d73275f/fly/commands/pause_pipeline.go (about)

     1  package commands
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/pf-qiu/concourse/v6/atc"
     7  	"github.com/pf-qiu/concourse/v6/fly/commands/internal/displayhelpers"
     8  	"github.com/pf-qiu/concourse/v6/fly/commands/internal/flaghelpers"
     9  	"github.com/pf-qiu/concourse/v6/fly/rc"
    10  	"github.com/pf-qiu/concourse/v6/go-concourse/concourse"
    11  )
    12  
    13  type PausePipelineCommand struct {
    14  	Pipeline *flaghelpers.PipelineFlag `short:"p"   long:"pipeline" description:"Pipeline to pause"`
    15  	All      bool                      `short:"a"   long:"all"      description:"Pause all pipelines"`
    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 *PausePipelineCommand) Validate() error {
    20  	_, err := command.Pipeline.Validate()
    21  	return err
    22  }
    23  
    24  func (command *PausePipelineCommand) Execute(args []string) error {
    25  	if command.Pipeline == nil && !command.All {
    26  		displayhelpers.Failf("one of the flags '-p, --pipeline' or '-a, --all' is required")
    27  	}
    28  
    29  	if command.Pipeline != nil && command.All {
    30  		displayhelpers.Failf("only one of the flags '-p, --pipeline' or '-a, --all' is allowed")
    31  	}
    32  
    33  	err := command.Validate()
    34  	if err != nil {
    35  		return err
    36  	}
    37  
    38  	target, err := rc.LoadTarget(Fly.Target, Fly.Verbose)
    39  	if err != nil {
    40  		return err
    41  	}
    42  
    43  	err = target.Validate()
    44  	if err != nil {
    45  		return err
    46  	}
    47  
    48  	var team concourse.Team
    49  	if command.Team != "" {
    50  		team, err = target.FindTeam(command.Team)
    51  		if err != nil {
    52  			return err
    53  		}
    54  	} else {
    55  		team = target.Team()
    56  	}
    57  
    58  	var pipelineRefs []atc.PipelineRef
    59  	if command.Pipeline != nil {
    60  		pipelineRefs = []atc.PipelineRef{command.Pipeline.Ref()}
    61  	}
    62  
    63  	if command.All {
    64  		pipelines, err := team.ListPipelines()
    65  		if err != nil {
    66  			return err
    67  		}
    68  
    69  		for _, pipeline := range pipelines {
    70  			pipelineRefs = append(pipelineRefs, pipeline.Ref())
    71  		}
    72  	}
    73  
    74  	for _, pipelineRef := range pipelineRefs {
    75  		found, err := team.PausePipeline(pipelineRef)
    76  		if err != nil {
    77  			return err
    78  		}
    79  
    80  		if found {
    81  			fmt.Printf("paused '%s'\n", pipelineRef.String())
    82  		} else {
    83  			displayhelpers.Failf("pipeline '%s' not found\n", pipelineRef.String())
    84  		}
    85  	}
    86  
    87  	return nil
    88  }