-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
71 lines (60 loc) · 1.62 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
package main
import (
"context"
"errors"
"fmt"
spanner "github.com/nozo-moto/Ratchet/pkg"
"github.com/spf13/cobra"
)
var (
project string
instance string
database string
credentialsFile string
)
func main() {
rootCmd := &cobra.Command{
Use: "Ratchet",
}
queryPlanCmd := &cobra.Command{
Use: "queryplan",
Short: "Show Query Plan",
RunE: queryPlan,
}
rootCmd.AddCommand(queryPlanCmd)
rootCmd.PersistentFlags().StringVar(&project, "project", "", "GCP's Project ID ")
rootCmd.PersistentFlags().StringVar(&instance, "instance", "", "Spanner instance name")
rootCmd.PersistentFlags().StringVar(&database, "database", "", "Spanner database name")
rootCmd.PersistentFlags().StringVar(&credentialsFile, "credentials", "", "Credentials")
if err := rootCmd.Execute(); err != nil {
panic(err)
}
}
func newSpannerClient(ctx context.Context, c *cobra.Command) (*spanner.Client, error) {
config := &spanner.Config{
Project: c.Flag("project").Value.String(),
Instance: c.Flag("instance").Value.String(),
Database: c.Flag("database").Value.String(),
CredentialsFile: c.Flag("credentials").Value.String(),
}
return spanner.NewClient(ctx, config)
}
func queryPlan(c *cobra.Command, args []string) error {
ctx := context.Background()
client, err := newSpannerClient(ctx, c)
if err != nil {
return err
}
if len(args) < 1 {
return errors.New("Input query")
}
fmt.Println("Run QueryPlan")
fmt.Println("Query: ", args[0])
fmt.Println("----------")
result, err := client.GetQueryPlan(ctx, args[0])
if err != nil {
return err
}
fmt.Println(result)
return err
}