Refactoring and setting clearer filenames in utils/
This commit is contained in:
parent
0d082374e9
commit
8ac9d5786b
22
parser/main.go
Normal file
22
parser/main.go
Normal file
|
@ -0,0 +1,22 @@
|
|||
package parser
|
||||
|
||||
// read node recieved on kafka
|
||||
// create a node in the node file
|
||||
// save domains in another file with node ID
|
||||
|
||||
// parallel routine
|
||||
// look throught domain names and if find some that exists already, create the
|
||||
// edge file
|
||||
|
||||
const (
|
||||
NodesFilename = "nodes.json"
|
||||
EdgesFilename = "edges.json"
|
||||
)
|
||||
|
||||
func ParseEvent(domains []string) {
|
||||
|
||||
// saveDomains()
|
||||
// go findDomainEdges()
|
||||
}
|
||||
|
||||
// Helpers
|
|
@ -1,15 +1,11 @@
|
|||
package utils
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
"github.com/jmoiron/jsonq"
|
||||
"github.com/sirupsen/logrus"
|
||||
"gitlab.dcso.lolcat/LABS/styx/models"
|
||||
"gitlab.dcso.lolcat/LABS/styx/parser"
|
||||
)
|
||||
|
||||
// ExtractCertFromStream builds the structures before saving them. It uses the
|
||||
|
@ -191,45 +187,3 @@ func extractLeafCertStruct(input jsonq.JsonQuery) (models.LeafCertStruct, error)
|
|||
}, nil
|
||||
|
||||
}
|
||||
|
||||
func SaveNode(node *models.Node) {
|
||||
err := FileExists(parser.NodesFilename)
|
||||
if err != nil {
|
||||
logrus.Error(err)
|
||||
}
|
||||
nodeFile, err := ioutil.ReadFile(parser.NodesFilename)
|
||||
if err != nil {
|
||||
logrus.Error(err)
|
||||
}
|
||||
nodeDatas := []models.Node{}
|
||||
|
||||
if err := json.Unmarshal(nodeFile, &nodeDatas); err != nil {
|
||||
logrus.Error(err)
|
||||
}
|
||||
|
||||
nodeDatas = append(nodeDatas, *node)
|
||||
|
||||
nodeBytes, err := json.Marshal(nodeDatas)
|
||||
if err != nil {
|
||||
logrus.Error(err)
|
||||
}
|
||||
|
||||
err = ioutil.WriteFile(parser.NodesFilename, nodeBytes, 0644)
|
||||
if err != nil {
|
||||
logrus.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func SaveDomains(domains []string) {
|
||||
f, err := os.OpenFile("domains.txt", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
||||
if err != nil {
|
||||
logrus.Error(err)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
for _, d := range domains {
|
||||
if _, err := f.WriteString(d + ","); err != nil {
|
||||
logrus.Error(err)
|
||||
}
|
||||
}
|
||||
}
|
15
utils/files.go
Normal file
15
utils/files.go
Normal file
|
@ -0,0 +1,15 @@
|
|||
package utils
|
||||
|
||||
import "os"
|
||||
|
||||
func FileExists(filename string) error {
|
||||
_, err := os.Stat(filename)
|
||||
if os.IsNotExist(err) {
|
||||
_, err := os.Create(filename)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
53
utils/saves.go
Normal file
53
utils/saves.go
Normal file
|
@ -0,0 +1,53 @@
|
|||
package utils
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
"gitlab.dcso.lolcat/LABS/styx/models"
|
||||
"gitlab.dcso.lolcat/LABS/styx/parser"
|
||||
)
|
||||
|
||||
func SaveNode(node *models.Node) {
|
||||
err := FileExists(parser.NodesFilename)
|
||||
if err != nil {
|
||||
logrus.Error(err)
|
||||
}
|
||||
nodeFile, err := ioutil.ReadFile(parser.NodesFilename)
|
||||
if err != nil {
|
||||
logrus.Error(err)
|
||||
}
|
||||
nodeDatas := []models.Node{}
|
||||
|
||||
if err := json.Unmarshal(nodeFile, &nodeDatas); err != nil {
|
||||
logrus.Error(err)
|
||||
}
|
||||
|
||||
nodeDatas = append(nodeDatas, *node)
|
||||
|
||||
nodeBytes, err := json.Marshal(nodeDatas)
|
||||
if err != nil {
|
||||
logrus.Error(err)
|
||||
}
|
||||
|
||||
err = ioutil.WriteFile(parser.NodesFilename, nodeBytes, 0644)
|
||||
if err != nil {
|
||||
logrus.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func SaveDomains(domains []string) {
|
||||
f, err := os.OpenFile("domains.txt", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
||||
if err != nil {
|
||||
logrus.Error(err)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
for _, d := range domains {
|
||||
if _, err := f.WriteString(d + ","); err != nil {
|
||||
logrus.Error(err)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue