158 lines
2.9 KiB
Go
158 lines
2.9 KiB
Go
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"bytes"
|
|
"encoding/json"
|
|
"errors"
|
|
"flag"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
|
|
"github.com/BurntSushi/toml"
|
|
)
|
|
|
|
var (
|
|
config Setting
|
|
configPath string
|
|
filePath string
|
|
)
|
|
|
|
func init() {
|
|
flag.StringVar(&configPath, "config-path", "./settings/settings.toml", "Path to config file")
|
|
flag.StringVar(&filePath, "file", "test.csv", "Path to file with some cis")
|
|
}
|
|
|
|
// ParseFile ...
|
|
func ParseFile(fileName string) ([]string, error) {
|
|
if fileName == "" {
|
|
return nil, errors.New("Empty file path")
|
|
}
|
|
file, err := os.Open(fileName)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer file.Close()
|
|
|
|
scanner := bufio.NewScanner(file)
|
|
|
|
var lines []string
|
|
|
|
for scanner.Scan() {
|
|
lines = append(lines, scanner.Text())
|
|
}
|
|
return lines, nil
|
|
}
|
|
|
|
// SplitSlice ...
|
|
func SplitSlice(lines []string) error {
|
|
l := len(lines)
|
|
max := 20
|
|
if l >= max {
|
|
|
|
filesNum := l / max
|
|
if l%max > 0 {
|
|
filesNum++
|
|
}
|
|
|
|
files := make([]string, filesNum)
|
|
|
|
start := 0
|
|
end := max
|
|
for i := 1; i <= filesNum; i++ {
|
|
if start+max <= filesNum {
|
|
end = start + max
|
|
} else {
|
|
end = start + l%max
|
|
}
|
|
fName := fmt.Sprintf("./out/part_%d.csv", i)
|
|
if err := MakePartFile(lines[start:end], fName); err != nil {
|
|
return err
|
|
}
|
|
files = append(files, fName)
|
|
}
|
|
for _, file := range files {
|
|
SendToMicroservice(file)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// SendToMicroservice ...
|
|
func SendToMicroservice(fileName string) error {
|
|
lines, err := ParseFile(fileName)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
products := make([]Cis, 0)
|
|
for _, line := range lines {
|
|
if line != "" {
|
|
cis := NewCis(line)
|
|
products = append(products, cis)
|
|
}
|
|
}
|
|
document := NewDocument(fileName, products)
|
|
|
|
data, err := json.Marshal(document)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
req, err := http.NewRequest("POST", config.MicroServiceURL, bytes.NewBuffer(data))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
req.Header.Set("Content-Type", "application/json")
|
|
req.Header.Set("token", config.Token)
|
|
|
|
client := http.Client{}
|
|
resp, err := client.Do(req)
|
|
fmt.Printf("Make request to %s\n with data %s\n", config.MicroServiceURL, string(data))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer resp.Body.Close()
|
|
fmt.Println("response Status:", resp.Status)
|
|
fmt.Println("response Headers:", resp.Header)
|
|
body, _ := ioutil.ReadAll(resp.Body)
|
|
fmt.Println(string(body))
|
|
return nil
|
|
}
|
|
|
|
// MakePartFile ...
|
|
func MakePartFile(lines []string, fName string) error {
|
|
file, err := os.OpenFile(fName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0755)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer file.Close()
|
|
|
|
for _, line := range lines {
|
|
if _, err := file.WriteString(fmt.Sprintln(line[0:31])); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func main() {
|
|
flag.Parse()
|
|
|
|
_, err := toml.DecodeFile(configPath, &config)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
lines, err := ParseFile(filePath)
|
|
if err != nil {
|
|
fmt.Println(err.Error())
|
|
}
|
|
if err = SplitSlice(lines); err != nil {
|
|
fmt.Println(err.Error())
|
|
}
|
|
}
|