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

     1  package commands
     2  
     3  import (
     4  	"os"
     5  	"sort"
     6  	"strconv"
     7  
     8  	"github.com/chenbh/concourse/v6/fly/commands/internal/displayhelpers"
     9  	"github.com/chenbh/concourse/v6/fly/rc"
    10  	"github.com/chenbh/concourse/v6/fly/ui"
    11  	"github.com/fatih/color"
    12  )
    13  
    14  type ContainersCommand struct {
    15  	Json bool `long:"json" description:"Print command result as JSON"`
    16  }
    17  
    18  func (command *ContainersCommand) Execute([]string) error {
    19  	target, err := rc.LoadTarget(Fly.Target, Fly.Verbose)
    20  	if err != nil {
    21  		return err
    22  	}
    23  
    24  	err = target.Validate()
    25  	if err != nil {
    26  		return err
    27  	}
    28  
    29  	containers, err := target.Team().ListContainers(map[string]string{})
    30  	if err != nil {
    31  		return err
    32  	}
    33  
    34  	if command.Json {
    35  		err = displayhelpers.JsonPrint(containers)
    36  		if err != nil {
    37  			return err
    38  		}
    39  		return nil
    40  	}
    41  
    42  	table := ui.Table{
    43  		Headers: ui.TableRow{
    44  			{Contents: "handle", Color: color.New(color.Bold)},
    45  			{Contents: "worker", Color: color.New(color.Bold)},
    46  			{Contents: "pipeline", Color: color.New(color.Bold)},
    47  			{Contents: "job", Color: color.New(color.Bold)},
    48  			{Contents: "build #", Color: color.New(color.Bold)},
    49  			{Contents: "build id", Color: color.New(color.Bold)},
    50  			{Contents: "type", Color: color.New(color.Bold)},
    51  			{Contents: "name", Color: color.New(color.Bold)},
    52  			{Contents: "attempt", Color: color.New(color.Bold)},
    53  		},
    54  	}
    55  
    56  	for _, c := range containers {
    57  		row := ui.TableRow{
    58  			{Contents: c.ID},
    59  			{Contents: c.WorkerName},
    60  			stringOrDefault(c.PipelineName),
    61  			stringOrDefault(c.JobName),
    62  			stringOrDefault(c.BuildName),
    63  			buildIDOrNone(c.BuildID),
    64  			{Contents: c.Type},
    65  			stringOrDefault(c.StepName + c.ResourceName),
    66  			stringOrDefault(c.Attempt, "n/a"),
    67  		}
    68  
    69  		table.Data = append(table.Data, row)
    70  	}
    71  
    72  	sort.Sort(table.Data)
    73  
    74  	return table.Render(os.Stdout, Fly.PrintTableHeaders)
    75  }
    76  
    77  func buildIDOrNone(id int) ui.TableCell {
    78  	var column ui.TableCell
    79  
    80  	if id == 0 {
    81  		column.Contents = "none"
    82  		column.Color = ui.OffColor
    83  	} else {
    84  		column.Contents = strconv.Itoa(id)
    85  	}
    86  
    87  	return column
    88  }
    89  
    90  func stringOrDefault(containerType string, def ...string) ui.TableCell {
    91  	var column ui.TableCell
    92  
    93  	column.Contents = containerType
    94  	if column.Contents == "" || column.Contents == "[]" {
    95  		if len(def) == 0 {
    96  			column.Contents = "none"
    97  			column.Color = color.New(color.Faint)
    98  		} else {
    99  			column.Contents = def[0]
   100  		}
   101  	}
   102  
   103  	return column
   104  }