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