-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
57 lines (47 loc) · 1.3 KB
/
config.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
package main
import (
// "fmt"
"github.com/decred/dcrd/dcrutil/v2"
flags "github.com/jessevdk/go-flags"
"log"
"os"
"path/filepath"
"strings"
)
const defaultConfigFilename = "devstats.conf"
var (
defaultDevstatsDir = dcrutil.AppDataDir("devstats", false)
defaultConfigFile = filepath.Join(defaultDevstatsDir, defaultConfigFilename)
)
type config struct {
ConfigFile string `short:"C" long:"configfile" description:"Path to config file"`
GithubToken string `long:"token" description:"Your github token"`
Users []string `long:"user" description:"Map of all repos from that user"`
}
func loadConfig() (*config, error) {
defaultConfig := config{
ConfigFile: defaultConfigFile,
}
preCfg := defaultConfig
if _, err := flags.Parse(&preCfg); err != nil {
return nil, err
}
appName := filepath.Base(os.Args[0])
appName = strings.TrimSuffix(appName, filepath.Ext(appName))
// usageMessage := fmt.Sprintf("Use %s -h to show usage", appName)
var configFileError error
cfg := preCfg
if err := flags.IniParse(defaultDevstatsDir, &cfg); err != nil {
if _, ok := err.(*flags.IniError); ok {
return nil, err
}
configFileError = err
}
if _, err := flags.Parse(&cfg); err != nil {
return nil, err
}
if configFileError != nil {
log.Printf("%v", configFileError)
}
return &cfg, nil
}