-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmachine_list.go
44 lines (39 loc) · 955 Bytes
/
machine_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
package rove
import (
"encoding/json"
"fmt"
"github.com/evantbyrne/trance"
)
type MachineListCommand struct {
ConfigFile string `flag:"" name:"config" help:"Config file." type:"path" default:".rove"`
Json bool `flag:"" name:"json" help:"Output as JSON."`
}
type MachineListJson struct {
Machines []*Machine `json:"machines"`
}
func (cmd *MachineListCommand) Run() error {
return Database(cmd.ConfigFile, func() error {
return trance.Query[Machine]().
Sort("name").
All().
Then(func(machines []*Machine) error {
if cmd.Json {
to := MachineListJson{
Machines: machines,
}
out, err := json.MarshalIndent(to, "", " ")
if err != nil {
fmt.Println("🚫 Could not format JSON:\n", to)
return err
}
fmt.Println(string(out))
} else {
for _, machine := range machines {
fmt.Println(machine.Name, machine.Address)
}
}
return nil
}).
Error
})
}