Merge branch '8784-dir-listings'
[arvados.git] / sdk / go / config / load.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: Apache-2.0
4
5 package config
6
7 import (
8         "fmt"
9         "io/ioutil"
10
11         "github.com/ghodss/yaml"
12 )
13
14 // LoadFile loads configuration from the file given by configPath and
15 // decodes it into cfg.
16 //
17 // YAML and JSON formats are supported.
18 func LoadFile(cfg interface{}, configPath string) error {
19         buf, err := ioutil.ReadFile(configPath)
20         if err != nil {
21                 return err
22         }
23         err = yaml.Unmarshal(buf, cfg)
24         if err != nil {
25                 return fmt.Errorf("Error decoding config %q: %v", configPath, err)
26         }
27         return nil
28 }
29
30 // Dump returns a YAML representation of cfg.
31 func Dump(cfg interface{}) ([]byte, error) {
32         return yaml.Marshal(cfg)
33 }