data_funcs.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package config
  2. import (
  3. "github.com/rancher/os/log"
  4. "github.com/rancher/os/util"
  5. )
  6. type CfgFunc func(*CloudConfig) (*CloudConfig, error)
  7. type CfgFuncData struct {
  8. Name string
  9. Func CfgFunc
  10. }
  11. type CfgFuncs []CfgFuncData
  12. func ChainCfgFuncs(cfg *CloudConfig, cfgFuncs CfgFuncs) (*CloudConfig, error) {
  13. len := len(cfgFuncs)
  14. for c, d := range cfgFuncs {
  15. i := c + 1
  16. name := d.Name
  17. cfgFunc := d.Func
  18. if cfg == nil {
  19. log.Infof("[%d/%d] Starting %s WITH NIL cfg", i, len, name)
  20. } else {
  21. log.Infof("[%d/%d] Starting %s", i, len, name)
  22. }
  23. var err error
  24. if cfg, err = cfgFunc(cfg); err != nil {
  25. log.Errorf("Failed [%d/%d] %s: %s", i, len, name, err)
  26. return cfg, err
  27. }
  28. log.Debugf("[%d/%d] Done %s", i, len, name)
  29. }
  30. return cfg, nil
  31. }
  32. func filterKey(data map[interface{}]interface{}, key []string) (filtered, rest map[interface{}]interface{}) {
  33. if len(key) == 0 {
  34. return data, map[interface{}]interface{}{}
  35. }
  36. filtered = map[interface{}]interface{}{}
  37. rest = util.MapCopy(data)
  38. k := key[0]
  39. if d, ok := data[k]; ok {
  40. switch d := d.(type) {
  41. case map[interface{}]interface{}:
  42. f, r := filterKey(d, key[1:])
  43. if len(f) != 0 {
  44. filtered[k] = f
  45. }
  46. if len(r) != 0 {
  47. rest[k] = r
  48. } else {
  49. delete(rest, k)
  50. }
  51. default:
  52. filtered[k] = d
  53. delete(rest, k)
  54. }
  55. }
  56. return
  57. }