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

     1  package commands
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"github.com/pf-qiu/concourse/v6/go-concourse/concourse"
     7  	"sort"
     8  
     9  	"github.com/pf-qiu/concourse/v6/atc"
    10  	"github.com/pf-qiu/concourse/v6/fly/commands/internal/displayhelpers"
    11  	"github.com/pf-qiu/concourse/v6/fly/commands/internal/flaghelpers"
    12  	"github.com/pf-qiu/concourse/v6/fly/rc"
    13  )
    14  
    15  var ErrMissingPipelineName = errors.New("Need to specify atleast one pipeline name")
    16  
    17  type OrderPipelinesCommand struct {
    18  	Alphabetical bool                       `short:"a"  long:"alphabetical" description:"Order all pipelines alphabetically"`
    19  	Pipelines    []flaghelpers.PipelineFlag `short:"p" long:"pipeline" description:"Name of pipeline to order"`
    20  	Team         string                     `long:"team" description:"Name of the team to which the pipelines belong, if different from the target default"`
    21  }
    22  
    23  func (command *OrderPipelinesCommand) Validate() (atc.OrderPipelinesRequest, error) {
    24  	var pipelineRefs atc.OrderPipelinesRequest
    25  
    26  	for _, p := range command.Pipelines {
    27  		_, err := p.Validate()
    28  		if err != nil {
    29  			return nil, err
    30  		}
    31  		pipelineRefs = append(pipelineRefs, p.Ref())
    32  	}
    33  	return pipelineRefs, nil
    34  }
    35  
    36  func (command *OrderPipelinesCommand) Execute(args []string) error {
    37  	var pipelineRefs atc.OrderPipelinesRequest
    38  
    39  	if !command.Alphabetical && command.Pipelines == nil {
    40  		displayhelpers.Failf("error: either --pipeline or --alphabetical are required")
    41  	}
    42  
    43  	target, err := rc.LoadTarget(Fly.Target, Fly.Verbose)
    44  	if err != nil {
    45  		return err
    46  	}
    47  
    48  	err = target.Validate()
    49  	if err != nil {
    50  		return err
    51  	}
    52  
    53  	if command.Alphabetical {
    54  		ps, err := target.Team().ListPipelines()
    55  		if err != nil {
    56  			return err
    57  		}
    58  
    59  		for _, p := range ps {
    60  			pipelineRefs = append(pipelineRefs, p.Ref())
    61  		}
    62  		sort.Sort(pipelineRefs)
    63  	} else {
    64  		pipelineRefs, err = command.Validate()
    65  		if err != nil {
    66  			return err
    67  		}
    68  	}
    69  
    70  	var team concourse.Team
    71  	if command.Team != "" {
    72  		team, err = target.FindTeam(command.Team)
    73  		if err != nil {
    74  			return err
    75  		}
    76  	} else {
    77  		team = target.Team()
    78  	}
    79  
    80  	err = team.OrderingPipelines(pipelineRefs)
    81  	if err != nil {
    82  		displayhelpers.FailWithErrorf("failed to order pipelines", err)
    83  	}
    84  
    85  	fmt.Printf("ordered pipelines \n")
    86  	for _, p := range pipelineRefs {
    87  		fmt.Printf("  - %s \n", p.String())
    88  	}
    89  
    90  	return nil
    91  }