This repository was archived by the owner on Feb 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbloomsource.go
100 lines (92 loc) · 2.06 KB
/
bloomsource.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
package dataloading
import (
"fmt"
"os"
"io/ioutil"
"github.com/spf13/viper"
"gopkg.in/yaml.v2"
)
func showUsage() {
fmt.Printf("Usage: %s <command>\n", os.Args[0])
fmt.Println("=============================\n")
fmt.Println("Avaialable commands:")
fmt.Printf("%s bootstrap # setup datasource in BloomAPI\n", os.Args[0])
fmt.Printf("%s fetch # fetch latest data and add to BloomAPI\n", os.Args[0])
fmt.Printf("%s drop # remove all tables\n", os.Args[0])
fmt.Printf("%s search-index # index in elasticsearch\n", os.Args[0])
fmt.Printf("%s search-drop # remove elasticsearch types\n", os.Args[0])
fmt.Printf("%s schema # fetch latest data and scan schema\n", os.Args[0])
}
func CreateCmd(desc Description) {
if (len(os.Args) != 2) {
fmt.Println("Invalid command usage\n")
showUsage()
os.Exit(1)
}
arg := os.Args[1]
viper.SetConfigName("config")
viper.SetConfigType("toml")
viper.AddConfigPath("./")
configPath := os.Getenv("BLOOM_CONFIG")
if configPath != "" {
viper.AddConfigPath(configPath)
}
err := viper.ReadInConfig()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
switch arg {
case "bootstrap":
err := Bootstrap()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
case "fetch":
err := Fetch(desc)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
case "drop":
err := Drop()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
case "search-index":
err := Index()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
case "search-drop":
err := IndexDrop()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
case "schema":
schema, err := Schema(desc)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
mapping := SchemaToMapping(schema)
marshaled, err := yaml.Marshal(&mapping)
if err != nil {
fmt.Println("Failed to marshal schema", err)
os.Exit(1)
}
err = ioutil.WriteFile("dbmapping.yaml", marshaled, 0644)
if err != nil {
fmt.Println("Failed to write schema", err)
os.Exit(1)
}
default:
fmt.Println("Invalid command:", arg)
showUsage()
os.Exit(1)
}
}