Skip to content

Commit

Permalink
fix a couple issues
Browse files Browse the repository at this point in the history
  • Loading branch information
gphorvath committed Nov 12, 2024
1 parent 3bd78a8 commit cf73680
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 38 deletions.
18 changes: 13 additions & 5 deletions src/cmd/common/file_operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@ import (
"github.com/gphorvath/grimoire/src/config"
)

// FileExists reports if a file exists
func FileExists(path string) bool {
_, err := os.Stat(path)
return !os.IsNotExist(err)
}

func CreateFileIfNotExists(path string) error {
if !FileExists(path) {
file, err := os.Create(path)
// CreateIfNotExists creates a file if it does not exist
// and returns an error if it fails to create the file
func CreateIfNotExists(filePath string) error {
if !FileExists(filePath) {
file, err := os.Create(filePath)
if err != nil {
return err
}
Expand All @@ -25,7 +28,10 @@ func CreateFileIfNotExists(path string) error {
return nil
}

func FindFile(baseDir, filename string) (string, error) {
// FindAndJoin walks the directory tree rooted at baseDir
// and returns the path of the first file with the given filename
// and an error if the file is not found
func FindAndJoin(baseDir, filename string) (string, error) {
var foundPath string
err := filepath.Walk(baseDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
Expand All @@ -46,7 +52,9 @@ func FindFile(baseDir, filename string) (string, error) {
return foundPath, nil
}

func OpenFileInEditor(path string) error {
// OpenInEditor opens a file in the configured editor
// and returns an error if it fails to open the file
func OpenInEditor(path string) error {
if !FileExists(path) {
return errors.New("file does not exist")
}
Expand Down
10 changes: 1 addition & 9 deletions src/cmd/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cmd
import (
"fmt"
"os"
"path/filepath"

"github.com/gphorvath/grimoire/src/cmd/common"
"github.com/gphorvath/grimoire/src/config"
Expand All @@ -25,19 +24,12 @@ func init() {
func runCopyCmd(cmd *cobra.Command, args []string) {
baseDir := config.GetPromptDir()
filename := args[0] + ".md"
dir, err := common.FindFile(baseDir, filename)
filePath, err := common.FindAndJoin(baseDir, filename)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}

if dir == "" {
fmt.Fprintf(os.Stderr, "Error: prompt not found\n")
os.Exit(1)
}

filePath := filepath.Join(dir, filename)

// Init returns an error if the package is not ready for use.
err = clipboard.Init()
if err != nil {
Expand Down
9 changes: 1 addition & 8 deletions src/cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cmd
import (
"fmt"
"os"
"path/filepath"

"github.com/gphorvath/grimoire/src/cmd/common"
"github.com/gphorvath/grimoire/src/config"
Expand All @@ -25,18 +24,12 @@ func runDeleteCmd(cmd *cobra.Command, args []string) {
baseDir := config.GetPromptDir()
filename := args[0] + ".md"

dir, err := common.FindFile(baseDir, filename)
filePath, err := common.FindAndJoin(baseDir, filename)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}

if dir == "" {
fmt.Fprintf(os.Stderr, "Error: prompt not found\n")
os.Exit(1)
}

filePath := filepath.Join(dir, filename)
if err := os.Remove(filePath); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
Expand Down
6 changes: 3 additions & 3 deletions src/cmd/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func runEditCmd(cmd *cobra.Command, args []string) {
filename := args[0] + ".md"

// Try to find existing file
filePath, err := common.FindFile(baseDir, filename)
filePath, err := common.FindAndJoin(baseDir, filename)
if err != nil && !os.IsNotExist(err) {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
Expand All @@ -35,7 +35,7 @@ func runEditCmd(cmd *cobra.Command, args []string) {
// If file doesn't exist, create it in the base directory
if filePath == "" {
filePath = filepath.Join(baseDir, filename)
if err := common.CreateFileIfNotExists(filePath); err != nil {
if err := common.CreateIfNotExists(filePath); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
Expand All @@ -47,7 +47,7 @@ func runEditCmd(cmd *cobra.Command, args []string) {
fmt.Printf("Created new prompt file %s\n", filePath)
}

if err := common.OpenFileInEditor(filePath); err != nil {
if err := common.OpenInEditor(filePath); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
Expand Down
53 changes: 40 additions & 13 deletions src/cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ import (
"io"
"net/http"
"os"
"path/filepath"
"strings"

"github.com/gphorvath/grimoire/src/cmd/common"

"github.com/gphorvath/grimoire/src/config"
"github.com/spf13/cobra"
)
Expand All @@ -27,22 +25,29 @@ type OllamaResponse struct {
}

var (
promptFile string
promptFile string
editBeforeGenerate bool

generateCmd = &cobra.Command{
Use: "generate [flags] [input...]",
Short: "Get generation from Ollama",
Long: "Requests generation from Ollama while optionally prepending a prompt",
Args: cobra.MinimumNArgs(1),
RunE: runGenerate,
Args: func(cmd *cobra.Command, args []string) error {
editFlag, _ := cmd.Flags().GetBool("edit")
if !editFlag && len(args) < 1 {
return fmt.Errorf("requires at least 1 arg when not using edit flag")
}
return nil
},
RunE: runGenerate,
}
)

func init() {
rootCmd.AddCommand(generateCmd)
generateCmd.Flags().StringVarP(&promptFile, "prompt", "p", "", "Prompt file to prepend to input")
generateCmd.Flags().BoolVarP(&editBeforeGenerate, "edit", "e", false, "Edit input before generating")
}

func runGenerate(cmd *cobra.Command, args []string) error {
// Join all arguments as the input text
input := strings.Join(args, " ")
Expand All @@ -53,17 +58,12 @@ func runGenerate(cmd *cobra.Command, args []string) error {
baseDir := config.GetPromptDir()
filename := promptFile + ".md"

dir, err := common.FindFile(baseDir, filename)
filepath, err := common.FindAndJoin(baseDir, filename)
if err != nil {
return err
}

if dir == "" {
return fmt.Errorf("prompt not found")
}

filePath := filepath.Join(dir, filename)
content, err := os.ReadFile(filePath)
content, err := os.ReadFile(filepath)
if err != nil {
return err
}
Expand All @@ -73,6 +73,33 @@ func runGenerate(cmd *cobra.Command, args []string) error {
finalPrompt = input
}

if editBeforeGenerate {
// Create temporary file for editing
tmpFile, err := os.CreateTemp("", "grimoire-*.txt")
if err != nil {
return err
}
defer os.Remove(tmpFile.Name())

// Write prompt to temp file
if _, err := tmpFile.WriteString(finalPrompt); err != nil {
return err
}
tmpFile.Close()

// Open in editor
if err := common.OpenInEditor(tmpFile.Name()); err != nil {
return err
}

// Read back edited content
content, err := os.ReadFile(tmpFile.Name())
if err != nil {
return err
}
finalPrompt = string(content)
}

// Create request body
reqBody := OllamaRequest{
Model: config.OllamaModel,
Expand Down

0 comments on commit cf73680

Please sign in to comment.