hid: cleanups
No functional changes.
This commit is contained in:
parent
f32e2f1483
commit
5a40d7c2ed
27
hid/main.go
27
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 -----------------------------------------------------------
|
||||
|
Loading…
Reference in New Issue
Block a user