2015-02-09 04:38:37 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2015-11-26 12:41:42 +00:00
|
|
|
yaml "github.com/cloudfoundry-incubator/candiedyaml"
|
2015-10-12 11:50:17 +00:00
|
|
|
"github.com/rancher/os/util"
|
2015-02-09 04:38:37 +00:00
|
|
|
)
|
|
|
|
|
2016-05-31 21:34:04 +00:00
|
|
|
func Merge(bytes []byte) error {
|
2016-06-02 01:41:55 +00:00
|
|
|
data, err := readConfigs(bytes, false, true)
|
2016-05-31 21:34:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-02-09 04:38:37 +00:00
|
|
|
}
|
2016-06-02 01:41:55 +00:00
|
|
|
existing, err := readConfigs(nil, false, true, CloudConfigFile)
|
2016-05-31 21:34:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-12-17 15:34:26 +00:00
|
|
|
}
|
2016-05-31 21:34:04 +00:00
|
|
|
return WriteToFile(util.Merge(existing, data), CloudConfigFile)
|
2015-02-09 04:38:37 +00:00
|
|
|
}
|
|
|
|
|
2016-05-31 21:34:04 +00:00
|
|
|
func Export(private, full bool) (string, error) {
|
2016-12-01 00:41:58 +00:00
|
|
|
rawCfg := loadRawConfig("", full)
|
2016-06-01 01:10:56 +00:00
|
|
|
if !private {
|
2016-05-31 21:34:04 +00:00
|
|
|
rawCfg = filterPrivateKeys(rawCfg)
|
2015-03-15 04:27:04 +00:00
|
|
|
}
|
2015-02-09 04:38:37 +00:00
|
|
|
|
2016-05-31 21:34:04 +00:00
|
|
|
bytes, err := yaml.Marshal(rawCfg)
|
2015-03-15 04:27:04 +00:00
|
|
|
return string(bytes), err
|
2015-02-09 04:38:37 +00:00
|
|
|
}
|
|
|
|
|
2016-05-31 21:34:04 +00:00
|
|
|
func Get(key string) (interface{}, error) {
|
2016-06-02 01:41:55 +00:00
|
|
|
cfg := LoadConfig()
|
2015-07-29 06:52:15 +00:00
|
|
|
|
2016-04-01 04:31:46 +00:00
|
|
|
data := map[interface{}]interface{}{}
|
2016-05-31 21:34:04 +00:00
|
|
|
if err := util.ConvertIgnoreOmitEmpty(cfg, &data); err != nil {
|
2016-04-01 04:31:46 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
v, _ := getOrSetVal(key, data, nil)
|
|
|
|
return v, nil
|
|
|
|
}
|
|
|
|
|
2016-08-12 18:05:16 +00:00
|
|
|
func GetCmdline(key string) interface{} {
|
|
|
|
cmdline := readCmdline()
|
|
|
|
v, _ := getOrSetVal(key, cmdline, nil)
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2016-05-31 21:34:04 +00:00
|
|
|
func Set(key string, value interface{}) error {
|
2016-06-02 01:41:55 +00:00
|
|
|
existing, err := readConfigs(nil, false, true, CloudConfigFile)
|
2015-03-15 04:27:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-02-04 16:13:20 +00:00
|
|
|
|
2016-06-02 01:41:55 +00:00
|
|
|
_, modified := getOrSetVal(key, existing, value)
|
2016-06-16 17:14:52 +00:00
|
|
|
|
|
|
|
c := &CloudConfig{}
|
|
|
|
if err = util.Convert(modified, c); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-06-02 01:41:55 +00:00
|
|
|
return WriteToFile(modified, CloudConfigFile)
|
2015-04-16 05:57:59 +00:00
|
|
|
}
|