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