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

     1  package commands
     2  
     3  import (
     4  	"encoding/json"
     5  	"errors"
     6  	"fmt"
     7  	"os"
     8  
     9  	"sigs.k8s.io/yaml"
    10  
    11  	"github.com/pf-qiu/concourse/v6/atc"
    12  	"github.com/pf-qiu/concourse/v6/fly/commands/internal/displayhelpers"
    13  	"github.com/pf-qiu/concourse/v6/fly/commands/internal/flaghelpers"
    14  	"github.com/pf-qiu/concourse/v6/fly/rc"
    15  	"github.com/pf-qiu/concourse/v6/fly/ui"
    16  	"github.com/pf-qiu/concourse/v6/go-concourse/concourse"
    17  	"github.com/mattn/go-isatty"
    18  )
    19  
    20  type GetPipelineCommand struct {
    21  	Pipeline flaghelpers.PipelineFlag `short:"p" long:"pipeline" required:"true" description:"Get configuration of this pipeline"`
    22  	JSON     bool                     `short:"j" long:"json"                     description:"Print config as json instead of yaml"`
    23  	Team     string                   `long:"team" description:"Name of the team to which the pipeline belongs, if different from the target default"`
    24  }
    25  
    26  func (command *GetPipelineCommand) Validate() error {
    27  	_, err := command.Pipeline.Validate()
    28  	return err
    29  }
    30  
    31  func (command *GetPipelineCommand) Execute(args []string) error {
    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  	config, _, found, err := team.PipelineConfig(command.Pipeline.Ref())
    59  	if err != nil {
    60  		return err
    61  	}
    62  
    63  	if !found {
    64  		return errors.New("pipeline not found")
    65  	}
    66  
    67  	return dump(config, command.JSON)
    68  }
    69  
    70  func dump(config atc.Config, asJSON bool) error {
    71  	var payload []byte
    72  	var err error
    73  	if asJSON {
    74  		payload, err = json.Marshal(config)
    75  	} else {
    76  		payload, err = yaml.Marshal(config)
    77  	}
    78  	if err != nil {
    79  		return err
    80  	}
    81  
    82  	_, err = fmt.Printf("%s", payload)
    83  
    84  	return err
    85  }
    86  
    87  func (command *GetPipelineCommand) showConfigWarning() {
    88  	if isatty.IsTerminal(os.Stdout.Fd()) {
    89  		fmt.Fprintln(ui.Stderr, "")
    90  	}
    91  	displayhelpers.PrintWarningHeader()
    92  	fmt.Fprintln(ui.Stderr, "Existing config is invalid, it was returned as-is")
    93  }