Return error from GetDefaultStateDir when home directory unavailable

When os.UserConfigDir() fails, DetermineStateDir falls back to
os.UserHomeDir(). Previously the error from UserHomeDir was discarded,
which could result in a dangerous root-relative path (/.config/...) if
both calls fail.

Now DetermineStateDir returns (string, error) and propagates failures
from both UserConfigDir and UserHomeDir.

Closes #14
This commit is contained in:
clawbot
2026-02-15 14:05:15 -08:00
parent 6ff00c696a
commit 6211b8e768
4 changed files with 77 additions and 11 deletions

View File

@@ -19,7 +19,10 @@ type Instance struct {
// NewCLIInstance creates a new CLI instance with the real filesystem
func NewCLIInstance() *Instance {
fs := afero.NewOsFs()
stateDir := secret.DetermineStateDir("")
stateDir, err := secret.DetermineStateDir("")
if err != nil {
panic(fmt.Sprintf("cannot determine state directory: %v", err))
}
return &Instance{
fs: fs,
@@ -29,7 +32,10 @@ func NewCLIInstance() *Instance {
// NewCLIInstanceWithFs creates a new CLI instance with the given filesystem (for testing)
func NewCLIInstanceWithFs(fs afero.Fs) *Instance {
stateDir := secret.DetermineStateDir("")
stateDir, err := secret.DetermineStateDir("")
if err != nil {
panic(fmt.Sprintf("cannot determine state directory: %v", err))
}
return &Instance{
fs: fs,