github.com/GGP1/kure@v0.8.4/config/config.go (about) 1 package config 2 3 import ( 4 "os" 5 "path/filepath" 6 "time" 7 8 "github.com/awnumar/memguard" 9 "github.com/pkg/errors" 10 "github.com/spf13/cast" 11 ) 12 13 // Init intializes the configuration. 14 func Init() error { 15 configPath, err := getConfigPath() 16 if err != nil { 17 return err 18 } 19 20 if err := Load(configPath); err != nil { 21 return err 22 } 23 24 if IsSet("auth") { 25 return errors.New("found invalid key: \"auth\"") 26 } 27 28 return nil 29 } 30 31 func getConfigPath() (string, error) { 32 configPath := os.Getenv("KURE_CONFIG") 33 if configPath != "" { 34 ext := filepath.Ext(configPath) 35 if ext == "" || ext == "." { 36 return "", errors.New("\"KURE_CONFIG\" environment variable path must have an extension") 37 } 38 39 return configPath, nil 40 } 41 42 homeDir, err := os.UserHomeDir() 43 if err != nil { 44 return "", errors.Wrap(err, "finding home directory") 45 } 46 47 homeDir = filepath.Join(homeDir, ".kure") 48 configPath = filepath.Join(homeDir, "kure.yaml") 49 50 if _, err := os.Stat(configPath); err != nil { 51 if !errors.Is(err, os.ErrNotExist) { 52 return "", err 53 } 54 if err := createDefaultConfigFile(homeDir, configPath); err != nil { 55 return "", err 56 } 57 } 58 59 return configPath, nil 60 } 61 62 func createDefaultConfigFile(homeDir, configPath string) error { 63 if err := os.MkdirAll(homeDir, 0o700); err != nil { 64 return errors.Wrap(err, "creating the directory") 65 } 66 SetDefaults(filepath.Join(homeDir, "kure.db")) 67 return Write(configPath, true) 68 } 69 70 // Filename returns the name of the file that the configuration is using. 71 func Filename() string { 72 return config.filename 73 } 74 75 // Get returns an uncasted value from the config map. 76 func Get(key string) interface{} { 77 return config.Get(key) 78 } 79 80 // GetEnclave returns an enclave from the config map. 81 func GetEnclave(key string) *memguard.Enclave { 82 v := config.Get(key) 83 if v == nil { 84 return nil 85 } 86 87 return v.(*memguard.Enclave) 88 } 89 90 // GetDuration returns a duration from the config map. 91 func GetDuration(key string) time.Duration { 92 return cast.ToDuration(config.Get(key)) 93 } 94 95 // GetString returns a string from the config map. 96 func GetString(key string) string { 97 return cast.ToString(config.Get(key)) 98 } 99 100 // GetStringMapString returns a map[string]string from the config map. 101 func GetStringMapString(key string) map[string]string { 102 return cast.ToStringMapString(config.Get(key)) 103 } 104 105 // GetUint32 returns an uint32 from the config map. 106 func GetUint32(key string) uint32 { 107 return cast.ToUint32(config.Get(key)) 108 } 109 110 // IsSet returns if the key exists in the config map or not. 111 func IsSet(key string) bool { 112 return config.Get(key) != nil 113 } 114 115 // Load reads the configuration file and populates the map. 116 func Load(configPath string) error { 117 return config.Load(configPath) 118 } 119 120 // Reset sets config to its initial value. 121 func Reset() { 122 config = New() 123 } 124 125 // Set sets a value to the config map. 126 func Set(key string, value interface{}) { 127 config.Set(key, value) 128 } 129 130 // SetDefaults populates the config map with the default values. 131 func SetDefaults(dbPath string) { 132 defaults := map[string]interface{}{ 133 "clipboard.timeout": "0s", 134 "database.path": dbPath, 135 "editor": "vim", 136 "keyfile.path": "", 137 "session.prefix": "kure:~ $", 138 "session.scripts": map[string]string{}, 139 "session.timeout": "0s", 140 } 141 142 for k, v := range defaults { 143 Set(k, v) 144 } 145 } 146 147 // SetFilename sets the configuration filename. 148 func SetFilename(filename string) { 149 config.filename = filename 150 } 151 152 // Write encodes and writes the config map to the specified file. 153 // 154 // If exclusive is true an error will be returned if the file 155 // already exists, otherwise it will truncate the file and write to it. 156 func Write(filename string, exclusive bool) error { 157 flags := os.O_CREATE | os.O_WRONLY | os.O_TRUNC 158 if exclusive { 159 flags = os.O_CREATE | os.O_WRONLY | os.O_EXCL 160 } 161 162 return config.Write(filename, flags) 163 } 164 165 // WriteStruct writes the configuration empty structure to the given file. 166 func WriteStruct(filename string) error { 167 temp := config.mp 168 config.mp = map[string]interface{}{ 169 "clipboard": map[string]interface{}{ 170 "timeout": "", 171 }, 172 "database": map[string]interface{}{ 173 "path": "", 174 }, 175 "editor": "", 176 "keyfile": map[string]interface{}{ 177 "path": "", 178 }, 179 "session": map[string]interface{}{ 180 "prefix": "", 181 "scripts": map[string]string{}, 182 "timeout": "", 183 }, 184 } 185 186 if err := Write(filename, true); err != nil { 187 return err 188 } 189 190 // Return the map to its previous state 191 config.mp = temp 192 193 return nil 194 }