generated from wesen/wesen-go-template
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from wesen/task/add-which-edit
Add which and edit commands for prompt management
- Loading branch information
Showing
7 changed files
with
167 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
Added Which Command | ||
Added a new "which" command to show the source location of prompts, helping users locate prompt files in their repositories. | ||
|
||
- Added new which command that shows repository and file location for a given prompt | ||
- Integrated which command into root command structure | ||
|
||
Added Edit Command | ||
Added a new "edit" command to open prompts in the user's preferred editor ($EDITOR). | ||
|
||
- Added new edit command that opens prompt files in the system editor | ||
- Uses $EDITOR environment variable with fallback to vim | ||
- Integrated edit command into root command structure |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package cmds | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
|
||
"github.com/go-go-golems/prompto/pkg" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
type EditCommand struct { | ||
repositories []string | ||
} | ||
|
||
func NewEditCommand(options *CommandOptions) *cobra.Command { | ||
editCmd := &EditCommand{ | ||
repositories: options.Repositories, | ||
} | ||
|
||
return &cobra.Command{ | ||
Use: "edit prompt-name", | ||
Short: "Edit a prompt in your default editor", | ||
Args: cobra.ExactArgs(1), | ||
RunE: editCmd.run, | ||
} | ||
} | ||
|
||
func openInEditor(filepath string) error { | ||
editor := os.Getenv("EDITOR") | ||
if editor == "" { | ||
editor = "vim" // Default to vim | ||
} | ||
|
||
cmd := exec.Command(editor, filepath) | ||
cmd.Stdin = os.Stdin | ||
cmd.Stdout = os.Stdout | ||
cmd.Stderr = os.Stderr | ||
|
||
return cmd.Run() | ||
} | ||
|
||
func (e *EditCommand) run(cmd *cobra.Command, args []string) error { | ||
promptName := args[0] | ||
|
||
for _, repoPath := range e.repositories { | ||
repo := pkg.NewRepository(repoPath) | ||
err := repo.LoadPromptos() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, file := range repo.Promptos { | ||
if file.Name == promptName { | ||
fullPath := filepath.Join(repoPath, "prompto", file.Name) | ||
err := openInEditor(fullPath) | ||
if err != nil { | ||
return fmt.Errorf("failed to open editor: %w", err) | ||
} | ||
return nil | ||
} | ||
} | ||
} | ||
|
||
return fmt.Errorf("prompt '%s' not found in any repository", promptName) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package cmds | ||
|
||
import ( | ||
"fmt" | ||
"path/filepath" | ||
|
||
"github.com/go-go-golems/prompto/pkg" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
type WhichCommand struct { | ||
repositories []string | ||
} | ||
|
||
func NewWhichCommand(options *CommandOptions) *cobra.Command { | ||
whichCmd := &WhichCommand{ | ||
repositories: options.Repositories, | ||
} | ||
|
||
return &cobra.Command{ | ||
Use: "which prompt-name", | ||
Short: "Show the source location of a prompt", | ||
Args: cobra.ExactArgs(1), | ||
RunE: whichCmd.run, | ||
} | ||
} | ||
|
||
func (w *WhichCommand) run(cmd *cobra.Command, args []string) error { | ||
promptName := args[0] | ||
|
||
for _, repoPath := range w.repositories { | ||
repo := pkg.NewRepository(repoPath) | ||
err := repo.LoadPromptos() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, file := range repo.Promptos { | ||
if file.Name == promptName { | ||
fullPath := filepath.Join(repoPath, "prompto", file.Name) | ||
fmt.Printf("Repository: %s\nFile: %s\nFull path: %s\n", repoPath, file.Name, fullPath) | ||
return nil | ||
} | ||
} | ||
} | ||
|
||
return fmt.Errorf("prompt '%s' not found in any repository", promptName) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters