-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
74 lines (66 loc) · 1.37 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
// Copyright 2023 The Fileganizer Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package main
import (
"fileganizer/config"
"fileganizer/grok"
"fileganizer/logger"
"fileganizer/output"
"fileganizer/textextract"
"fmt"
"os"
"os/exec"
"strings"
_ "embed"
"go.uber.org/zap"
)
var (
Version string = strings.TrimSpace(version)
//go:embed version.txt
version string
)
func main() {
l := logger.Get()
cfg, err := config.New(Version)
if err != nil {
os.Exit(1)
}
txt, err := textextract.TextExtract(cfg.InputFile, cfg.ExtractTextCommand)
if err != nil {
os.Exit(1)
}
if cfg.TextOutput {
fmt.Printf("%v\n", txt)
os.Exit(0)
}
g := grok.New(cfg.GrokPatterns)
o := output.New(cfg.CommonTemplate, cfg.Months)
for _, fd := range cfg.FileDescriptions {
r, err := g.ParseAll(fd.Patterns, txt)
if err != nil {
os.Exit(1)
}
if r == nil {
continue
}
values := map[string]any{
"env": cfg.EnvVars,
"grok": r,
"filename": cfg.InputFile,
}
outputResult, err := o.FromTemplate(fd.Output, values)
if err != nil {
continue
}
if cfg.NoDryRun {
run, err := exec.Command("bash", "-c", outputResult).Output()
fmt.Printf("%s", string(run))
if err != nil {
l.Fatal("Run output command", zap.String("command output", string(run)), zap.Error(err))
os.Exit(1)
}
} else {
fmt.Printf("%s", outputResult)
}
}
}