pkiadm/config.go
Gibheer 039f72c3d5 initial commit
The basic server and client are working and it is possible to add, list,
show, set and remove subjects.

Locations are not yet written to the filesystem yet and need to be
fixed.
2017-05-28 11:33:04 +02:00

47 lines
867 B
Go

package pkiadm
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
)
var configLookupPath = []string{
"config.json",
"pkiadm.json",
"/etc/pkiadm.json",
}
type (
Config struct {
Path string // path to the unix socket
Storage string // path to the storage location
}
)
func LoadConfig() (*Config, error) {
for _, path := range configLookupPath {
if _, err := os.Stat(path); os.IsNotExist(err) {
continue
}
return tryToLoadConfig(path)
}
return nil, fmt.Errorf("no config file found")
}
// tryToLoadConfig loads the config and tries to parse the file. When this
// doesn't work out, the error is returned.
func tryToLoadConfig(path string) (*Config, error) {
var cfg *Config
raw, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
if err := json.Unmarshal(raw, &cfg); err != nil {
return nil, err
}
return cfg, nil
}