@@ -4,23 +4,17 @@ import (
4
4
"context"
5
5
"fmt"
6
6
"log"
7
- "sort"
8
- "strings"
9
- "sync"
10
7
"time"
11
8
9
+ "github.com/clems4ever/go-graphkb/internal/database"
12
10
"github.com/clems4ever/go-graphkb/internal/knowledge"
13
11
"github.com/clems4ever/go-graphkb/internal/server"
14
- "github.com/clems4ever/go-graphkb/internal/sources"
15
12
"github.com/spf13/cobra"
16
13
"github.com/spf13/viper"
17
14
)
18
15
19
- // Sources repository of sources
20
- var Sources []sources.Source
21
-
22
16
// Database the selected database
23
- var Database knowledge. GraphDB
17
+ var Database * database. MariaDB
24
18
25
19
// ConfigPath string
26
20
var ConfigPath string
@@ -34,12 +28,6 @@ func main() {
34
28
Use : "go-graphkb [opts]" ,
35
29
}
36
30
37
- startCmd := & cobra.Command {
38
- Use : "start" ,
39
- Run : start ,
40
- Args : cobra .MaximumNArgs (1 ),
41
- }
42
-
43
31
listenCmd := & cobra.Command {
44
32
Use : "listen" ,
45
33
Run : listen ,
@@ -61,12 +49,6 @@ func main() {
61
49
Args : cobra .ExactArgs (1 ),
62
50
}
63
51
64
- sourceCmd := & cobra.Command {
65
- Use : "source [source]" ,
66
- Run : getSource ,
67
- Args : cobra .ExactArgs (1 ),
68
- }
69
-
70
52
queryCmd := & cobra.Command {
71
53
Use : "query [query]" ,
72
54
Run : queryFunc ,
@@ -77,7 +59,7 @@ func main() {
77
59
78
60
cobra .OnInitialize (onInit )
79
61
80
- rootCmd .AddCommand (startCmd , cleanCmd , sourceCmd , listenCmd , countCmd , readCmd , queryCmd )
62
+ rootCmd .AddCommand (cleanCmd , listenCmd , countCmd , readCmd , queryCmd )
81
63
if err := rootCmd .Execute (); err != nil {
82
64
log .Fatal (err )
83
65
}
@@ -97,75 +79,11 @@ func onInit() {
97
79
if dbName == "" {
98
80
log .Fatal ("Please provide database_name option in your configuration file" )
99
81
}
100
- Database = knowledge .NewMariaDB (
82
+ Database = database .NewMariaDB (
101
83
viper .GetString ("mariadb.username" ),
102
84
viper .GetString ("mariadb.password" ),
103
85
viper .GetString ("mariadb.host" ),
104
86
dbName )
105
-
106
- Sources = []sources.Source {
107
- sources .NewCSVSource (),
108
- }
109
-
110
- for _ , s := range Sources {
111
- sources .Registry .Add (s )
112
- g , err := s .Graph ()
113
-
114
- if err != nil {
115
- log .Fatal (err )
116
- }
117
-
118
- for _ , a := range g .Assets () {
119
- knowledge .SchemaRegistrySingleton .AddAssetType (a )
120
- }
121
-
122
- for _ , r := range g .Relations () {
123
- knowledge .SchemaRegistrySingleton .AddRelationType (r .Type )
124
- }
125
- }
126
- }
127
-
128
- func start (cmd * cobra.Command , args []string ) {
129
- var wg sync.WaitGroup
130
- wg .Add (1 )
131
-
132
- eventBus := make (chan knowledge.SourceSubGraphUpdates )
133
- listener := knowledge .NewSourceListener (Database )
134
-
135
- if err := Database .InitializeSchema (); err != nil {
136
- log .Fatal (err )
137
- }
138
-
139
- // Start kb listening
140
- go listener .Listen (eventBus )
141
-
142
- var selectedSources []sources.Source
143
-
144
- // if argument is provided, we select the source
145
- if len (args ) == 1 {
146
- for _ , s := range Sources {
147
- if s .Name () == args [0 ] {
148
- selectedSources = []sources.Source {s }
149
- break
150
- }
151
- }
152
- if len (selectedSources ) == 0 {
153
- log .Fatal (fmt .Sprintf ("Unable to find source with name %s" , args [0 ]))
154
- }
155
-
156
- } else {
157
- selectedSources = Sources
158
- }
159
-
160
- for _ , source := range selectedSources {
161
- emitter := knowledge .NewGraphEmitter (source .Name (), eventBus , Database )
162
- err := source .Start (emitter )
163
- if err != nil {
164
- fmt .Printf ("[ERROR] %s\n " , err )
165
- }
166
- }
167
-
168
- wg .Wait ()
169
87
}
170
88
171
89
func count (cmd * cobra.Command , args []string ) {
@@ -188,53 +106,8 @@ func flush(cmd *cobra.Command, args []string) {
188
106
fmt .Println ("Successul flush" )
189
107
}
190
108
191
- func getSource (cmd * cobra.Command , args []string ) {
192
- sourceName := args [0 ]
193
-
194
- selectedSources := []sources.Source {}
195
- for _ , s := range Sources {
196
- if s .Name () == sourceName {
197
- selectedSources = append (selectedSources , s )
198
- }
199
- }
200
-
201
- assets := make (map [string ]bool )
202
- relations := make (map [string ]bool )
203
- for _ , s := range selectedSources {
204
- g , err := s .Graph ()
205
- if err != nil {
206
- log .Fatal (err )
207
- }
208
-
209
- for _ , a := range g .Assets () {
210
- assets [string (a )] = true
211
- }
212
-
213
- for _ , r := range g .Relations () {
214
- t := fmt .Sprintf ("%s_%s_%s" , r .FromType , r .Type , r .ToType )
215
- relations [t ] = true
216
- }
217
- }
218
-
219
- assetsSlice := make ([]string , 0 )
220
- relationsSlice := make ([]string , 0 )
221
- for a := range assets {
222
- assetsSlice = append (assetsSlice , fmt .Sprintf ("\t %s" , a ))
223
- }
224
- for r := range relations {
225
- relationsSlice = append (relationsSlice , fmt .Sprintf ("\t %s" , r ))
226
- }
227
-
228
- sort .Strings (assetsSlice )
229
- sort .Strings (relationsSlice )
230
-
231
- fmt .Printf ("assets -> \n %s\n relations -> \n %s\n " ,
232
- strings .Join (assetsSlice , "\n " ),
233
- strings .Join (relationsSlice , "\n " ))
234
- }
235
-
236
109
func listen (cmd * cobra.Command , args []string ) {
237
- server .StartServer (Database )
110
+ server .StartServer (Database , Database )
238
111
}
239
112
240
113
func read (cmd * cobra.Command , args []string ) {
0 commit comments