github.com/misfo/deis@v1.0.1-0.20141111224634-e0eee0392b8a/deisctl/backend/fleet/list_unit_files.go (about) 1 package fleet 2 3 import ( 4 "fmt" 5 "os" 6 "sort" 7 "strconv" 8 "strings" 9 10 "github.com/coreos/fleet/job" 11 "github.com/coreos/fleet/machine" 12 "github.com/coreos/fleet/schema" 13 ) 14 15 const ( 16 defaultListUnitFilesFields = "unit,hash,dstate,state,tmachine" 17 ) 18 19 var ( 20 listUnitFilesFields = map[string]unitToField{ 21 "unit": func(u schema.Unit, full bool) string { 22 return u.Name 23 }, 24 "global": func(u schema.Unit, full bool) string { 25 return strconv.FormatBool(suToGlobal(u)) 26 }, 27 "dstate": func(u schema.Unit, full bool) string { 28 if u.DesiredState == "" { 29 return "-" 30 } 31 return u.DesiredState 32 }, 33 "tmachine": func(u schema.Unit, full bool) string { 34 if suToGlobal(u) || u.MachineID == "" { 35 return "-" 36 } 37 ms := cachedMachineState(u.MachineID) 38 if ms == nil { 39 ms = &machine.MachineState{ID: u.MachineID} 40 } 41 42 return machineFullLegend(*ms, full) 43 }, 44 "state": func(u schema.Unit, full bool) string { 45 if suToGlobal(u) || u.CurrentState == "" { 46 return "-" 47 } 48 return u.CurrentState 49 }, 50 "hash": func(u schema.Unit, full bool) string { 51 uf := schema.MapSchemaUnitOptionsToUnitFile(u.Options) 52 if !full { 53 return uf.Hash().Short() 54 } 55 return uf.Hash().String() 56 }, 57 "desc": func(u schema.Unit, full bool) string { 58 uf := schema.MapSchemaUnitOptionsToUnitFile(u.Options) 59 d := uf.Description() 60 if d == "" { 61 return "-" 62 } 63 return d 64 }, 65 } 66 ) 67 68 type unitToField func(u schema.Unit, full bool) string 69 70 // ListUnitFiles prints all Deis-related unit files to Stdout 71 func (c *FleetClient) ListUnitFiles() (err error) { 72 var sortable sort.StringSlice 73 units := make(map[string]*schema.Unit, 0) 74 75 us, err := cAPI.Units() 76 if err != nil { 77 return err 78 } 79 80 for _, u := range us { 81 if strings.HasPrefix(u.Name, "deis-") { 82 units[u.Name] = u 83 sortable = append(sortable, u.Name) 84 } 85 } 86 sortable.Sort() 87 printUnitFiles(units, sortable) 88 return 89 } 90 91 // printUnitFiles writes unit files to stdout using a tabwriter 92 func printUnitFiles(units map[string]*schema.Unit, sortable sort.StringSlice) { 93 cols := strings.Split(defaultListUnitFilesFields, ",") 94 for _, s := range cols { 95 if _, ok := listUnitsFields[s]; !ok { 96 fmt.Fprintf(os.Stderr, "Invalid key in output format: %q\n", s) 97 } 98 } 99 fmt.Fprintln(out, strings.ToUpper(strings.Join(cols, "\t"))) 100 for _, name := range sortable { 101 var f []string 102 u := units[name] 103 for _, c := range cols { 104 f = append(f, listUnitFilesFields[c](*u, false)) 105 } 106 fmt.Fprintln(out, strings.Join(f, "\t")) 107 } 108 out.Flush() 109 } 110 111 // suToGlobal returns whether or not a schema.Unit refers to a global unit 112 func suToGlobal(su schema.Unit) bool { 113 u := job.Unit{ 114 Unit: *schema.MapSchemaUnitOptionsToUnitFile(su.Options), 115 } 116 return u.IsGlobal() 117 }