Files
go-nc-files/defaultpath.go
T
Konrad NeitzelandCursor 9068d8ba8e Extract Files seam, WebDAV, and Visit from CheckDNS.
Publish go-nc-files v1 with local/WebDAV Root, DefaultPathStore, and
neutral Working Folder language; depends on go-nc-exapp v0.1.0.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-26 19:16:53 +02:00

66 lines
1.5 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package goncfiles
import (
"encoding/json"
"os"
"path/filepath"
"strings"
)
// DefaultPathStore persists the Saved Default Working Folder path (User Files Root–relative).
type DefaultPathStore interface {
GetDefault() (string, error)
SetDefault(rel string) error
}
// FileStore is the local-adapter settings JSON file.
type FileStore struct {
Path string
}
type settingsFile struct {
DefaultPath string `json:"defaultPath"`
}
// GetDefault reads the saved default; empty means the caller should use the product initial default.
func (s FileStore) GetDefault() (string, error) {
body, err := os.ReadFile(s.Path)
if err != nil {
if os.IsNotExist(err) {
return "", nil
}
return "", err
}
var sf settingsFile
if err := json.Unmarshal(body, &sf); err != nil {
return "", err
}
return strings.TrimSpace(sf.DefaultPath), nil
}
// SetDefault writes the saved Working Folder path.
func (s FileStore) SetDefault(rel string) error {
body, err := json.MarshalIndent(settingsFile{DefaultPath: rel}, "", " ")
if err != nil {
return err
}
if err := os.MkdirAll(filepath.Dir(s.Path), 0o755); err != nil {
return err
}
return os.WriteFile(s.Path, append(body, '\n'), 0o644)
}
// Memory is an in-memory DefaultPathStore for tests.
type Memory struct {
Value string
}
// GetDefault returns the in-memory value.
func (m *Memory) GetDefault() (string, error) { return m.Value, nil }
// SetDefault stores the value in memory.
func (m *Memory) SetDefault(rel string) error {
m.Value = rel
return nil
}