From 6aa548577746d997fbaace279b2bd0d31f406d12 Mon Sep 17 00:00:00 2001 From: awilliams Date: Wed, 19 Mar 2014 19:35:51 +0100 Subject: [PATCH] no pretty print; no api calls when --host flag --- main.go | 77 ++++++++++++++------------------------------------------- 1 file changed, 19 insertions(+), 58 deletions(-) diff --git a/main.go b/main.go index b7761b3..412023a 100644 --- a/main.go +++ b/main.go @@ -4,10 +4,8 @@ import ( "code.google.com/p/gcfg" "fmt" "github.com/awilliams/linode-inventory/api" - "github.com/mgutz/ansi" "log" "os" - "sort" "path/filepath" ) @@ -42,45 +40,19 @@ func getConfig() (*Configuration, error) { return &config.Linode, nil } -type sortedLinodes []*api.Linode - -func (self sortedLinodes) Len() int { - return len(self) -} -func (self sortedLinodes) Swap(i, j int) { - self[i], self[j] = self[j], self[i] -} -func (self sortedLinodes) Less(i, j int) bool { - return self[i].Label < self[j].Label -} - -func printLinodes(linodes api.Linodes) { - grouped := make(map[string]sortedLinodes) - for _, linode := range linodes { - grouped[linode.DisplayGroup] = append(grouped[linode.DisplayGroup], linode) +func getLinodes(config *Configuration) (api.Linodes, error) { + linodes, err := api.LinodeListWithIps(config.ApiKey) + if err != nil { + return nil, err } - for displayGroup, linodes := range grouped { - sort.Sort(linodes) - fmt.Printf("[%s]\n\n", ansi.Color(displayGroup, "green")) - for _, linode := range linodes { - labelColor := "magenta" - if linode.Status != 1 { - labelColor = "blue" - } - fmt.Printf(" * %-25s\tRunning=%v, Ram=%d, LinodeId=%d\n", ansi.Color(linode.Label, labelColor), linode.Status == 1, linode.Ram, linode.Id) - linode.SortIps() - for _, ip := range linode.Ips { - var ipType string - if ip.Public == 1 { - ipType = "Public" - } else { - ipType = "Private" - } - fmt.Printf(" %-15s\t%s\n", ip.Ip, ipType) - } - fmt.Println("") - } + // 1 == running + linodes = linodes.FilterByStatus(1) + // only apply DisplayGroup filter when using ansible feature + if config.DisplayGroup != "" { + linodes = linodes.FilterByDisplayGroup(config.DisplayGroup) } + + return linodes, nil } func main() { @@ -89,22 +61,16 @@ func main() { log.Fatal(err) } - linodes, err := api.LinodeListWithIps(config.ApiKey) - if err != nil { - log.Fatal(err) - } - // --list and --host are called from Ansible // see: http://docs.ansible.com/developing_inventory.html - if len(os.Args) > 1 && os.Args[1][0:2] == "--" { - // 1 == running - linodes = linodes.FilterByStatus(1) - // only apply DisplayGroup filter when using ansible feature - if config.DisplayGroup != "" { - linodes = linodes.FilterByDisplayGroup(config.DisplayGroup) - } + if len(os.Args) > 1 && os.Args[1][0:2] == "--" { switch os.Args[1] { case "--list": + linodes, err := getLinodes(config) + if err != nil { + log.Fatal(err) + } + inventory := makeInventory(linodes) inventoryJson, err := inventory.toJson() if err != nil { @@ -115,14 +81,9 @@ func main() { // empty hash fmt.Fprint(os.Stdout, "{}") default: - fmt.Errorf("Unrecognized flag: %v\nAvailable flags: --list or --host\n", os.Args[1]) + fmt.Fprintf(os.Stderr, "Unrecognized flag: %v\nUsage: linode-inventory --list or --host\n", os.Args[1]) } } else { - // non-ansible case, just print linodes - // optionally using first arg as display group filter - if len(os.Args) > 1 { - linodes = linodes.FilterByDisplayGroup(os.Args[1]) - } - printLinodes(linodes) + fmt.Fprint(os.Stderr, "Usage: linode-inventory --list or --host\n") } }