Return configuration save failures
This commit is contained in:
committed by
Brandon McGinty
parent
501943c8f7
commit
c36305cb38
+11
-9
@@ -46,20 +46,22 @@ type eUser struct {
|
||||
LocallyMuted bool // Changed from Muted to LocallyMuted to match User struct
|
||||
}
|
||||
|
||||
func (c *Config) SaveConfig() {
|
||||
var data []byte
|
||||
// SaveConfig atomically replaces the persisted configuration. Errors are
|
||||
// returned so an unavailable directory cannot crash the client.
|
||||
func (c *Config) SaveConfig() error {
|
||||
data, err := toml.Marshal(c.config)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
return err
|
||||
}
|
||||
err = ioutil.WriteFile(c.fn+".tmp", data, 0600)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
tmp := c.fn + ".tmp"
|
||||
if err := ioutil.WriteFile(tmp, data, 0600); err != nil {
|
||||
return err
|
||||
}
|
||||
err = os.Rename(c.fn+".tmp", c.fn)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
if err := os.Rename(tmp, c.fn); err != nil {
|
||||
_ = os.Remove(tmp)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func key(k uiterm.Key) *uiterm.Key {
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// Regression: a configuration write failure panicked the client instead of
|
||||
// returning an error to the caller.
|
||||
func TestSaveConfigReturnsWriteError(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "missing", "barnard.toml")
|
||||
cfg := NewConfig(&path)
|
||||
if err := cfg.SaveConfig(); err == nil {
|
||||
t.Fatal("expected configuration write error")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user