Add root command to open depository with $EDITOR

- Implement openDepository function in cmd/root.go to open the depository directory in $EDITOR
- Falls back to vi if $EDITOR is not set
- Update README.md with new usage section documenting bare 'jade' command
- Include config.go changes that move config location to depository/.jade/
This commit is contained in:
2026-01-02 20:18:50 +01:00
parent 52160345bf
commit 0ebfaf835d
3 changed files with 69 additions and 34 deletions
+27 -29
View File
@@ -11,8 +11,6 @@ import (
type Config struct {
// DepoPath is the path to the jade depository. Default ~/jade-depository
DepoPath string `mapstructure:"depo_path"`
// ConfigPath is the path to the config directory. Default ~/.config/jade/config.yml
ConfigPath string `mapstructure:"config_path"`
// TagPrefix is the prefix used for tags in notes. Default "+"
TagPrefix string `mapstructure:"tag_prefix"`
}
@@ -26,13 +24,9 @@ func getDefaultDepoPath() string {
return filepath.Join(home, "jade-depository")
}
// getDefaultConfigPath returns the default config directory path
func getDefaultConfigPath() string {
home, err := os.UserHomeDir()
if err != nil {
return "./.config/jade/config.yml"
}
return filepath.Join(home, ".config", "jade", "config.yml")
// getConfigPath returns the config file path within the depository
func getConfigPath(depoPath string) string {
return filepath.Join(depoPath, ".jade", "config.yml")
}
// initConfig initializes the configuration using Viper
@@ -42,32 +36,39 @@ func initConfig(cfgPath, depoPath string) (*Config, error) {
viper.SetEnvPrefix("JADE")
viper.AutomaticEnv()
// Set defaults
viper.SetDefault("depo_path", getDefaultDepoPath())
viper.SetDefault("config_path", getDefaultConfigPath())
viper.SetDefault("tag_prefix", "+")
// Determine config file location
configDir := cfgPath
if configDir == "" {
configDir = getDefaultConfigPath()
// Determine depository path (from flag, env var, or default)
actualDepoPath := depoPath
if actualDepoPath == "" {
actualDepoPath = viper.GetString("depo_path")
if actualDepoPath == "" {
actualDepoPath = getDefaultDepoPath()
}
}
// Ensure config directory exists
if err := os.MkdirAll(configDir, 0755); err != nil {
return nil, fmt.Errorf("failed to create config directory: %w", err)
// Set defaults
viper.SetDefault("depo_path", actualDepoPath)
viper.SetDefault("tag_prefix", "+")
// Ensure depository and .jade directory exist
jadeDir := filepath.Join(actualDepoPath, ".jade")
if err := os.MkdirAll(jadeDir, 0755); err != nil {
return nil, fmt.Errorf("failed to create .jade directory: %w", err)
}
// Determine config file location
configFilePath := cfgPath
if configFilePath == "" {
configFilePath = getConfigPath(actualDepoPath)
}
// Set up config file
viper.SetConfigName("config")
viper.SetConfigType("yaml")
viper.AddConfigPath(configDir)
viper.SetConfigFile(configFilePath)
// Try to read existing config file
if err := viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
// Check if file doesn't exist (handles both viper.ConfigFileNotFoundError and os.IsNotExist)
if _, ok := err.(viper.ConfigFileNotFoundError); ok || os.IsNotExist(err) {
// Config file not found, create it with defaults
configFilePath := filepath.Join(configDir, "config.yaml")
if err := viper.SafeWriteConfigAs(configFilePath); err != nil {
return nil, fmt.Errorf("failed to create config file: %w", err)
}
@@ -81,9 +82,6 @@ func initConfig(cfgPath, depoPath string) (*Config, error) {
if depoPath != "" {
viper.Set("depo_path", depoPath)
}
if cfgPath != "" {
viper.Set("config_path", cfgPath)
}
// Unmarshal config into struct
cfg := &Config{}