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
+14 -4
View File
@@ -19,6 +19,16 @@ go build -o jade
## Usage ## Usage
### Open Depository
```bash
# Open the depository directory in your $EDITOR
jade
# This opens ~/jade-depository (or your configured path) in your editor
# Works great with neovim, vim, or any editor that supports directory opening
```
### Initialize and List Notes ### Initialize and List Notes
```bash ```bash
@@ -77,24 +87,24 @@ jade tags
## Configuration ## Configuration
Configuration is stored in `~/.config/jade/config.yml` (by default). Configuration is stored in `<depo_path>/.jade/config.yml` and is created automatically when you first run jade.
Example config: Example config:
```yaml ```yaml
depo_path: /home/user/jade-depository depo_path: /home/user/jade-depository
config_path: /home/user/.config/jade
tag_prefix: "+" tag_prefix: "+"
``` ```
You can override these with flags: You can override these with flags:
- `--depo`: Depository path - `--depo`: Depository path
- `--config`: Config directory path - `--config`: Config file path (if you need to override the default location)
## Depository Structure ## Depository Structure
``` ```
jade-depository/ jade-depository/
├── .jade/ ├── .jade/
│ ├── config.yml # Configuration file
│ └── trash/ # Deleted notes go here │ └── trash/ # Deleted notes go here
├── note-1.md ├── note-1.md
├── note-2.md ├── note-2.md
@@ -102,7 +112,7 @@ jade-depository/
└── note-3.md └── note-3.md
``` ```
The `.jade/` directory is automatically created and should be added to `.gitignore`. The `.jade/` directory is automatically created and contains configuration and metadata. You can optionally add it to `.gitignore` if you don't want to track it in version control.
## Note Format ## Note Format
+28 -1
View File
@@ -3,6 +3,7 @@ package cmd
import ( import (
"fmt" "fmt"
"os" "os"
"os/exec"
"git.jnss.me/joakim/jadedepo/internal/engine" "git.jnss.me/joakim/jadedepo/internal/engine"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@@ -11,6 +12,7 @@ import (
var ( var (
rootCmd = &cobra.Command{ rootCmd = &cobra.Command{
Short: "Jade CLI is a note-manager and orchestrator.", Short: "Jade CLI is a note-manager and orchestrator.",
Run: openDepository,
} }
) )
@@ -27,11 +29,36 @@ func init() {
} }
}) })
rootCmd.PersistentFlags().StringVar(&cfgPath, "config", "", "Configuration path. Default $HOME/.config/jade") rootCmd.PersistentFlags().StringVar(&cfgPath, "config", "", "Configuration file path. Default <depo>/.jade/config.yml")
rootCmd.PersistentFlags().StringVar(&depoPath, "depo", "", "Depository path. Default $HOME/jade-depository") rootCmd.PersistentFlags().StringVar(&depoPath, "depo", "", "Depository path. Default $HOME/jade-depository")
} }
func openDepository(cmd *cobra.Command, args []string) {
jd, err := engine.GetInstance()
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
// Get editor from environment variable, fallback to vi
editor := os.Getenv("EDITOR")
if editor == "" {
editor = "vi"
}
// Open the depository directory with the editor
editorCmd := exec.Command(editor, jd.Config.DepoPath)
editorCmd.Stdin = os.Stdin
editorCmd.Stdout = os.Stdout
editorCmd.Stderr = os.Stderr
if err := editorCmd.Run(); err != nil {
fmt.Fprintf(os.Stderr, "Error opening depository: %v\n", err)
os.Exit(1)
}
}
func Execute() { func Execute() {
if err := rootCmd.Execute(); err != nil { if err := rootCmd.Execute(); err != nil {
fmt.Println(err) fmt.Println(err)
+27 -29
View File
@@ -11,8 +11,6 @@ import (
type Config struct { type Config struct {
// DepoPath is the path to the jade depository. Default ~/jade-depository // DepoPath is the path to the jade depository. Default ~/jade-depository
DepoPath string `mapstructure:"depo_path"` 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 is the prefix used for tags in notes. Default "+"
TagPrefix string `mapstructure:"tag_prefix"` TagPrefix string `mapstructure:"tag_prefix"`
} }
@@ -26,13 +24,9 @@ func getDefaultDepoPath() string {
return filepath.Join(home, "jade-depository") return filepath.Join(home, "jade-depository")
} }
// getDefaultConfigPath returns the default config directory path // getConfigPath returns the config file path within the depository
func getDefaultConfigPath() string { func getConfigPath(depoPath string) string {
home, err := os.UserHomeDir() return filepath.Join(depoPath, ".jade", "config.yml")
if err != nil {
return "./.config/jade/config.yml"
}
return filepath.Join(home, ".config", "jade", "config.yml")
} }
// initConfig initializes the configuration using Viper // initConfig initializes the configuration using Viper
@@ -42,32 +36,39 @@ func initConfig(cfgPath, depoPath string) (*Config, error) {
viper.SetEnvPrefix("JADE") viper.SetEnvPrefix("JADE")
viper.AutomaticEnv() viper.AutomaticEnv()
// Set defaults // Determine depository path (from flag, env var, or default)
viper.SetDefault("depo_path", getDefaultDepoPath()) actualDepoPath := depoPath
viper.SetDefault("config_path", getDefaultConfigPath()) if actualDepoPath == "" {
viper.SetDefault("tag_prefix", "+") actualDepoPath = viper.GetString("depo_path")
if actualDepoPath == "" {
// Determine config file location actualDepoPath = getDefaultDepoPath()
configDir := cfgPath }
if configDir == "" {
configDir = getDefaultConfigPath()
} }
// Ensure config directory exists // Set defaults
if err := os.MkdirAll(configDir, 0755); err != nil { viper.SetDefault("depo_path", actualDepoPath)
return nil, fmt.Errorf("failed to create config directory: %w", err) 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 // Set up config file
viper.SetConfigName("config") viper.SetConfigFile(configFilePath)
viper.SetConfigType("yaml")
viper.AddConfigPath(configDir)
// Try to read existing config file // Try to read existing config file
if err := viper.ReadInConfig(); err != nil { 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 // Config file not found, create it with defaults
configFilePath := filepath.Join(configDir, "config.yaml")
if err := viper.SafeWriteConfigAs(configFilePath); err != nil { if err := viper.SafeWriteConfigAs(configFilePath); err != nil {
return nil, fmt.Errorf("failed to create config file: %w", err) return nil, fmt.Errorf("failed to create config file: %w", err)
} }
@@ -81,9 +82,6 @@ func initConfig(cfgPath, depoPath string) (*Config, error) {
if depoPath != "" { if depoPath != "" {
viper.Set("depo_path", depoPath) viper.Set("depo_path", depoPath)
} }
if cfgPath != "" {
viper.Set("config_path", cfgPath)
}
// Unmarshal config into struct // Unmarshal config into struct
cfg := &Config{} cfg := &Config{}