-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
70 lines (56 loc) · 1.54 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
package main
import (
"encoding/json"
"flag"
"fmt"
"os"
"path/filepath"
"strings"
"text/tabwriter"
"github.com/saracen/kubeql/query"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
"k8s.io/client-go/tools/clientcmd"
)
func main() {
var kubeconfig *string
if home := homeDir(); home != "" {
kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")
} else {
kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
}
var execute = flag.String("execute", "", "query to execute")
flag.Parse()
// use the current context in kubeconfig
config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
if err != nil {
panic(err.Error())
}
results, err := query.ExecuteQuery(config, *execute)
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
writer := new(tabwriter.Writer)
writer.Init(os.Stdout, 0, 8, 1, ' ', 0)
fmt.Fprintln(writer, strings.Join(results.Headers, "\t"))
var underscore []string
for _, header := range results.Headers {
underscore = append(underscore, strings.Repeat("-", len(header)))
}
fmt.Fprintln(writer, strings.Join(underscore, "\t"))
for _, row := range results.Rows {
c := make([]string, len(row.Columns))
for i, column := range row.Columns {
pretty, _ := json.Marshal(column)
c[i] = string(pretty)
}
fmt.Fprintln(writer, strings.Join(c, "\t"))
}
writer.Flush()
}
func homeDir() string {
if h := os.Getenv("HOME"); h != "" {
return h
}
return os.Getenv("USERPROFILE") // windows
}