52 lines
1.0 KiB
Go
52 lines
1.0 KiB
Go
package tins2020
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
type ResourceLoader struct {
|
|
Resources map[string]string
|
|
}
|
|
|
|
func NewResourceLoader() *ResourceLoader {
|
|
return &ResourceLoader{}
|
|
}
|
|
|
|
func (l *ResourceLoader) parseResourcesFile(res *Resources, name string) error {
|
|
f, err := res.Fs().Open(name)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer f.Close()
|
|
|
|
l.Resources = map[string]string{}
|
|
scanner := bufio.NewScanner(f)
|
|
for scanner.Scan() {
|
|
definition := scanner.Text()
|
|
sep := strings.Index(definition, ":")
|
|
if sep == -1 {
|
|
continue
|
|
}
|
|
name := strings.TrimSpace(definition[:sep])
|
|
content := strings.TrimSpace(definition[sep+1:])
|
|
l.Resources[name] = content
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (l *ResourceLoader) LoadFromFile(res *Resources, name string, action func(string, string) error) error {
|
|
err := l.parseResourcesFile(res, name)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for name, content := range l.Resources {
|
|
err = action(name, content)
|
|
if err != nil {
|
|
return fmt.Errorf("cannot load resource '%s'; error: %v", name, err)
|
|
}
|
|
}
|
|
return nil
|
|
}
|