Clean up config file name stuff

This commit is contained in:
Jon Staab
2026-05-26 15:50:14 -07:00
parent 2fcc48abed
commit e9260f40f1
7 changed files with 65 additions and 63 deletions
+21 -21
View File
@@ -1,12 +1,13 @@
package zooid
import (
"encoding/json"
"fiatjaf.com/nostr"
"fmt"
"regexp"
"github.com/BurntSushi/toml"
"os"
"path/filepath"
"regexp"
"slices"
)
@@ -60,7 +61,7 @@ type Config struct {
Roles map[string]Role `toml:"roles" json:"roles"`
// Private/parsed values
// Parsed values
path string
secret nostr.SecretKey
}
@@ -76,22 +77,14 @@ type BlossomS3Settings struct {
KeyPrefix string `toml:"key_prefix" json:"key_prefix"`
}
func ConfigPathFromId(id string) string {
return filepath.Join(Env("CONFIG"), id+".toml")
func ConfigNameFromId(id string) string {
return id + ".toml"
}
func ConfigPathFromName(name string) string {
return filepath.Join(Env("CONFIG"), name)
}
func LoadConfigFromId(id string) (*Config, error) {
return LoadConfigFromPath(ConfigPathFromId(id))
}
func LoadConfigFromName(name string) (*Config, error) {
return LoadConfigFromPath(ConfigPathFromName(name))
}
func LoadConfigFromPath(path string) (*Config, error) {
var config Config
if _, err := toml.DecodeFile(path, &config); err != nil {
@@ -107,6 +100,21 @@ func LoadConfigFromPath(path string) (*Config, error) {
return &config, nil
}
func LoadConfigFromJson(path string, body []byte) (*Config, error) {
var config Config
if err := json.Unmarshal(body, &config); err != nil {
return nil, fmt.Errorf("invalid json config: %w", err)
}
config.path = path
if err := config.Validate(); err != nil {
return nil, err
}
return &config, nil
}
func (config *Config) Validate() error {
if config.Blossom.Adapter == "" {
config.Blossom.Adapter = "local"
@@ -124,14 +132,12 @@ func (config *Config) Validate() error {
return fmt.Errorf("schema must contain only lowercase letters, numbers, and underscores")
}
secret, err := nostr.SecretKeyFromHex(config.Secret)
secret, err := nostr.SecretKeyFromHex(config.Secret)
if err != nil {
return fmt.Errorf("invalid secret key: %w", err)
}
// Make the secret... secret
config.Secret = ""
config.secret = secret
if _, err := nostr.PubKeyFromHex(config.Info.Pubkey); err != nil {
@@ -159,9 +165,6 @@ func (config *Config) Validate() error {
}
func (config *Config) Save() error {
// Restore the secret key to the public field for saving
config.Secret = config.secret.Hex()
file, err := os.Create(config.path)
if err != nil {
return fmt.Errorf("Failed to open config file %s: %w", config.path, err)
@@ -173,9 +176,6 @@ func (config *Config) Save() error {
return fmt.Errorf("Failed to encode config file %s: %w", config.path, err)
}
// Clear the secret again
config.Secret = ""
return nil
}