-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
103 lines (85 loc) · 2.42 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package main
import (
"flag"
"fmt"
"os"
"time"
"github.com/airenas/go-app/pkg/goapp"
"github.com/airenas/tts-line/internal/pkg/exporter"
"github.com/airenas/tts-line/internal/pkg/mongodb"
"github.com/mattn/go-colorable"
"github.com/labstack/gommon/color"
"github.com/pkg/errors"
)
type params struct {
delete bool
to time.Time
}
func main() {
os.Setenv("LOGGER_OUT_NAME", "stderr")
fs := flag.CommandLine
ap := ¶ms{}
takeParams(fs, ap)
goapp.StartWithFlags(fs, os.Args)
goapp.Log.Info("Starting")
printBanner()
defer goapp.Estimate("Export")()
sp, err := mongodb.NewSessionProvider(goapp.Config.GetString("mongo.url"))
if err != nil {
goapp.Log.Fatal(errors.Wrap(err, "can't init mongo session provider"))
}
defer sp.Close()
ts, err := mongodb.NewTextSaver(sp)
if err != nil {
goapp.Log.Fatal(errors.Wrap(err, "can't init DB exporter"))
}
p := exporter.Params{To: ap.to, Delete: ap.delete, Out: os.Stdout, Exporter: ts}
err = exporter.Export(p)
if err != nil {
goapp.Log.Fatal(errors.Wrap(err, "can't start the service"))
}
goapp.Log.Info("Finished")
}
var (
version string
)
func printBanner() {
banner := `
_________________ __
/_ __/_ __/ ___/ ___ _ ______ ____ _____/ /_
/ / / / \__ \ / _ \| |/_/ __ \/ __ \/ ___/ __/
/ / / / ___/ / / __/> </ /_/ / /_/ / / / /_
/_/ /_/ /____/ \___/_/|_/ .___/\____/_/ \__/ v: %s
/_/
%s
________________________________________________________
`
cl := color.New()
cl.SetOutput(colorable.NewColorableStderr())
cl.Printf(banner, cl.Red(version), cl.Green("https://github.com/airenas/tts-line"))
}
func takeParams(fs *flag.FlagSet, data *params) {
fs.Usage = func() {
fmt.Fprintf(fs.Output(), "Usage of %s: <params> > out.json\n", os.Args[0])
fs.PrintDefaults()
}
fs.Var(timeValue{to: &data.to}, "to", "Filters records according to the time provided here. Takes older record than the time. Format 'YYYY-MM-DD'")
fs.BoolVar(&data.delete, "delete", false, "Deletes filtered records from database")
}
type timeValue struct {
to *time.Time
}
func (v timeValue) String() string {
if v.to != nil && !v.to.IsZero() {
return v.to.Format("2006-01-02")
}
return ""
}
func (v timeValue) Set(s string) error {
t, err := time.Parse("2006-01-02", s)
if err != nil {
return err
}
*v.to = t
return nil
}