-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
186 lines (158 loc) · 3.48 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"strings"
"sync"
"github.com/fatih/color"
"github.com/pkg/errors"
"github.com/rodaine/table"
"golang.org/x/exp/slices"
"gopkg.in/yaml.v2"
)
type Config struct {
Servers []Server
Aliases map[string]string
Columns []Column
}
type Server string
type Column struct {
Name string
Command string
Hide bool
Value string
}
func columnNames(columns []Column) []string {
result := []string{}
for _, column := range columns {
if !column.Hide {
result = append(result, column.Name)
}
}
return result
}
func columnValues(columns []Column) []string {
result := []string{}
for _, column := range columns {
if !column.Hide {
result = append(result, column.Value)
}
}
return result
}
func ReadConfig(filename string) (*Config, error) {
b, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
config := Config{}
err = yaml.Unmarshal(b, &config)
if err != nil {
return nil, err
}
return &config, nil
}
func toInterfaceSlice(s1 string, rest []string) []interface{} {
result := make([]interface{}, len(rest)+1)
result[0] = s1
for k, v := range rest {
result[k+1] = v
}
return result
}
func start() error {
config, err := ReadConfig("inspector.yml")
if err != nil {
return err
}
var (
serverWg sync.WaitGroup
serverMutex sync.Mutex
serverResults = make(map[string][]Column)
output = "table"
)
args := []string{}
copy(args, os.Args)
if slices.Contains(os.Args, "--json") {
output = "json"
args = make([]string, 0, len(os.Args)-1)
for _, v := range os.Args {
if v == "--json" {
continue
}
args = append(args, v)
}
}
if len(args) > 1 {
switch args[1] {
case "run":
commandArgs := args[2:]
if len(commandArgs) == 0 {
return errors.New("No command given")
}
commandString := strings.Join(commandArgs, " ")
config.Columns = []Column{
Column{
Name: "Output",
Command: commandString,
},
}
default:
if commandString, ok := config.Aliases[args[1]]; ok {
config.Columns = []Column{
Column{
Name: "Output",
Command: commandString,
},
}
break
}
return errors.New("Invalid parameter error, no such command or alias")
}
}
for _, server := range config.Servers {
serverWg.Add(1)
go func(serverName string) {
defer serverWg.Done()
columns := make([]Column, len(config.Columns))
copy(columns, config.Columns[:])
columns, err := sshRun(serverName, columns)
if err != nil {
fmt.Printf("Error for %s: %s\n", serverName, err)
}
// fmt.Printf("%s %#v\n", serverName, columns)
serverMutex.Lock()
defer serverMutex.Unlock()
serverResults[serverName] = columns
}(string(server))
}
serverWg.Wait()
switch output {
case "table":
headerFmt := color.New(color.FgGreen, color.Underline).SprintfFunc()
columnFmt := color.New(color.FgYellow).SprintfFunc()
tbl := table.New(toInterfaceSlice("Server", columnNames(config.Columns))...)
tbl.WithHeaderFormatter(headerFmt).WithFirstColumnFormatter(columnFmt)
for _, server := range config.Servers {
serverName := string(server)
if columns, ok := serverResults[serverName]; ok {
tbl.AddRow(toInterfaceSlice(serverName, columnValues(columns))...)
}
}
tbl.Print()
case "json":
b, err := json.MarshalIndent(serverResults, "", " ")
if err != nil {
return err
}
fmt.Println(string(b))
}
return nil
}
func main() {
if err := start(); err != nil {
fmt.Println(err)
}
}