github.com/cloud-foundations/dominator@v0.0.0-20221004181915-6e4fee580046/fleetmanager/topology/list.go (about) 1 package topology 2 3 import ( 4 "fmt" 5 "os" 6 "strings" 7 8 proto "github.com/Cloud-Foundations/Dominator/proto/fleetmanager" 9 ) 10 11 func splitPath(path string) []string { 12 return strings.Split(path, string(os.PathSeparator)) 13 } 14 15 func (t *Topology) findDirectory(dirname string) (*Directory, error) { 16 directory := t.Root 17 if dirname == "" { 18 return directory, nil 19 } 20 for list := splitPath(dirname); len(list) > 0; { 21 if subdir, ok := directory.nameToDirectory[list[0]]; !ok { 22 return nil, fmt.Errorf("directory: %s not found", dirname) 23 } else { 24 directory = subdir 25 list = list[1:] 26 } 27 } 28 return directory, nil 29 } 30 31 func (t *Topology) listMachines(dirname string) ([]*proto.Machine, error) { 32 directory, err := t.findDirectory(dirname) 33 if err != nil { 34 return nil, err 35 } 36 return directory.listMachines(), nil 37 } 38 39 func (directory *Directory) listMachines() []*proto.Machine { 40 machines := directory.Machines 41 for _, subdir := range directory.Directories { 42 machines = append(machines, subdir.listMachines()...) 43 } 44 return machines 45 }