Publicize the raw io.Reader reading function
This commit is contained in:
parent
863e415310
commit
3cfe8be7bb
@ -116,12 +116,7 @@ type Config struct {
|
|||||||
|
|
||||||
// Load loads and parses the files /etc/xdg/<name>/<name>.conf and
|
// Load loads and parses the files /etc/xdg/<name>/<name>.conf and
|
||||||
// <home>/.config/<name>/<name>.conf, unless the corresponding XDG environment
|
// <home>/.config/<name>/<name>.conf, unless the corresponding XDG environment
|
||||||
// variables are set - then it uses those. Configuration files are divided into
|
// variables are set - then it uses those.
|
||||||
// lines where each line may be blank, a comment, or a key-value pair. If the
|
|
||||||
// line is blank or begins with a # character, it is ignored. Else, the line
|
|
||||||
// must have a key and a value separated by a colon. Before they are processed,
|
|
||||||
// leading and trailing whitespace is trimmed from the key and the value. Keys
|
|
||||||
// are case sensitive.
|
|
||||||
func (config *Config) Load (name string) (err error) {
|
func (config *Config) Load (name string) (err error) {
|
||||||
if strings.ContainsAny(name, "/\\|:.%") {
|
if strings.ContainsAny(name, "/\\|:.%") {
|
||||||
err = ErrorIllegalName
|
err = ErrorIllegalName
|
||||||
@ -158,7 +153,7 @@ func (config *Config) Load (name string) (err error) {
|
|||||||
|
|
||||||
file, fileErr := os.Open(path)
|
file, fileErr := os.Open(path)
|
||||||
if fileErr != nil { continue }
|
if fileErr != nil { continue }
|
||||||
parseErr := config.loadFile(file)
|
parseErr := config.LoadFrom(file)
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
|
|
||||||
if parseErr != nil {
|
if parseErr != nil {
|
||||||
@ -171,8 +166,14 @@ func (config *Config) Load (name string) (err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (config *Config) loadFile (file io.Reader) (err error) {
|
// LoadFrom parses a configuration file from an io.Reader. Configuration files
|
||||||
scanner := bufio.NewScanner(file)
|
// are divided into lines where each line may be blank, a comment, or a
|
||||||
|
// key-value pair. If the line is blank or begins with a # character, it is
|
||||||
|
// ignored. Else, the line must have a key and a value separated by a colon.
|
||||||
|
// Before they are processed, leading and trailing whitespace is trimmed from
|
||||||
|
// the key and the value. Keys are case sensitive.
|
||||||
|
func (config *Config) LoadFrom (reader io.Reader) (err error) {
|
||||||
|
scanner := bufio.NewScanner(reader)
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
line := strings.TrimSpace(scanner.Text())
|
line := strings.TrimSpace(scanner.Text())
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user