github.com/chenbh/concourse/v6@v6.4.2/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/chenbh/concourse/v6/atc"
    12  	"github.com/chenbh/concourse/v6/fly/commands/internal/displayhelpers"
    13  	"github.com/chenbh/concourse/v6/fly/commands/internal/flaghelpers"
    14  	"github.com/chenbh/concourse/v6/fly/rc"
    15  	"github.com/chenbh/concourse/v6/fly/ui"
    16  	"github.com/mattn/go-isatty"
    17  )
    18  
    19  type GetPipelineCommand struct {
    20  	Pipeline flaghelpers.PipelineFlag `short:"p" long:"pipeline" required:"true" description:"Get configuration of this pipeline"`
    21  	JSON     bool                     `short:"j" long:"json"                     description:"Print config as json instead of yaml"`
    22  }
    23  
    24  func (command *GetPipelineCommand) Validate() error {
    25  	_, err := command.Pipeline.Validate()
    26  	return err
    27  }
    28  
    29  func (command *GetPipelineCommand) Execute(args []string) error {
    30  	err := command.Validate()
    31  	if err != nil {
    32  		return err
    33  	}
    34  
    35  	asJSON := command.JSON
    36  	pipelineName := string(command.Pipeline)
    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  	config, _, found, err := target.Team().PipelineConfig(pipelineName)
    49  	if err != nil {
    50  		return err
    51  	}
    52  
    53  	if !found {
    54  		return errors.New("pipeline not found")
    55  	}
    56  
    57  	return dump(config, asJSON)
    58  }
    59  
    60  func dump(config atc.Config, asJSON bool) error {
    61  	var payload []byte
    62  	var err error
    63  	if asJSON {
    64  		payload, err = json.Marshal(config)
    65  	} else {
    66  		payload, err = yaml.Marshal(config)
    67  	}
    68  	if err != nil {
    69  		return err
    70  	}
    71  
    72  	_, err = fmt.Printf("%s", payload)
    73  
    74  	return err
    75  }
    76  
    77  func (command *GetPipelineCommand) showConfigWarning() {
    78  	if isatty.IsTerminal(os.Stdout.Fd()) {
    79  		fmt.Fprintln(ui.Stderr, "")
    80  	}
    81  	displayhelpers.PrintWarningHeader()
    82  	fmt.Fprintln(ui.Stderr, "Existing config is invalid, it was returned as-is")
    83  }