-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
82 lines (67 loc) · 2.08 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package main
import (
"fmt"
"os"
"strconv"
dadojusbr "github.com/dadosjusbr/proto"
"github.com/dadosjusbr/proto/coleta"
"github.com/dadosjusbr/status"
"google.golang.org/protobuf/encoding/prototext"
"google.golang.org/protobuf/types/known/timestamppb"
)
const (
agenciaID = "mppb"
repColetor = "https://github.com/dadosjusbr/coletor-mppb"
)
func main() {
month, err := strconv.Atoi(os.Getenv("MONTH"))
if err != nil {
status.ExitFromError(status.NewError(status.InvalidInput, fmt.Errorf("Invalid month (\"%s\"): %w", os.Getenv("MONTH"), err)))
}
year, err := strconv.Atoi(os.Getenv("YEAR"))
if err != nil {
status.ExitFromError(status.NewError(status.InvalidInput, fmt.Errorf("Invalid year (\"%s\"): %w", os.Getenv("YEAR"), err)))
}
outputFolder := os.Getenv("OUTPUT_FOLDER")
if outputFolder == "" {
outputFolder = "./output"
}
if err := os.Mkdir(outputFolder, os.ModePerm); err != nil && !os.IsExist(err) {
status.ExitFromError(status.NewError(status.SystemError, fmt.Errorf("Error creating output folder(%s): %w", outputFolder, err)))
}
crawlerVersion := os.Getenv("CRAWLER_VERSION")
if crawlerVersion == "" {
crawlerVersion = "unspecified"
}
files, err := Crawl(outputFolder, month, year)
if err != nil {
status.ExitFromError(err)
}
chaveColeta := dadojusbr.IDColeta(agenciaID, month, year)
folha, parseErr := Parse(files, chaveColeta)
if parseErr != nil {
status.ExitFromError(parseErr)
}
colRes := coleta.Coleta{
ChaveColeta: chaveColeta,
Orgao: agenciaID,
Mes: int32(month),
Ano: int32(year),
TimestampColeta: timestamppb.Now(),
RepositorioColetor: repColetor,
VersaoColetor: crawlerVersion,
DirColetor: agenciaID,
Arquivos: files,
}
metadados := Metadados(int32(year), int32(month))
rc := coleta.ResultadoColeta{
Coleta: &colRes,
Folha: folha,
Metadados: &metadados,
}
b, err := prototext.Marshal(&rc)
if err != nil {
status.ExitFromError(status.NewError(status.OutputError, fmt.Errorf("JSON marshaling error: %w", err)))
}
fmt.Printf("%s", b)
}