github.com/spg/deis@v1.7.3/deisctl/backend/fleet/list_units.go (about) 1 package fleet 2 3 import ( 4 "fmt" 5 "os" 6 "strings" 7 "text/tabwriter" 8 9 "github.com/coreos/fleet/machine" 10 "github.com/coreos/fleet/schema" 11 "github.com/deis/deis/deisctl/units" 12 ) 13 14 // initialize tabwriter on stdout 15 func init() { 16 out = new(tabwriter.Writer) 17 out.Init(os.Stdout, 0, 8, 1, '\t', 0) 18 } 19 20 const ( 21 //defaultListUnitFields = "unit,state,load,active,sub,machine" 22 defaultListUnitsFields = "unit,machine,load,active,sub" 23 ) 24 25 type usToField func(us *schema.UnitState, full bool) string 26 27 var ( 28 out *tabwriter.Writer 29 listUnitsFields = map[string]usToField{ 30 "unit": func(us *schema.UnitState, full bool) string { 31 if us == nil { 32 return "-" 33 } 34 return us.Name 35 }, 36 "load": func(us *schema.UnitState, full bool) string { 37 if us == nil { 38 return "-" 39 } 40 return us.SystemdLoadState 41 }, 42 "active": func(us *schema.UnitState, full bool) string { 43 if us == nil { 44 return "-" 45 } 46 return us.SystemdActiveState 47 }, 48 "sub": func(us *schema.UnitState, full bool) string { 49 if us == nil { 50 return "-" 51 } 52 return us.SystemdSubState 53 }, 54 "machine": func(us *schema.UnitState, full bool) string { 55 if us == nil || us.MachineID == "" { 56 return "-" 57 } 58 ms := cachedMachineState(us.MachineID) 59 if ms == nil { 60 ms = &machine.MachineState{ID: us.MachineID} 61 } 62 return machineFullLegend(*ms, full) 63 }, 64 "hash": func(us *schema.UnitState, full bool) string { 65 if us == nil || us.Hash == "" { 66 return "-" 67 } 68 if !full { 69 return us.Hash[:7] 70 } 71 return us.Hash 72 }, 73 } 74 ) 75 76 // ListUnits prints all Deis-related units to Stdout 77 func (c *FleetClient) ListUnits() (err error) { 78 var states []*schema.UnitState 79 80 unitStates, err := cAPI.UnitStates() 81 if err != nil { 82 return err 83 } 84 85 for _, us := range unitStates { 86 for _, prefix := range units.Names { 87 if strings.HasPrefix(us.Name, prefix) { 88 states = append(states, us) 89 break 90 } 91 } 92 } 93 printUnits(states) 94 return 95 } 96 97 // printUnits writes units to stdout using a tabwriter 98 func printUnits(states []*schema.UnitState) { 99 cols := strings.Split(defaultListUnitsFields, ",") 100 for _, s := range cols { 101 if _, ok := listUnitsFields[s]; !ok { 102 fmt.Fprintf(os.Stderr, "Invalid key in output format: %q\n", s) 103 } 104 } 105 fmt.Fprintln(out, strings.ToUpper(strings.Join(cols, "\t"))) 106 for _, us := range states { 107 var f []string 108 for _, c := range cols { 109 f = append(f, listUnitsFields[c](us, false)) 110 } 111 fmt.Fprintln(out, strings.Join(f, "\t")) 112 } 113 out.Flush() 114 } 115 116 func machineIDLegend(ms machine.MachineState, full bool) string { 117 legend := ms.ID 118 if !full { 119 legend = fmt.Sprintf("%s...", ms.ShortID()) 120 } 121 return legend 122 } 123 124 func machineFullLegend(ms machine.MachineState, full bool) string { 125 legend := machineIDLegend(ms, full) 126 if len(ms.PublicIP) > 0 { 127 legend = fmt.Sprintf("%s/%s", legend, ms.PublicIP) 128 } 129 return legend 130 }