Skip to content

Commit

Permalink
Create public interface by exporting internal types.
Browse files Browse the repository at this point in the history
And also pass options in Start instead of relying on viper in the
implementation.
  • Loading branch information
clems4ever committed Jan 9, 2020
1 parent 46402f7 commit 5bd3749
Show file tree
Hide file tree
Showing 14 changed files with 348 additions and 187 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@
*.tar.gz
go-graphkb
importer-csv

.config.yml
9 changes: 8 additions & 1 deletion cmd/importer-csv/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,14 @@ func main() {
rootCmd := &cobra.Command{
Use: "source-csv [opts]",
Run: func(cmd *cobra.Command, args []string) {
if err := graphkb.Start(NewCSVSource(), nil); err != nil {

options := graphkb.ImporterOptions{
URL: viper.GetString("graphkb.url"),
AuthToken: viper.GetString("graphkb.auth_token"),
SkipVerify: viper.GetBool("graphkb.skip_verify"),
}

if err := graphkb.Start(NewCSVSource(), options); err != nil {
log.Fatal(err)
}
},
Expand Down
2 changes: 2 additions & 0 deletions graphkb/graph_importer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ package graphkb
import "github.com/clems4ever/go-graphkb/internal/knowledge"

type GraphImporter = knowledge.GraphImporter

type Transaction = knowledge.Transaction
30 changes: 21 additions & 9 deletions graphkb/importer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,28 @@ package graphkb

import (
"fmt"

"github.com/clems4ever/go-graphkb/internal/knowledge"
"github.com/clems4ever/go-graphkb/internal/schema"
"github.com/clems4ever/go-graphkb/internal/sources"
"github.com/spf13/viper"
)

type ImporterOptions struct {
CacheGraph bool
URL string
AuthToken string
SkipVerify bool
}

func Start(source sources.Source, options *ImporterOptions) error {
url := viper.GetString("graphkb.url")
if url == "" {
func Start(source sources.Source, options ImporterOptions) error {
if options.URL == "" {
return fmt.Errorf("Please provide graphkb URL in configuration file")
}

authToken := viper.GetString("graphkb.auth_token")
if authToken == "" {
if options.AuthToken == "" {
return fmt.Errorf("Please provide a graphkb auth token to communicate with GraphKB")
}

observableSource := sources.NewObservableSource(source)
api := knowledge.NewGraphAPI(url, authToken)
api := knowledge.NewGraphAPI(options.URL, options.AuthToken, options.SkipVerify)
graphImporter := knowledge.NewGraphImporter(api)

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

return nil
}

func CreateRelation(fromType schema.AssetType, relation, toType schema.AssetType) RelationType {
return schema.RelationType{
FromType: fromType,
Type: RelationKeyType(relation),
ToType: toType,
}
}

func CreateAsset(fromType string) AssetType {
return schema.AssetType(fromType)
}
2 changes: 2 additions & 0 deletions graphkb/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ package graphkb
import "github.com/clems4ever/go-graphkb/internal/utils"

type RecurrentTask = utils.RecurrentTask

var NewRecurrentTask = utils.NewRecurrentTask
Loading

0 comments on commit 5bd3749

Please sign in to comment.