2015-02-06 22:31:59 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
2016-02-24 13:03:36 +01:00
|
|
|
func gatherResources(s *state) map[string][]string {
|
2015-02-06 22:31:59 +01:00
|
|
|
groups := make(map[string][]string, 0)
|
2015-12-10 04:54:19 +01:00
|
|
|
for _, res := range s.resources() {
|
2015-12-15 04:24:41 +01:00
|
|
|
for _, grp := range res.Groups() {
|
2016-02-16 12:12:49 +01:00
|
|
|
tmpGroup := []string{}
|
2015-12-10 04:54:19 +01:00
|
|
|
|
2015-12-15 04:24:41 +01:00
|
|
|
_, ok := groups[grp]
|
2016-02-16 12:12:49 +01:00
|
|
|
if ok {
|
|
|
|
tmpGroup = groups[grp].([]string)
|
2015-12-15 04:24:41 +01:00
|
|
|
}
|
2015-12-10 04:54:19 +01:00
|
|
|
|
2016-02-16 12:12:49 +01:00
|
|
|
tmpGroup = append(tmpGroup, res.Address())
|
|
|
|
groups[grp] = tmpGroup
|
2015-12-15 04:24:41 +01:00
|
|
|
}
|
2015-02-06 22:31:59 +01:00
|
|
|
}
|
|
|
|
|
2016-02-18 11:46:40 +01:00
|
|
|
if(len(s.outputs()) > 0) {
|
|
|
|
groups["all"] = make(map[string]string, 0)
|
|
|
|
for _, out := range s.outputs() {
|
|
|
|
groups["all"].(map[string]string)[out.keyName] = out.value
|
|
|
|
}
|
2016-02-16 12:12:49 +01:00
|
|
|
}
|
2016-02-24 13:03:36 +01:00
|
|
|
return groups
|
|
|
|
}
|
|
|
|
|
|
|
|
func cmdList(stdout io.Writer, stderr io.Writer, s *state) int {
|
|
|
|
return output(stdout, stderr, gatherResources(s))
|
|
|
|
}
|
|
|
|
|
|
|
|
func cmdInventory(stdout io.Writer, stderr io.Writer, s *state) int {
|
|
|
|
groups := gatherResources(s)
|
|
|
|
for group, res := range groups {
|
|
|
|
|
|
|
|
_, err := io.WriteString(stdout, "["+group+"]\n")
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(stderr, "Error writing Invetory: %s\n", err)
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ress := range res {
|
|
|
|
|
|
|
|
_, err := io.WriteString(stdout, ress + "\n")
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(stderr, "Error writing Invetory: %s\n", err)
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = io.WriteString(stdout, "\n")
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(stderr, "Error writing Invetory: %s\n", err)
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2016-02-16 12:12:49 +01:00
|
|
|
|
2016-02-24 13:03:36 +01:00
|
|
|
return 0;
|
2015-02-06 22:31:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func cmdHost(stdout io.Writer, stderr io.Writer, s *state, hostname string) int {
|
2015-12-10 04:54:19 +01:00
|
|
|
for _, res := range s.resources() {
|
2015-12-10 05:34:25 +01:00
|
|
|
if hostname == res.Address() {
|
2015-06-05 04:43:56 +02:00
|
|
|
return output(stdout, stderr, res.Attributes())
|
2015-02-06 22:31:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintf(stderr, "No such host: %s\n", hostname)
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// output marshals an arbitrary JSON object and writes it to stdout, or writes
|
|
|
|
// an error to stderr, then returns the appropriate exit code.
|
|
|
|
func output(stdout io.Writer, stderr io.Writer, whatever interface{}) int {
|
|
|
|
b, err := json.Marshal(whatever)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(stderr, "Error encoding JSON: %s\n", err)
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = stdout.Write(b)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(stderr, "Error writing JSON: %s\n", err)
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|