Skip to content

Commit 5bd3749

Browse files
committed
Create public interface by exporting internal types.
And also pass options in Start instead of relying on viper in the implementation.
1 parent 46402f7 commit 5bd3749

File tree

14 files changed

+348
-187
lines changed

14 files changed

+348
-187
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,5 @@
2020
*.tar.gz
2121
go-graphkb
2222
importer-csv
23+
24+
.config.yml

cmd/importer-csv/main.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,14 @@ func main() {
9898
rootCmd := &cobra.Command{
9999
Use: "source-csv [opts]",
100100
Run: func(cmd *cobra.Command, args []string) {
101-
if err := graphkb.Start(NewCSVSource(), nil); err != nil {
101+
102+
options := graphkb.ImporterOptions{
103+
URL: viper.GetString("graphkb.url"),
104+
AuthToken: viper.GetString("graphkb.auth_token"),
105+
SkipVerify: viper.GetBool("graphkb.skip_verify"),
106+
}
107+
108+
if err := graphkb.Start(NewCSVSource(), options); err != nil {
102109
log.Fatal(err)
103110
}
104111
},

graphkb/graph_importer.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ package graphkb
33
import "github.com/clems4ever/go-graphkb/internal/knowledge"
44

55
type GraphImporter = knowledge.GraphImporter
6+
7+
type Transaction = knowledge.Transaction

graphkb/importer.go

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,28 @@ package graphkb
22

33
import (
44
"fmt"
5+
56
"github.com/clems4ever/go-graphkb/internal/knowledge"
7+
"github.com/clems4ever/go-graphkb/internal/schema"
68
"github.com/clems4ever/go-graphkb/internal/sources"
7-
"github.com/spf13/viper"
89
)
910

1011
type ImporterOptions struct {
11-
CacheGraph bool
12+
URL string
13+
AuthToken string
14+
SkipVerify bool
1215
}
1316

14-
func Start(source sources.Source, options *ImporterOptions) error {
15-
url := viper.GetString("graphkb.url")
16-
if url == "" {
17+
func Start(source sources.Source, options ImporterOptions) error {
18+
if options.URL == "" {
1719
return fmt.Errorf("Please provide graphkb URL in configuration file")
1820
}
19-
20-
authToken := viper.GetString("graphkb.auth_token")
21-
if authToken == "" {
21+
if options.AuthToken == "" {
2222
return fmt.Errorf("Please provide a graphkb auth token to communicate with GraphKB")
2323
}
2424

2525
observableSource := sources.NewObservableSource(source)
26-
api := knowledge.NewGraphAPI(url, authToken)
26+
api := knowledge.NewGraphAPI(options.URL, options.AuthToken, options.SkipVerify)
2727
graphImporter := knowledge.NewGraphImporter(api)
2828

2929
if err := observableSource.Start(graphImporter); err != nil {
@@ -32,3 +32,15 @@ func Start(source sources.Source, options *ImporterOptions) error {
3232

3333
return nil
3434
}
35+
36+
func CreateRelation(fromType schema.AssetType, relation, toType schema.AssetType) RelationType {
37+
return schema.RelationType{
38+
FromType: fromType,
39+
Type: RelationKeyType(relation),
40+
ToType: toType,
41+
}
42+
}
43+
44+
func CreateAsset(fromType string) AssetType {
45+
return schema.AssetType(fromType)
46+
}

graphkb/tasks.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ package graphkb
33
import "github.com/clems4ever/go-graphkb/internal/utils"
44

55
type RecurrentTask = utils.RecurrentTask
6+
7+
var NewRecurrentTask = utils.NewRecurrentTask

0 commit comments

Comments
 (0)