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

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