github.com/Cloud-Foundations/Dominator@v0.3.4/cmd/domtool/getInfoForSubs.go (about) 1 package main 2 3 import ( 4 "bufio" 5 "fmt" 6 "os" 7 "path/filepath" 8 9 "github.com/Cloud-Foundations/Dominator/lib/errors" 10 "github.com/Cloud-Foundations/Dominator/lib/fsutil" 11 "github.com/Cloud-Foundations/Dominator/lib/json" 12 "github.com/Cloud-Foundations/Dominator/lib/log" 13 "github.com/Cloud-Foundations/Dominator/lib/srpc" 14 "github.com/Cloud-Foundations/Dominator/proto/dominator" 15 ) 16 17 func getInfoForSubsSubcommand(args []string, logger log.DebugLogger) error { 18 if err := getInfoForSubs(getClient()); err != nil { 19 return fmt.Errorf("error getting info for subs: %s", err) 20 } 21 return nil 22 } 23 24 func getInfoForSubs(client *srpc.Client) error { 25 hostnames, err := getSubsFromFile() 26 if err != nil { 27 return err 28 } 29 request := dominator.GetInfoForSubsRequest{ 30 Hostnames: hostnames, 31 LocationsToMatch: locationsToMatch, 32 StatusesToMatch: statusesToMatch, 33 TagsToMatch: tagsToMatch, 34 } 35 var reply dominator.GetInfoForSubsResponse 36 if err := client.RequestReply("Dominator.GetInfoForSubs", request, 37 &reply); err != nil { 38 return err 39 } 40 if err := errors.New(reply.Error); err != nil { 41 return err 42 } 43 json.WriteWithIndent(os.Stdout, " ", reply.Subs) 44 return nil 45 } 46 47 func getSubsFromFile() ([]string, error) { 48 if *subsList == "" { 49 return nil, nil 50 } 51 file, err := os.Open(*subsList) 52 if err != nil { 53 return nil, err 54 } 55 defer file.Close() 56 reader := bufio.NewReader(file) 57 if filepath.Ext(*subsList) != ".json" { 58 return fsutil.ReadLines(reader) 59 } 60 var data interface{} 61 if err := json.Read(reader, &data); err != nil { 62 return nil, err 63 } 64 entries, ok := data.([]interface{}) 65 if !ok { 66 return nil, errors.New("non slice data") 67 } 68 if len(entries) < 1 { 69 return nil, nil 70 } 71 hostnames := make([]string, 0, len(entries)) 72 for _, entry := range entries { 73 if hostname, ok := entry.(string); ok { 74 hostnames = append(hostnames, hostname) 75 } else if mapEntry, ok := entry.(map[string]interface{}); !ok { 76 return nil, fmt.Errorf("unsupported entry type: %T, value: %v", 77 entry, entry) 78 } else { 79 if value, ok := mapEntry["Hostname"]; !ok { 80 return nil, fmt.Errorf("map entry missing Hostname: %v", 81 entry) 82 } else if hostname, ok := value.(string); !ok { 83 return nil, fmt.Errorf("Hostname not a string: %v", 84 value) 85 } else { 86 hostnames = append(hostnames, hostname) 87 } 88 } 89 } 90 return hostnames, nil 91 }