-
Notifications
You must be signed in to change notification settings - Fork 11
/
configuration.go
69 lines (57 loc) · 1.41 KB
/
configuration.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
package main
import (
"fmt"
"os"
"strings"
"github.com/spf13/viper"
)
// Configurations
type Configurations struct {
Server struct {
Host string
Port int
SSL bool
User string
Password string
Connections int
}
Groups string
ParallelScans int
Step int
Days int
Path string
Verbose bool
}
var conf Configurations
func loadConfig() error {
// Set the file name of the configurations file
viper.SetConfigName("config")
// Set the path to look for the configurations file
viper.AddConfigPath(".")
// Set config type to yaml
viper.SetConfigType("yaml")
if err := viper.ReadInConfig(); err != nil {
if strings.Contains(err.Error(), "Not Found") {
fmt.Printf("Config file \"config.yml\" not found. Creating config file...\n")
defaultConfig := []byte(defaultConfig())
if err := os.WriteFile("./config.yml", defaultConfig, 0644); err != nil {
fmt.Printf("Error creating configuration file: %s\n", err)
return err
} else {
fmt.Printf("Config file \"config.yml\" created. Please edit default values.\n")
os.Exit(0)
}
} else {
fmt.Printf("Error reading configuration file: %s\n", err)
return err
}
}
if err := viper.Unmarshal(&conf); err != nil {
fmt.Printf("Unable to decode configure structure, %v\n", err)
return err
}
if verbose {
fmt.Println("Configuration loaded")
}
return nil
}