aboutsummaryrefslogtreecommitdiff
path: root/xS
diff options
context:
space:
mode:
authorPřemysl Janouch <p@janouch.name>2018-08-06 12:31:31 +0200
committerPřemysl Janouch <p@janouch.name>2018-08-06 12:31:31 +0200
commit5c7ac9a92bd86f76b770d7d3fcc8265a346d00c0 (patch)
treef42109f6d07048cb27f13f955110f2d05bfbcfbf /xS
parent3fee7e8051dc65438e2e633f76c807d743f1a6ea (diff)
downloadxK-5c7ac9a92bd86f76b770d7d3fcc8265a346d00c0.tar.gz
xK-5c7ac9a92bd86f76b770d7d3fcc8265a346d00c0.tar.xz
xK-5c7ac9a92bd86f76b770d7d3fcc8265a346d00c0.zip
hid: cleanups
No functional changes.
Diffstat (limited to 'xS')
-rw-r--r--xS/main.go27
1 files changed, 15 insertions, 12 deletions
diff --git a/xS/main.go b/xS/main.go
index c49b2f6..826ea46 100644
--- a/xS/main.go
+++ b/xS/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 -----------------------------------------------------------