-
Notifications
You must be signed in to change notification settings - Fork 1
/
opts.go
118 lines (100 loc) · 2.42 KB
/
opts.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package cmd
import (
"errors"
"strconv"
"strings"
"github.com/gnames/bhlindex/internal/config"
"github.com/gnames/gnfmt"
"github.com/gnames/gnsys"
"github.com/rs/zerolog/log"
"github.com/spf13/viper"
)
// getOpts imports data from the configuration file. Some of the settings can
// be overriden by command line flags.
func getOpts(cfgPath string) {
cfg := &cfgData{}
err := viper.Unmarshal(cfg)
if err != nil {
log.Fatal().Err(err).Msg("Cannot deserialize config data")
}
if cfg.BHLdir != "" {
opts = append(opts, config.OptBHLdir(cfg.BHLdir))
} else {
log.Fatal().Err(errors.New("cannot find BHL data")).Msgf(
"Tell bhlindex where to find BHL data by setting up BHLdir in %s",
cfgPath,
)
}
if cfg.OutputFormat != "" {
f := getFormat(cfg.OutputFormat)
opts = append(opts, config.OptOutputFormat(f))
}
if cfg.OutputDir != "" {
d, err := getDir(cfg.OutputDir)
if err != nil {
log.Fatal().Err(err).Msg("Cannot set OutputDir from config file")
}
opts = append(opts, config.OptOutputDir(d))
}
if cfg.PgHost != "" {
opts = append(opts, config.OptPgHost(cfg.PgHost))
}
if cfg.PgUser != "" {
opts = append(opts, config.OptPgUser(cfg.PgUser))
}
if cfg.PgPass != "" {
opts = append(opts, config.OptPgPass(cfg.PgPass))
}
if cfg.PgDatabase != "" {
opts = append(opts, config.OptPgDatabase(cfg.PgDatabase))
}
if cfg.Jobs > 0 {
opts = append(opts, config.OptJobs(cfg.Jobs))
}
if cfg.VerifierURL != "" {
opts = append(opts, config.OptVerifierURL(cfg.VerifierURL))
}
if cfg.WithoutConfirm {
opts = append(opts, config.OptWithoutConfirm(true))
}
}
func getFormat(f string) gnfmt.Format {
res := gnfmt.CSV
switch f {
case "csv":
res = gnfmt.CSV
case "tsv":
res = gnfmt.TSV
case "json":
res = gnfmt.CompactJSON
default:
log.Warn().Msgf("Format '%s' is not supported, using default 'csv' format",
f)
log.Warn().Msg("Supported formats are 'csv', 'tsv', 'json'")
}
return res
}
func getDir(dir string) (string, error) {
exists, _, _ := gnsys.DirExists(dir)
if exists {
return dir, nil
}
log.Warn().Msgf("Dir '%s' does not exist, creating...", dir)
err := gnsys.MakeDir(dir)
if err != nil {
return "", err
}
return dir, nil
}
func getSources(s string) ([]int, error) {
var res []int
ss := strings.Split(s, ",")
for _, v := range ss {
i, err := strconv.Atoi(strings.TrimSpace(v))
if err != nil {
return res, err
}
res = append(res, i)
}
return res, nil
}