Skip to content

Commit

Permalink
Merge pull request #1 from canack/improvement
Browse files Browse the repository at this point in the history
Feature: Add flag to open in browser
  • Loading branch information
bayraktugrul authored Sep 13, 2024
2 parents 792ad39 + d9daf0b commit 5e78b70
Show file tree
Hide file tree
Showing 7 changed files with 681 additions and 619 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,10 @@ go.work.sum

# env file
.env

# ide
.idea
.vscode

# generated files
dependency_tree.html
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ your module's dependency graph.

<img src="modview-opt.gif" alt="modview go mod graph dependency tree" style="width: 100%; max-width: 100%;" />

To automatically open the dependency graph in your default web browser, use the `--open` flag:
```
modview --open
```

## Contributing
Contributions are welcome! Please feel free to submit a Pull Request.

Expand Down
6 changes: 3 additions & 3 deletions internal/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func Convert(r io.Reader) (*Graph, error) {
return &g, nil
}

func GenerateHTML(graph *Graph) string {
func GenerateHTML(graph *Graph) (string, error) {
data := struct {
Nodes []string
MvsPicked []string
Expand All @@ -111,15 +111,15 @@ func GenerateHTML(graph *Graph) string {
},
}).Parse(Template)
if err != nil {
panic(err)
return "", err
}

var buf bytes.Buffer
if err := tmplObj.Execute(&buf, data); err != nil {
panic(err)
}

return buf.String()
return buf.String(), nil
}

func getAllNodes(graph *Graph) []string {
Expand Down
28 changes: 28 additions & 0 deletions internal/browser.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package internal

import (
"fmt"
"os/exec"
"runtime"
)

func OpenInBrowser(path string) error {
var cmd string
var args []string

switch runtime.GOOS {
case "linux":
cmd = "xdg-open"
args = []string{path}
case "windows":
cmd = "cmd"
args = []string{"/c", "start", path}
case "darwin":
cmd = "open"
args = []string{path}
default:
return fmt.Errorf("unsupported platform")
}

return exec.Command(cmd, args...).Start()
}
Loading

0 comments on commit 5e78b70

Please sign in to comment.