-
Notifications
You must be signed in to change notification settings - Fork 4
/
config.go
84 lines (71 loc) · 1.64 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
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
package config
import (
"fmt"
"github.com/mitchellh/go-homedir"
"github.com/spf13/viper"
"os"
"path/filepath"
"strings"
)
// Config is the root collection of command configurations
type Config struct {
Server ServerConfig
Base64 Base64Config
Random RandomConfig
UUID UUIDConfig
Hash HashConfig
JWT JWTConfig
}
// ServerConfig allows configuration settings of the server cmd
type ServerConfig struct {
Address string
Port string
OpenBrowser bool
}
// Base64Config allows configuration settings of the base64 cmd
type Base64Config struct {
Encoding string
}
// RandomConfig allows configuration settings of the random cmd
type RandomConfig struct {
Algorithm string
Length int
}
// UUIDConfig allows configuration settings of the uuid cmd
type UUIDConfig struct {
Namespace string
Version int
}
// HashConfig allows configuration settings of the hash cmd
type HashConfig struct {
Algorithm string
Cost int
}
// JWTConfig allows configuration settings of the jwt cmd
type JWTConfig struct {
Secret string
SecretFile string
Base64Secret bool
}
// Cfg the root object of the configuration
var CfgFile string
var Cfg *Config
// InitConfig initializes the configuration if provided.
func InitConfig() {
home, err := homedir.Dir()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
if CfgFile == "" {
viper.SetConfigName(".dt")
viper.AddConfigPath(home)
} else {
base := filepath.Base(strings.TrimSuffix(CfgFile, filepath.Ext(CfgFile)))
viper.SetConfigName(base)
dir := filepath.Dir(CfgFile)
viper.AddConfigPath(strings.Replace(dir, "~", home, 1))
}
viper.ReadInConfig()
viper.Unmarshal(&Cfg)
}