2015-02-06 22:31:59 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
|
|
|
func cmdList(stdout io.Writer, stderr io.Writer, s *state) int {
|
|
|
|
groups := make(map[string][]string, 0)
|
|
|
|
|
2015-12-10 04:54:19 +01:00
|
|
|
// Add each instance name as a pseudo-group, so they can be provisioned
|
2015-02-06 22:31:59 +01:00
|
|
|
// individually where necessary.
|
2015-12-10 04:54:19 +01:00
|
|
|
for _, res := range s.resources() {
|
|
|
|
_, ok := groups[res.Name]
|
|
|
|
if !ok {
|
|
|
|
groups[res.Name] = []string{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the instance by name. There can be many instances with the same name,
|
|
|
|
// created using the count parameter.
|
|
|
|
groups[res.Name] = append(groups[res.Name], res.Address())
|
|
|
|
|
2015-12-10 05:33:40 +01:00
|
|
|
// Add the instance by its full name, including the counter.
|
2015-12-10 04:54:19 +01:00
|
|
|
groups[res.NameWithCounter()] = []string{res.Address()}
|
2015-02-06 22:31:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return output(stdout, stderr, groups)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|