(filename string, opener func(filename string) (jsonconfig.File, error))
| 505 | } |
| 506 | |
| 507 | func load(filename string, opener func(filename string) (jsonconfig.File, error)) (*Config, error) { |
| 508 | c := osutil.NewJSONConfigParser() |
| 509 | c.Open = opener |
| 510 | m, err := c.ReadFile(filename) |
| 511 | if err != nil { |
| 512 | return nil, err |
| 513 | } |
| 514 | obj := jsonconfig.Obj(m) |
| 515 | conf := &Config{ |
| 516 | jconf: obj, |
| 517 | } |
| 518 | |
| 519 | if lowLevel := obj.OptionalBool("handlerConfig", false); lowLevel { |
| 520 | if err := conf.readFields(); err != nil { |
| 521 | return nil, err |
| 522 | } |
| 523 | return conf, nil |
| 524 | } |
| 525 | |
| 526 | // Check whether the high-level config uses the old names. |
| 527 | if err := detectConfigChange(obj); err != nil { |
| 528 | return nil, err |
| 529 | } |
| 530 | |
| 531 | // Because the original high-level config might have expanded |
| 532 | // through the use of functions, we re-encode the map back to |
| 533 | // JSON here so we can unmarshal it into the hiLevelConf |
| 534 | // struct later. |
| 535 | highExpandedJSON, err := json.Marshal(m) |
| 536 | if err != nil { |
| 537 | return nil, fmt.Errorf("Can't re-marshal high-level JSON config: %v", err) |
| 538 | } |
| 539 | |
| 540 | var hiLevelConf serverconfig.Config |
| 541 | if err := json.Unmarshal(highExpandedJSON, &hiLevelConf); err != nil { |
| 542 | return nil, fmt.Errorf("Could not unmarshal into a serverconfig.Config: %v", err) |
| 543 | } |
| 544 | |
| 545 | // At this point, conf.jconf.UnknownKeys() contains all the names found in |
| 546 | // the given high-level configuration. We check them against |
| 547 | // highLevelConfFields(), which gives us all the possible valid |
| 548 | // configuration names, to catch typos or invalid names. |
| 549 | allFields := highLevelConfFields() |
| 550 | for _, v := range conf.jconf.UnknownKeys() { |
| 551 | if _, ok := allFields[v]; !ok { |
| 552 | return nil, fmt.Errorf("unknown high-level configuration parameter: %q in file %q", v, filename) |
| 553 | } |
| 554 | } |
| 555 | conf, err = genLowLevelConfig(&hiLevelConf) |
| 556 | if err != nil { |
| 557 | return nil, fmt.Errorf( |
| 558 | "failed to transform user config file into internal handler configuration: %v", |
| 559 | err) |
| 560 | } |
| 561 | if v, _ := strconv.ParseBool(os.Getenv("CAMLI_DEBUG_CONFIG")); v { |
| 562 | jsconf, _ := json.MarshalIndent(conf.jconf, "", " ") |
| 563 | log.Printf("From high-level config, generated low-level config: %s", jsconf) |
| 564 | } |
no test coverage detected