-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
143 lines (128 loc) · 3.26 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
package main
import (
"encoding/json"
"fmt"
"os"
"context"
"log/slog"
_ "github.com/marcboeker/go-duckdb"
"github.com/urfave/cli/v3"
"github.com/zawachte/kubsto/internal/querier"
"github.com/zawachte/kubsto/internal/runner"
"github.com/zawachte/kubsto/pkg/kubeclient"
)
func main() {
kubeconfig := ""
databaseLocation := ""
outputFormat := ""
cmd := &cli.Command{
Commands: []*cli.Command{
{
Name: "load",
Aliases: []string{"l"},
Usage: "load metadata from kubernetes cluster",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "kubeconfig",
Value: "",
Usage: "kubeconfig file",
Destination: &kubeconfig,
},
&cli.StringFlag{
Name: "data-dir",
Value: "data",
Usage: "database location",
Destination: &databaseLocation,
},
},
Action: func(ctx context.Context, cmd *cli.Command) error {
logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelInfo}))
cs, err := kubeclient.CreateClientSet(kubeconfig)
if err != nil {
return err
}
rr, err := runner.NewRunner(runner.RunnerParams{
Logger: logger,
ClientSet: cs,
DatabaseLocation: databaseLocation,
})
if err != nil {
return err
}
err = rr.Run(context.Background())
if err != nil {
return err
}
return nil
},
},
{
Name: "query",
Aliases: []string{"q"},
Usage: "query duckdb database",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "data-dir",
Value: "data",
Usage: "database location",
Destination: &databaseLocation,
Aliases: []string{"d"},
},
&cli.StringFlag{
Name: "output-format",
Value: "text",
Usage: "output format",
Destination: &outputFormat,
Aliases: []string{"o"},
},
},
Action: func(ctx context.Context, cmd *cli.Command) error {
logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelInfo}))
if cmd.NArg() == 0 {
return fmt.Errorf("no query string provided")
}
queryString := cmd.Args().Get(0)
querierInstance, err := querier.NewQuerier(querier.QuerierParams{
Logger: logger,
DatabaseLocation: databaseLocation,
})
if err != nil {
return err
}
results, err := querierInstance.Query(context.Background(), queryString)
if err != nil {
return err
}
return printResults(results, outputFormat)
},
},
},
}
if err := cmd.Run(context.Background(), os.Args); err != nil {
fmt.Println("error:", err)
os.Exit(1)
}
}
func printResults(results []map[string]string, outputFormat string) error {
switch outputFormat {
case "json":
// marshal results to json
marshaledResults, err := json.Marshal(results)
if err != nil {
return err
}
fmt.Println(string(marshaledResults))
return nil
case "text":
// print results as text
for _, result := range results {
for key, value := range result {
fmt.Printf("%s: %s\n", key, value)
}
fmt.Println("----------")
}
return nil
default:
return fmt.Errorf("unknown output format: %s", outputFormat)
}
}