github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/cmd/juju/controller/listcontrollersformatters.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package controller 5 6 import ( 7 "fmt" 8 "io" 9 "sort" 10 11 "github.com/juju/errors" 12 "github.com/juju/version" 13 14 "github.com/juju/juju/cmd/output" 15 jujuversion "github.com/juju/juju/version" 16 ) 17 18 const ( 19 noValueDisplay = "-" 20 notKnownDisplay = "(unknown)" 21 ) 22 23 func (c *listControllersCommand) formatControllersListTabular(writer io.Writer, value interface{}) error { 24 controllers, ok := value.(ControllerSet) 25 if !ok { 26 return errors.Errorf("expected value of type %T, got %T", controllers, value) 27 } 28 return formatControllersTabular(writer, controllers, !c.refresh) 29 } 30 31 // formatControllersTabular returns a tabular summary of controller/model items 32 // sorted by controller name alphabetically. 33 func formatControllersTabular(writer io.Writer, set ControllerSet, promptRefresh bool) error { 34 tw := output.TabWriter(writer) 35 w := output.Wrapper{tw} 36 37 if promptRefresh && len(set.Controllers) > 0 { 38 fmt.Fprintln(writer, "Use --refresh to see the latest information.") 39 fmt.Fprintln(writer) 40 } 41 w.Println("CONTROLLER", "MODEL", "USER", "ACCESS", "CLOUD/REGION", "MODELS", "MACHINES", "HA", "VERSION") 42 tw.SetColumnAlignRight(5) 43 tw.SetColumnAlignRight(6) 44 tw.SetColumnAlignRight(7) 45 46 names := []string{} 47 for name := range set.Controllers { 48 names = append(names, name) 49 } 50 sort.Strings(names) 51 52 for _, name := range names { 53 c := set.Controllers[name] 54 modelName := noValueDisplay 55 if c.ModelName != "" { 56 modelName = c.ModelName 57 } 58 userName := noValueDisplay 59 access := noValueDisplay 60 if c.User != "" { 61 userName = c.User 62 access = notKnownDisplay 63 if c.Access != "" { 64 access = c.Access 65 } 66 } 67 if name == set.CurrentController { 68 name += "*" 69 w.PrintColor(output.CurrentHighlight, name) 70 } else { 71 w.Print(name) 72 } 73 cloudRegion := c.Cloud 74 if c.CloudRegion != "" { 75 cloudRegion += "/" + c.CloudRegion 76 } 77 agentVersion := c.AgentVersion 78 staleVersion := false 79 if agentVersion == "" { 80 agentVersion = notKnownDisplay 81 } else { 82 agentVersionNum, err := version.Parse(agentVersion) 83 staleVersion = err == nil && jujuversion.Current.Compare(agentVersionNum) > 0 84 } 85 machineCount := noValueDisplay 86 if c.MachineCount != nil && *c.MachineCount > 0 { 87 machineCount = fmt.Sprintf("%d", *c.MachineCount) 88 } 89 modelCount := noValueDisplay 90 if c.ModelCount != nil && *c.ModelCount > 0 { 91 modelCount = fmt.Sprintf("%d", *c.ModelCount) 92 } 93 w.Print(modelName, userName, access, cloudRegion, modelCount, machineCount) 94 controllerMachineInfo, warn := controllerMachineStatus(c.ControllerMachines) 95 if warn { 96 w.PrintColor(output.WarningHighlight, controllerMachineInfo) 97 } else { 98 w.Print(controllerMachineInfo) 99 } 100 if staleVersion { 101 w.PrintColor(output.WarningHighlight, agentVersion) 102 } else { 103 w.Print(agentVersion) 104 } 105 w.Println() 106 } 107 tw.Flush() 108 return nil 109 } 110 111 func controllerMachineStatus(machines *ControllerMachines) (string, bool) { 112 if machines == nil || machines.Total == 0 { 113 return "-", false 114 } 115 if machines.Total == 1 { 116 return "none", false 117 } 118 controllerMachineStatus := "" 119 warn := machines.Active < machines.Total 120 controllerMachineStatus = fmt.Sprintf("%d", machines.Total) 121 if machines.Active < machines.Total { 122 controllerMachineStatus = fmt.Sprintf("%d/%d", machines.Active, machines.Total) 123 } 124 return controllerMachineStatus, warn 125 }