Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alpkeskin
GitHub Repository: alpkeskin/mosint
Path: blob/master/v3/internal/config/config.go
689 views
1
/*
2
Copyright © 2023 github.com/alpkeskin
3
*/
4
package config
5
6
import (
7
"fmt"
8
"os"
9
"strings"
10
11
"gopkg.in/yaml.v3"
12
)
13
14
type Config struct {
15
Services struct {
16
BreachDirectoryApiKey string `yaml:"breach_directory_api_key"`
17
EmailRepApiKey string `yaml:"emailrep_api_key"`
18
HunterApiKey string `yaml:"hunter_api_key"`
19
IntelXApiKey string `yaml:"intelx_api_key"`
20
HaveIBeenPwnedApiKey string `yaml:"haveibeenpwned_api_key"`
21
}
22
Settings struct {
23
IntelXMaxResults int `yaml:"intelx_max_results"`
24
}
25
}
26
27
var (
28
Cfg *Config
29
)
30
31
func New() *Config {
32
return &Config{}
33
}
34
35
func (c *Config) Parse(cfgFile string) error {
36
path := getConfigPath(cfgFile)
37
data, err := os.ReadFile(path)
38
39
if err != nil {
40
return err
41
}
42
43
var cFile Config
44
err = yaml.Unmarshal(data, &cFile)
45
46
if err != nil {
47
return err
48
}
49
50
Cfg = &cFile
51
52
return nil
53
}
54
55
func (c *Config) Exists(cfgFile string) bool {
56
path := getConfigPath(cfgFile)
57
58
_, err := os.Stat(path)
59
60
if os.IsNotExist(err) {
61
return false
62
}
63
64
return true
65
}
66
67
func getConfigPath(cfgFile string) string {
68
69
if !strings.EqualFold(cfgFile, "") {
70
return cfgFile
71
}
72
73
homeDir, err := os.UserHomeDir()
74
75
if err != nil {
76
panic(err)
77
}
78
79
return fmt.Sprintf("%s/.mosint.yaml", homeDir)
80
}
81
82