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

     1  package commands
     2  
     3  import (
     4  	"os"
     5  
     6  	"github.com/pf-qiu/concourse/v6/atc"
     7  	"github.com/pf-qiu/concourse/v6/fly/commands/internal/displayhelpers"
     8  	"github.com/pf-qiu/concourse/v6/fly/commands/internal/flaghelpers"
     9  	"github.com/pf-qiu/concourse/v6/fly/rc"
    10  	"github.com/pf-qiu/concourse/v6/fly/ui"
    11  	"github.com/fatih/color"
    12  )
    13  
    14  type ResourcesCommand struct {
    15  	Pipeline flaghelpers.PipelineFlag `short:"p" long:"pipeline" required:"true" description:"Get resources in this pipeline"`
    16  	Json     bool                     `long:"json" description:"Print command result as JSON"`
    17  }
    18  
    19  func (command *ResourcesCommand) Execute([]string) error {
    20  	target, err := rc.LoadTarget(Fly.Target, Fly.Verbose)
    21  	if err != nil {
    22  		return err
    23  	}
    24  
    25  	err = target.Validate()
    26  	if err != nil {
    27  		return err
    28  	}
    29  
    30  	var headers []string
    31  	var resources []atc.Resource
    32  
    33  	resources, err = target.Team().ListResources(command.Pipeline.Ref())
    34  	if err != nil {
    35  		return err
    36  	}
    37  
    38  	if command.Json {
    39  		err = displayhelpers.JsonPrint(resources)
    40  		if err != nil {
    41  			return err
    42  		}
    43  		return nil
    44  	}
    45  
    46  	headers = []string{"name", "type", "pinned", "check status"}
    47  	table := ui.Table{Headers: ui.TableRow{}}
    48  	for _, h := range headers {
    49  		table.Headers = append(table.Headers, ui.TableCell{Contents: h, Color: color.New(color.Bold)})
    50  	}
    51  
    52  	for _, resource := range resources {
    53  		var pinnedColumn ui.TableCell
    54  		if resource.PinnedVersion != nil {
    55  			pinnedColumn.Contents = ui.PresentVersion(resource.PinnedVersion)
    56  		} else {
    57  			pinnedColumn.Contents = "n/a"
    58  		}
    59  
    60  		var statusColumn ui.TableCell
    61  		if resource.Build != nil {
    62  			statusColumn = ui.BuildStatusCell(resource.Build.Status)
    63  		} else {
    64  			statusColumn.Contents = "n/a"
    65  			statusColumn.Color = ui.OffColor
    66  		}
    67  
    68  		table.Data = append(table.Data, ui.TableRow{
    69  			ui.TableCell{Contents: resource.Name},
    70  			ui.TableCell{Contents: resource.Type},
    71  			pinnedColumn,
    72  			statusColumn,
    73  		})
    74  	}
    75  
    76  	return table.Render(os.Stdout, Fly.PrintTableHeaders)
    77  }