github.com/polydawn/docket@v0.5.4-0.20140630233848-90b70fb433da/conf/load.go (about) 1 package conf 2 3 import ( 4 "io/ioutil" 5 "path/filepath" 6 ) 7 8 const ConfigFileName = "hroot.toml" 9 10 //A generic interface for loading configuration. 11 //Our implementation reads TOML files; roll your own! 12 type ConfigParser interface { 13 14 //Parses a new configuration string. 15 //Each call should override configuration added before. 16 AddConfig(data, dir string) ConfigParser 17 18 //Called to get the final configuration after loading. 19 GetConfig() *Configuration 20 21 } 22 23 //Recursively finds configuration files & folders. 24 func LoadConfigurationFromDisk(dir string, parser ConfigParser) (*Configuration, *Folders) { 25 //Default settings, folders, and parsed data 26 folders := DefaultFolders(dir) 27 files := []string{} 28 dirs := []string{} 29 30 //Recurse up the file tree looking for configuration 31 for { 32 33 //Try to read toml file 34 buf, err := ioutil.ReadFile(dir + "/" + ConfigFileName) 35 36 //Did we succeed? 37 if err == nil { 38 //Default graph folder is a child of the highest folder that has configuration 39 folders.Graph = filepath.Join(dir, GraphFolder) 40 41 //Convert data to a string, save for later 42 data := string(buf) 43 files = append(files, data) 44 dirs = append(dirs, dir) 45 46 //Increment folder for next stage 47 dir = filepath.Join("../", dir) 48 } else { 49 break //If the file was not readable, done loading config 50 } 51 } 52 53 //Unroll data - we discovered them in reverse order, send each to parser 54 for n := len(files) - 1; n >= 0; n-- { 55 parser.AddConfig(files[n], dirs[n]) 56 } 57 58 return parser.GetConfig(), folders 59 }