| 1 |
|
| 2 |
|
| 3 |
package config |
| 4 |
|
| 5 |
import ( |
| 6 |
"encoding/json" |
| 7 |
"io/ioutil" |
| 8 |
) |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
type Server struct { |
| 13 |
|
| 14 |
|
| 15 |
Addr string `json:"address"` |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
CAPath string `json:"ca_path,omitempty"` |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
KeyPaths string `json:"private_keys"` |
| 25 |
CertPaths string `json:"certificates"` |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
Systemd bool `json:"use_systemd,omitempty"` |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
type UI struct { |
| 34 |
|
| 35 |
Root string `json:"root"` |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
Static string `json:"static"` |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
type HipChat struct { |
| 44 |
Host string `json:"host"` |
| 45 |
Room string `json:"room"` |
| 46 |
APIKey string `json:"api_key"` |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
func (hc *HipChat) Valid() bool { |
| 52 |
if hc.APIKey == "" { |
| 53 |
return false |
| 54 |
} |
| 55 |
|
| 56 |
if hc.Room == "" { |
| 57 |
return false |
| 58 |
} |
| 59 |
|
| 60 |
if hc.Host == "" { |
| 61 |
return false |
| 62 |
} |
| 63 |
|
| 64 |
return true |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
type Metrics struct { |
| 70 |
Host string `json:"host"` |
| 71 |
Port string `json:"port"` |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
type Delegations struct { |
| 76 |
|
| 77 |
Persist bool `json:"persist"` |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
Policy string `json:"policy"` |
| 83 |
Users []string `json:"users"` |
| 84 |
|
| 85 |
|
| 86 |
Mechanism string `json:"mechanism"` |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
Location string `json:"location"` |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
type Config struct { |
| 97 |
Server *Server `json:"server"` |
| 98 |
UI *UI `json:"ui"` |
| 99 |
HipChat *HipChat `json:"hipchat"` |
| 100 |
Metrics *Metrics `json:"metrics"` |
| 101 |
Delegations *Delegations `json:"delegations"` |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
func (c *Config) Valid() bool { |
| 107 |
|
| 108 |
if len(c.Server.CertPaths) == 0 || len(c.Server.KeyPaths) == 0 { |
| 109 |
return false |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
if c.Server.Addr == "" && !c.Server.Systemd { |
| 114 |
return false |
| 115 |
} |
| 116 |
|
| 117 |
return true |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
func New() *Config { |
| 122 |
return &Config{ |
| 123 |
Server: &Server{}, |
| 124 |
UI: &UI{}, |
| 125 |
HipChat: &HipChat{}, |
| 126 |
Metrics: &Metrics{}, |
| 127 |
Delegations: &Delegations{}, |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
func Load(path string) (*Config, error) { |
| 133 |
cfg := New() |
| 134 |
in, err := ioutil.ReadFile(path) |
| 135 |
if err != nil { |
| 136 |
return nil, err |
| 137 |
} |
| 138 |
|
| 139 |
err = json.Unmarshal(in, cfg) |
| 140 |
if err != nil { |
| 141 |
return nil, err |
| 142 |
} |
| 143 |
|
| 144 |
return cfg, nil |
| 145 |
} |