-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
91 lines (75 loc) · 1.88 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
package main
import (
"bytes"
"context"
"fmt"
"os"
"strings"
"text/tabwriter"
"github.com/rs/zerolog/log"
"github.com/loivis/marvel-comics-api-data-loader/client/marvel"
"github.com/loivis/marvel-comics-api-data-loader/mongodb"
"github.com/loivis/marvel-comics-api-data-loader/process"
)
func main() {
conf := readConfig()
fmt.Fprintln(os.Stderr, conf)
ctx := context.Background()
marvelClient := marvel.NewClient("https://gateway.marvel.com/v1/public/", conf.privateKey, conf.publicKey)
mongodb, err := mongodb.New(conf.mongodbURI, conf.mongodbDatabase)
if err != nil {
log.Fatal().Msgf("failed to setup mongodb: %v", err)
}
p := process.NewProcessor(marvelClient, mongodb, conf.privateKey, conf.publicKey)
if err := p.Process(ctx); err != nil {
log.Fatal().Msg(err.Error())
}
}
type config struct {
mongodbURI string
mongodbDatabase string
privateKey string
publicKey string
}
func readConfig() *config {
return &config{
mongodbURI: os.Getenv("MONGODB_URI"),
mongodbDatabase: os.Getenv("MONGODB_DATABASE"),
privateKey: os.Getenv("MARVEL_API_PRIVATE_KEY"),
publicKey: os.Getenv("MARVEL_API_PUBLIC_KEY"),
}
}
func (c *config) String() string {
hideIfSet := func(v interface{}) string {
s := ""
switch typedV := v.(type) {
case string:
s = typedV
case []string:
s = strings.Join(typedV, ",")
case fmt.Stringer:
if typedV != nil {
s = typedV.String()
}
}
if s != "" {
return "<hidden>"
}
return ""
}
var buf bytes.Buffer
w := tabwriter.NewWriter(&buf, 0, 1, 4, ' ', 0)
for _, e := range []struct {
k string
v interface{}
}{
{"MONGODB_URI", hideIfSet(c.mongodbURI)},
{"MONGODB_DATABASE", c.mongodbDatabase},
{"MARVEL_API_PRIVATE_KEY", hideIfSet(c.privateKey)},
{"MARVEL_API_PUBLIC_KEY", c.publicKey},
} {
fmt.Fprintf(w, "%s\t%v\n", e.k, e.v)
}
w.Flush()
return buf.String()
}