-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
70 lines (60 loc) · 2.25 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
package main
import (
"fmt"
"os"
"strconv"
"strings"
"time"
"github.com/dadosjusbr/status"
)
const (
defaultGeneralTimeout = 5 * time.Minute // Duração máxima total da coleta de todos os arquivos. Valor padrão calculado a partir de uma média de execuções ~4.5min
defaulTimeBetweenSteps = 4 * time.Second //Tempo de espera entre passos do coletor."
)
func main() {
if _, err := strconv.Atoi(os.Getenv("MONTH")); err != nil {
status.ExitFromError(status.NewError(status.InvalidInput, fmt.Errorf("invalid month (\"%s\"): %w", os.Getenv("MONTH"), err)))
}
month := os.Getenv("MONTH")
if _, err := strconv.Atoi(os.Getenv("YEAR")); err != nil {
status.ExitFromError(status.NewError(status.InvalidInput, fmt.Errorf("invalid year (\"%s\"): %w", os.Getenv("YEAR"), err)))
}
year := os.Getenv("YEAR")
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)))
}
generalTimeout := defaultGeneralTimeout
if os.Getenv("GENERAL_TIMEOUT") != "" {
var err error
generalTimeout, err = time.ParseDuration(os.Getenv("GENERAL_TIMEOUT"))
if err != nil {
status.ExitFromError(status.NewError(status.InvalidInput, fmt.Errorf("invalid GENERAL_TIMEOUT (\"%s\"): %w", os.Getenv("GENERAL_TIMEOUT"), err)))
}
}
timeBetweenSteps := defaulTimeBetweenSteps
if os.Getenv("TIME_BETWEEN_STEPS") != "" {
var err error
timeBetweenSteps, err = time.ParseDuration(os.Getenv("TIME_BETWEEN_STEPS"))
if err != nil {
status.ExitFromError(status.NewError(status.InvalidInput, fmt.Errorf("invalid TIME_BETWEEN_STEPS (\"%s\"): %w", os.Getenv("TIME_BETWEEN_STEPS"), err)))
}
}
c := crawler{
collectionTimeout: generalTimeout,
timeBetweenSteps: timeBetweenSteps,
year: year,
month: month,
output: outputFolder,
}
downloads, err := c.crawl()
if err != nil {
status.ExitFromError(status.NewError(status.OutputError, err))
}
// O parser do MPAM espera os arquivos separados por \n. Mudanças aqui tem
// refletir as expectativas lá.
fmt.Println(strings.Join(downloads, "\n"))
}