diff options
author | Přemysl Janouch <p@janouch.name> | 2018-08-06 12:31:31 +0200 |
---|---|---|
committer | Přemysl Janouch <p@janouch.name> | 2018-08-06 12:31:31 +0200 |
commit | 5a40d7c2edc2ad323d102aa608cf06645040ec14 (patch) | |
tree | 501300c1a9fae44b5c91faf6a06356f548fece50 /hid/main.go | |
parent | f32e2f14839e6560409897d9472fd6e3820e2568 (diff) | |
download | haven-5a40d7c2edc2ad323d102aa608cf06645040ec14.tar.gz haven-5a40d7c2edc2ad323d102aa608cf06645040ec14.tar.xz haven-5a40d7c2edc2ad323d102aa608cf06645040ec14.zip |
hid: cleanups
No functional changes.
Diffstat (limited to 'hid/main.go')
-rw-r--r-- | hid/main.go | 27 |
1 files changed, 15 insertions, 12 deletions
diff --git a/hid/main.go b/hid/main.go index c49b2f6..826ea46 100644 --- a/hid/main.go +++ b/hid/main.go @@ -192,27 +192,30 @@ func resolveFilename(filename string, relativeCB func(string) string) string { // --- Simple file I/O --------------------------------------------------------- // Overwrites filename contents with data; creates directories as needed. -func writeFile(filename string, data []byte) error { - if dir := filepath.Dir(filename); dir != "." { +func writeFile(path string, data []byte) error { + if dir := filepath.Dir(path); dir != "." { if err := os.MkdirAll(dir, 0755); err != nil { return err } } - return ioutil.WriteFile(filename, data, 0644) + return ioutil.WriteFile(path, data, 0644) } // Wrapper for writeFile that makes sure that the new data has been written // to disk in its entirety before overriding the old file. -func writeFileSafe(filename string, data []byte) error { - temp := filename + ".new" +func writeFileSafe(path string, data []byte) error { + temp := path + ".new" if err := writeFile(temp, data); err != nil { return err } - return os.Rename(temp, filename) + return os.Rename(temp, path) } // --- Simple configuration ---------------------------------------------------- +// This is the bare minimum to make an application configurable. +// Keys are stripped of surrounding whitespace, values are not. + type simpleConfigItem struct { key string // INI key def string // default value @@ -229,8 +232,8 @@ func (sc simpleConfig) loadDefaults(table []simpleConfigItem) { func (sc simpleConfig) updateFromFile() error { basename := projectName + ".conf" - filename := resolveFilename(basename, resolveRelativeConfigFilename) - if filename == "" { + path := resolveFilename(basename, resolveRelativeConfigFilename) + if path == "" { return &os.PathError{ Op: "cannot find", Path: basename, @@ -238,7 +241,7 @@ func (sc simpleConfig) updateFromFile() error { } } - f, err := os.Open(filename) + f, err := os.Open(path) if err != nil { return err } @@ -253,7 +256,7 @@ func (sc simpleConfig) updateFromFile() error { equals := strings.IndexByte(line, '=') if equals <= 0 { - return fmt.Errorf("%s:%d: malformed line", filename, lineNo) + return fmt.Errorf("%s:%d: malformed line", path, lineNo) } sc[strings.TrimRight(line[:equals], " \t")] = line[equals+1:] @@ -295,13 +298,13 @@ func callSimpleConfigWriteDefault(pathHint string, table []simpleConfigItem) { ``, } - filename, err := simpleConfigWriteDefault( + path, err := simpleConfigWriteDefault( pathHint, strings.Join(prologLines, "\n"), table) if err != nil { log.Fatalln(err) } - log.Printf("configuration written to `%s'\n", filename) + log.Printf("configuration written to `%s'\n", path) } // --- Configuration ----------------------------------------------------------- |