Something went wrong. Try again.
cli + tui to publish to leaflet (wip) & manage tasks, notes & watch/read lists 🍃
charm leaflet readability golang
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127package store
import ( "os" "path/filepath"
"github.com/BurntSushi/toml" "github.com/stormlightlabs/noteleaf/internal/shared")
// Config holds application configurationtype Config struct { DatabasePath string `toml:"database_path,omitempty"` DataDir string `toml:"data_dir,omitempty"` DateFormat string `toml:"date_format"` ColorScheme string `toml:"color_scheme"` DefaultView string `toml:"default_view"` DefaultPriority string `toml:"default_priority,omitempty"` Editor string `toml:"editor,omitempty"` ArticlesDir string `toml:"articles_dir,omitempty"` NotesDir string `toml:"notes_dir,omitempty"` AutoArchive bool `toml:"auto_archive"` SyncEnabled bool `toml:"sync_enabled"` SyncEndpoint string `toml:"sync_endpoint,omitempty"` SyncToken string `toml:"sync_token,omitempty"` ExportFormat string `toml:"export_format"` MovieAPIKey string `toml:"movie_api_key,omitempty"` BookAPIKey string `toml:"book_api_key,omitempty"`
ATProtoDID string `toml:"atproto_did,omitempty"` ATProtoHandle string `toml:"atproto_handle,omitempty"` ATProtoAccessJWT string `toml:"atproto_access_jwt,omitempty"` ATProtoRefreshJWT string `toml:"atproto_refresh_jwt,omitempty"` ATProtoPDSURL string `toml:"atproto_pds_url,omitempty"` ATProtoExpiresAt string `toml:"atproto_expires_at,omitempty"` // ISO8601 timestamp}
// DefaultConfig returns a configuration with sensible defaultsfunc DefaultConfig() *Config { return &Config{ DateFormat: "2006-01-02", ColorScheme: "default", DefaultView: "list", AutoArchive: false, SyncEnabled: false, ExportFormat: "json", }}
// LoadConfig loads configuration from the config directory or NOTELEAF_CONFIG pathvar LoadConfig = func() (*Config, error) { var configPath string
if envConfigPath := os.Getenv("NOTELEAF_CONFIG"); envConfigPath != "" { configPath = envConfigPath } else { configDir, err := GetConfigDir() if err != nil { return nil, shared.ConfigError("failed to get config directory", err) } configPath = filepath.Join(configDir, ".noteleaf.conf.toml") }
if _, err := os.Stat(configPath); os.IsNotExist(err) { config := DefaultConfig() if err := SaveConfig(config); err != nil { return nil, shared.ConfigError("failed to create default config", err) } return config, nil }
data, err := os.ReadFile(configPath) if err != nil { return nil, shared.ConfigError("failed to read config file", err) }
config := DefaultConfig() if err := toml.Unmarshal(data, config); err != nil { return nil, shared.ConfigError("failed to parse config file", err) }
return config, nil}
// SaveConfig saves the configuration to the config directory or NOTELEAF_CONFIG pathfunc SaveConfig(config *Config) error { var configPath string
if envConfigPath := os.Getenv("NOTELEAF_CONFIG"); envConfigPath != "" { configPath = envConfigPath configDir := filepath.Dir(configPath) if err := os.MkdirAll(configDir, 0755); err != nil { return shared.ConfigError("failed to create config directory", err) } } else { configDir, err := GetConfigDir() if err != nil { return shared.ConfigError("failed to get config directory", err) } configPath = filepath.Join(configDir, ".noteleaf.conf.toml") }
data, err := toml.Marshal(config) if err != nil { return shared.ConfigError("failed to marshal config", err) }
if err := os.WriteFile(configPath, data, 0644); err != nil { return shared.ConfigError("failed to write config file", err) }
return nil}
// GetConfigPath returns the path to the configuration filefunc GetConfigPath() (string, error) { if envConfigPath := os.Getenv("NOTELEAF_CONFIG"); envConfigPath != "" { return envConfigPath, nil }
configDir, err := GetConfigDir() if err != nil { return "", err } return filepath.Join(configDir, ".noteleaf.conf.toml"), nil}