github.com/pf-qiu/concourse/v6@v6.7.3-0.20201207032516-1f455d73275f/fly/commands/unpause_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 UnpausePipelineCommand struct {
    14  	Pipeline *flaghelpers.PipelineFlag `short:"p" long:"pipeline" description:"Pipeline to unpause"`
    15  	All      bool                      `short:"a" long:"all"      description:"Unpause 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 *UnpausePipelineCommand) Validate() error {
    20  	_, err := command.Pipeline.Validate()
    21  	return err
    22  }
    23  
    24  func (command *UnpausePipelineCommand) 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  
    50  	if command.Team != "" {
    51  		team, err = target.FindTeam(command.Team)
    52  		if err != nil {
    53  			return err
    54  		}
    55  	} else {
    56  		team = target.Team()
    57  	}
    58  
    59  	var pipelineRefs []atc.PipelineRef
    60  	if command.Pipeline != nil {
    61  		pipelineRefs = []atc.PipelineRef{command.Pipeline.Ref()}
    62  	}
    63  
    64  	if command.All {
    65  		pipelines, err := team.ListPipelines()
    66  		if err != nil {
    67  			return err
    68  		}
    69  
    70  		for _, pipeline := range pipelines {
    71  			pipelineRefs = append(pipelineRefs, pipeline.Ref())
    72  		}
    73  	}
    74  
    75  	for _, pipelineRef := range pipelineRefs {
    76  		found, err := team.UnpausePipeline(pipelineRef)
    77  		if err != nil {
    78  			return err
    79  		}
    80  
    81  		if found {
    82  			fmt.Printf("unpaused '%s'\n", pipelineRef.String())
    83  		} else {
    84  			displayhelpers.Failf("pipeline '%s' not found\n", pipelineRef.String())
    85  		}
    86  	}
    87  
    88  	return nil
    89  }