1d87d93172
- Implement global CLI config at ~/.config/jade/config.yml - Add jade depo commands (add, list, remove, set-default) - Support depository short names and context-aware detection - Remove tag_prefix config, hardcode + syntax for consistency - Update depository resolution: flag -> context -> default - Auto-initialize .jade/ directory structure when adding depos - Update documentation with new multi-depository workflow
76 lines
1.5 KiB
Go
76 lines
1.5 KiB
Go
package cmd
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"os/exec"
|
|
|
|
"git.jnss.me/joakim/jadedepo/internal/engine"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
var (
|
|
rootCmd = &cobra.Command{
|
|
Short: "Jade CLI is a note-manager and orchestrator.",
|
|
Run: openDepository,
|
|
}
|
|
)
|
|
|
|
var (
|
|
depoInput string
|
|
)
|
|
|
|
func init() {
|
|
cobra.OnInitialize(initializeIfNeeded)
|
|
rootCmd.PersistentFlags().StringVar(&depoInput, "depo", "", "Depository name or path")
|
|
}
|
|
|
|
// initializeIfNeeded initializes the depository only for commands that need it
|
|
func initializeIfNeeded() {
|
|
// Skip initialization for depo management commands
|
|
cmd := os.Args
|
|
if len(cmd) > 1 && cmd[1] == "depo" {
|
|
return
|
|
}
|
|
|
|
// Initialize for all other commands
|
|
if _, err := engine.Init(depoInput); err != nil {
|
|
fmt.Fprintf(os.Stderr, "Error initializing configuration: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
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() {
|
|
if err := rootCmd.Execute(); err != nil {
|
|
fmt.Println(err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
os.Exit(0)
|
|
}
|