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

     1  package commands
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/chenbh/concourse/v6/fly/commands/internal/displayhelpers"
     7  	"github.com/chenbh/concourse/v6/fly/commands/internal/flaghelpers"
     8  	"github.com/chenbh/concourse/v6/fly/rc"
     9  	"github.com/chenbh/concourse/v6/go-concourse/concourse"
    10  )
    11  
    12  type UnpausePipelineCommand struct {
    13  	Pipeline flaghelpers.PipelineFlag `short:"p" long:"pipeline" description:"Pipeline to unpause"`
    14  	All      bool                     `short:"a" long:"all"      description:"Unpause all pipelines"`
    15  	Team     string                   `long:"team"              description:"Name of the team to which the pipeline belongs, if different from the target default"`
    16  }
    17  
    18  func (command *UnpausePipelineCommand) Validate() error {
    19  	_, err := command.Pipeline.Validate()
    20  	return err
    21  }
    22  
    23  func (command *UnpausePipelineCommand) Execute(args []string) error {
    24  	if string(command.Pipeline) == "" && !command.All {
    25  		displayhelpers.Failf("one of the flags '-p, --pipeline' or '-a, --all' is required")
    26  	}
    27  
    28  	if string(command.Pipeline) != "" && command.All {
    29  		displayhelpers.Failf("only one of the flags '-p, --pipeline' or '-a, --all' is allowed")
    30  	}
    31  
    32  	err := command.Validate()
    33  	if err != nil {
    34  		return err
    35  	}
    36  
    37  	target, err := rc.LoadTarget(Fly.Target, Fly.Verbose)
    38  	if err != nil {
    39  		return err
    40  	}
    41  
    42  	err = target.Validate()
    43  	if err != nil {
    44  		return err
    45  	}
    46  
    47  	var team concourse.Team
    48  
    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 pipelineNames []string
    59  	if string(command.Pipeline) != "" {
    60  		pipelineNames = []string{string(command.Pipeline)}
    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  			pipelineNames = append(pipelineNames, pipeline.Name)
    71  		}
    72  	}
    73  
    74  	for _, pipelineName := range pipelineNames {
    75  		found, err := team.UnpausePipeline(pipelineName)
    76  		if err != nil {
    77  			return err
    78  		}
    79  
    80  		if found {
    81  			fmt.Printf("unpaused '%s'\n", pipelineName)
    82  		} else {
    83  			displayhelpers.Failf("pipeline '%s' not found\n", pipelineName)
    84  		}
    85  	}
    86  
    87  	return nil
    88  }