Skip to content

Commit 46402f7

Browse files
committed
Create public interface by exporting internal types.
1 parent d54ae09 commit 46402f7

File tree

8 files changed

+99
-83
lines changed

8 files changed

+99
-83
lines changed

cmd/importer-csv/main.go

Lines changed: 71 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,83 @@
11
package main
22

33
import (
4+
"encoding/csv"
45
"fmt"
6+
"io"
57
"log"
8+
"os"
69

7-
"github.com/clems4ever/go-graphkb/importer"
8-
"github.com/clems4ever/go-graphkb/internal/sources"
10+
"github.com/clems4ever/go-graphkb/graphkb"
911
"github.com/spf13/cobra"
1012
"github.com/spf13/viper"
1113
)
1214

15+
type CSVSource struct {
16+
dataPath string
17+
}
18+
19+
func NewCSVSource() *CSVSource {
20+
csvSource := new(CSVSource)
21+
csvSource.dataPath = viper.GetString("path")
22+
23+
if csvSource.dataPath == "" {
24+
log.Fatal(fmt.Errorf("Unable to detect CSV file path in configuration. Check patch configuration is provided"))
25+
}
26+
27+
return csvSource
28+
}
29+
30+
func (cs *CSVSource) Start(importer *graphkb.GraphImporter) error {
31+
file, err := os.Open(cs.dataPath)
32+
if err != nil {
33+
return err
34+
}
35+
defer file.Close()
36+
37+
r := csv.NewReader(file)
38+
39+
previousGraph, err := importer.ReadCurrentGraph()
40+
if err != nil {
41+
return fmt.Errorf("Unable to read previous graph: %v", err)
42+
}
43+
44+
tx := importer.CreateTransaction(previousGraph)
45+
46+
header := true
47+
48+
for {
49+
record, err := r.Read()
50+
if err == io.EOF {
51+
break
52+
}
53+
if err != nil {
54+
return fmt.Errorf("Unable to read data in CSV file: %v", err)
55+
}
56+
57+
// Skip header line
58+
if header {
59+
header = false
60+
continue
61+
}
62+
63+
relationType := graphkb.RelationType{
64+
FromType: graphkb.AssetType(record[0]),
65+
ToType: graphkb.AssetType(record[3]),
66+
Type: graphkb.RelationKeyType(record[2]),
67+
}
68+
69+
tx.Relate(record[1], relationType, record[4])
70+
}
71+
72+
_, err = tx.Commit()
73+
fmt.Println("CSV data has been sent successfully")
74+
return err
75+
}
76+
77+
func (cs *CSVSource) Stop() error {
78+
return nil
79+
}
80+
1381
// ConfigPath string
1482
var ConfigPath string
1583

@@ -30,7 +98,7 @@ func main() {
3098
rootCmd := &cobra.Command{
3199
Use: "source-csv [opts]",
32100
Run: func(cmd *cobra.Command, args []string) {
33-
if err := importer.Start(sources.NewCSVSource(), nil); err != nil {
101+
if err := graphkb.Start(NewCSVSource(), nil); err != nil {
34102
log.Fatal(err)
35103
}
36104
},

graphkb/graph.go

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

graphkb/graph_api.go

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

graphkb/graph_importer.go

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

importer/importer.go renamed to graphkb/importer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package importer
1+
package graphkb
22

33
import (
44
"fmt"

graphkb/schema.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package graphkb
2+
3+
import "github.com/clems4ever/go-graphkb/internal/schema"
4+
5+
type AssetType = schema.AssetType
6+
type RelationKeyType = schema.RelationKeyType
7+
type RelationType = schema.RelationType

graphkb/tasks.go

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

internal/sources/csv_source.go

Lines changed: 0 additions & 79 deletions
This file was deleted.

0 commit comments

Comments
 (0)