Last active
June 29, 2016 16:17
-
-
Save wyattjoh/68055b6657a74f549325d2d89347231f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"os" | |
"strings" | |
) | |
// FileConfigLoader describes a file based loader which loads the configuration | |
// from a file listed. | |
type FileConfigLoader struct { | |
Filename string | |
} | |
// Provide implements the Provider interface. | |
func (fcl FileConfigLoader) Provide() (map[string]string, error) { | |
var config = make(map[string]string) | |
file, err := os.Open(fcl.Filename) | |
if err != nil { | |
return nil, err | |
} | |
defer file.Close() | |
scanner := bufio.NewScanner(file) | |
scanner.Split(bufio.ScanLines) | |
for scanner.Scan() { | |
line := scanner.Text() | |
if len(line) < 3 { | |
// the line doesn't have enough data | |
continue | |
} | |
if line[0] == '#' { | |
// the line starts with a comment character | |
continue | |
} | |
// find the first equals sign | |
index := strings.Index(line, "=") | |
// if we couldn't find one | |
if index <= 0 { | |
// the line is invalid | |
continue | |
} | |
if index == len(line)-1 { | |
// the line is invalid | |
continue | |
} | |
// add the item to the config | |
config[line[:index]] = line[index+1:] | |
} | |
return config, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment