Skip to content

Commit

Permalink
no pretty print; no api calls when --host flag
Browse files Browse the repository at this point in the history
  • Loading branch information
awilliams committed Mar 19, 2014
1 parent f7e9972 commit 6aa5485
Showing 1 changed file with 19 additions and 58 deletions.
77 changes: 19 additions & 58 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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() {
Expand All @@ -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 {
Expand All @@ -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")
}
}

0 comments on commit 6aa5485

Please sign in to comment.