diff --git a/.gitignore b/.gitignore index 3b1d510..1591736 100644 --- a/.gitignore +++ b/.gitignore @@ -29,11 +29,8 @@ go.work.sum # env file .env -# Editor/IDE -# .idea/ -# .vscode/ - # Editor/IDE .claude/ test/ +.DS_Store diff --git a/.issues/.counter b/.issues/.counter index aabe6ec..2bd5a0a 100644 --- a/.issues/.counter +++ b/.issues/.counter @@ -1 +1 @@ -21 +22 diff --git a/.issues/closed/021-add-open-file-command-to-open-issue-markdown-in-de.md b/.issues/closed/021-add-open-file-command-to-open-issue-markdown-in-de.md new file mode 100644 index 0000000..1213ce3 --- /dev/null +++ b/.issues/closed/021-add-open-file-command-to-open-issue-markdown-in-de.md @@ -0,0 +1,33 @@ +--- +id: "021" +assignee: "" +labels: + - feature +created: 2026-02-06T14:27:24.912527+09:00 +updated: 2026-02-06T14:40:03.891202+09:00 +--- + +# Add view command to open issue markdown in default program + +## Description + +Add a new `gi view` command that opens an issue's markdown file using the system's default program associated with the `.md` extension. This allows users to quickly view or edit issues in their preferred markdown editor or viewer (e.g., Typora, VS Code, Obsidian). + +Usage: `gi view 001` + +## Requirements + +- Accept an issue ID as argument (e.g., `gi view 001`) +- Look up the issue file in `.issues/open/` and `.issues/closed/` +- Open the file using the OS default program for `.md` files: + - macOS: use `open` command + - Linux: use `xdg-open` command +- Show an error if the issue ID is not found + +## Success Criteria + +- [ ] `gi view ` opens the issue markdown in the default associated program +- [ ] Works on macOS (`open`) and Linux (`xdg-open`) +- [ ] Displays clear error when issue ID does not exist +- [ ] Unit tests for file lookup logic +- [ ] Update README file diff --git a/README.md b/README.md index 777c842..472187e 100644 --- a/README.md +++ b/README.md @@ -82,6 +82,13 @@ gi edit 001 # Opens the issue file in $EDITOR (defaults to vim) ``` +### View an issue in default program + +```bash +gi view 001 +# Opens the issue markdown file in the system's default program +``` + ### Search issues ```bash diff --git a/cmd/view.go b/cmd/view.go new file mode 100644 index 0000000..0ebc6fd --- /dev/null +++ b/cmd/view.go @@ -0,0 +1,52 @@ +package cmd + +import ( + "fmt" + "os/exec" + "runtime" + + "github.com/Allra-Fintech/git-issue/pkg" + "github.com/fatih/color" + "github.com/spf13/cobra" +) + +var viewCmd = &cobra.Command{ + Use: "view ", + Short: "Open an issue in the default program", + Long: `Open an issue's markdown file in the system's default program (e.g., Typora, VS Code, Obsidian).`, + Args: cobra.ExactArgs(1), + RunE: runView, +} + +func init() { + rootCmd.AddCommand(viewCmd) +} + +func runView(cmd *cobra.Command, args []string) error { + issueID := args[0] + + // Find the issue file + path, _, err := pkg.FindIssueFile(issueID) + if err != nil { + return fmt.Errorf("failed to find issue: %w", err) + } + + // Determine the OS-appropriate open command + var opener string + switch runtime.GOOS { + case "darwin": + opener = "open" + case "linux": + opener = "xdg-open" + default: + return fmt.Errorf("unsupported platform: %s", runtime.GOOS) + } + + // Open the file in the default program (non-blocking) + if err := exec.Command(opener, path).Start(); err != nil { + return fmt.Errorf("failed to open file: %w", err) + } + + color.New(color.FgGreen).Printf("✓ Opened issue #%s in default program\n", issueID) + return nil +} diff --git a/cmd/view_test.go b/cmd/view_test.go new file mode 100644 index 0000000..7e25e51 --- /dev/null +++ b/cmd/view_test.go @@ -0,0 +1,21 @@ +package cmd + +import ( + "testing" +) + +func TestViewMissingArgs(t *testing.T) { + cmd := viewCmd + err := cmd.Args(cmd, []string{}) + if err == nil { + t.Error("expected error for missing arguments") + } +} + +func TestViewNonExistentIssue(t *testing.T) { + // runView should fail when the issue doesn't exist + err := runView(viewCmd, []string{"999"}) + if err == nil { + t.Error("expected error for non-existent issue") + } +}