-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
56 lines (46 loc) · 1.08 KB
/
main.go
File metadata and controls
56 lines (46 loc) · 1.08 KB
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
package main
import (
"flag"
"fmt"
"os"
"strings"
"github.com/innogames/serveradmin-go-client/adminapi"
)
// adminapi CLI entry point
func main() {
var attributes string
var orderBy string
var onlyOne bool
flag.StringVar(&attributes, "a", "hostname", "Attributes to fetch")
flag.StringVar(&orderBy, "order", "", "Attributes to order by the result")
flag.BoolVar(&onlyOne, "one", false, "Make sure exactly one server matches with the query")
flag.Parse()
query := flag.Arg(0)
if query == "" {
flag.PrintDefaults()
os.Exit(1)
}
q, err := adminapi.FromQuery(query)
if err != nil {
fmt.Println("Error parsing query:", err)
os.Exit(1)
}
attributeList := strings.Split(attributes, ",")
q.SetAttributes(attributeList...)
q.OrderBy(orderBy)
servers, err := q.All()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
if onlyOne && len(servers) != 1 {
fmt.Println("expected exactly one server object, got", len(servers))
os.Exit(1)
}
for _, server := range servers {
for _, arg := range attributeList {
fmt.Printf("%v ", server.Get(arg))
}
fmt.Print("\n")
}
}