parent
3fee7e8051
commit
5c7ac9a92b
27
xS/main.go
27
xS/main.go
|
@ -192,27 +192,30 @@ func resolveFilename(filename string, relativeCB func(string) string) string {
|
||||||
// --- Simple file I/O ---------------------------------------------------------
|
// --- Simple file I/O ---------------------------------------------------------
|
||||||
|
|
||||||
// Overwrites filename contents with data; creates directories as needed.
|
// Overwrites filename contents with data; creates directories as needed.
|
||||||
func writeFile(filename string, data []byte) error {
|
func writeFile(path string, data []byte) error {
|
||||||
if dir := filepath.Dir(filename); dir != "." {
|
if dir := filepath.Dir(path); dir != "." {
|
||||||
if err := os.MkdirAll(dir, 0755); err != nil {
|
if err := os.MkdirAll(dir, 0755); err != nil {
|
||||||
return err
|
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
|
// Wrapper for writeFile that makes sure that the new data has been written
|
||||||
// to disk in its entirety before overriding the old file.
|
// to disk in its entirety before overriding the old file.
|
||||||
func writeFileSafe(filename string, data []byte) error {
|
func writeFileSafe(path string, data []byte) error {
|
||||||
temp := filename + ".new"
|
temp := path + ".new"
|
||||||
if err := writeFile(temp, data); err != nil {
|
if err := writeFile(temp, data); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return os.Rename(temp, filename)
|
return os.Rename(temp, path)
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- Simple configuration ----------------------------------------------------
|
// --- Simple configuration ----------------------------------------------------
|
||||||
|
|
||||||
|
// This is the bare minimum to make an application configurable.
|
||||||
|
// Keys are stripped of surrounding whitespace, values are not.
|
||||||
|
|
||||||
type simpleConfigItem struct {
|
type simpleConfigItem struct {
|
||||||
key string // INI key
|
key string // INI key
|
||||||
def string // default value
|
def string // default value
|
||||||
|
@ -229,8 +232,8 @@ func (sc simpleConfig) loadDefaults(table []simpleConfigItem) {
|
||||||
|
|
||||||
func (sc simpleConfig) updateFromFile() error {
|
func (sc simpleConfig) updateFromFile() error {
|
||||||
basename := projectName + ".conf"
|
basename := projectName + ".conf"
|
||||||
filename := resolveFilename(basename, resolveRelativeConfigFilename)
|
path := resolveFilename(basename, resolveRelativeConfigFilename)
|
||||||
if filename == "" {
|
if path == "" {
|
||||||
return &os.PathError{
|
return &os.PathError{
|
||||||
Op: "cannot find",
|
Op: "cannot find",
|
||||||
Path: basename,
|
Path: basename,
|
||||||
|
@ -238,7 +241,7 @@ func (sc simpleConfig) updateFromFile() error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
f, err := os.Open(filename)
|
f, err := os.Open(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -253,7 +256,7 @@ func (sc simpleConfig) updateFromFile() error {
|
||||||
|
|
||||||
equals := strings.IndexByte(line, '=')
|
equals := strings.IndexByte(line, '=')
|
||||||
if equals <= 0 {
|
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:]
|
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)
|
pathHint, strings.Join(prologLines, "\n"), table)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("configuration written to `%s'\n", filename)
|
log.Printf("configuration written to `%s'\n", path)
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- Configuration -----------------------------------------------------------
|
// --- Configuration -----------------------------------------------------------
|
||||||
|
|
Loading…
Reference in New Issue