/
main.go
46 lines (42 loc) · 1.31 KB
/
main.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
46
package main
import (
"bytes"
"encoding/json"
"io"
"os"
"github.com/egeneralov/ansible-dynamic-inventory-nspawn/internal/types"
"go.uber.org/zap"
)
func main() {
defer func() { _ = logger.Sync() }()
task, err := cfg.execTask()
if err != nil {
logger.Fatal("failed to create exec task", zap.Error(err))
}
logger.Info("executing", zap.String("command", task.Command), zap.Strings("args", task.Args))
taskResult, err := task.Execute()
if err != nil {
logger.Fatal("failed to execute task", zap.Error(err))
}
var machinesList types.List
if err := json.Unmarshal([]byte(taskResult.Stdout), &machinesList); err != nil {
logger.Fatal("failed to parse command stdout into types.list structure", zap.Error(err))
}
for _, el := range machinesList {
logger.Debug("founded machine", zap.String("name", el.Machine), zap.String("address", el.Addresses))
}
if cfg.List {
jr, je := json.Marshal(machinesList.ToGroups(cfg.Bastion))
if je != nil {
logger.Fatal("failed to marshal", zap.Error(je))
}
//fmt.Println(string(jr))
if written, err := io.Copy(os.Stdout, bytes.NewBuffer(jr)); err != nil {
logger.Fatal("failed to write stdout answer", zap.Int64("len", written), zap.Error(err))
}
} else if cfg.Host != "" {
logger.Fatal("not implemented")
} else {
logger.Fatal("no default action available")
}
}