From 7012523a7ea377e12255c54da007165c766cdf86 Mon Sep 17 00:00:00 2001 From: Engin Date: Fri, 13 Sep 2024 15:15:19 +0300 Subject: [PATCH 1/5] Update gitignore --- .gitignore | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/.gitignore b/.gitignore index 6f72f89..a159f9b 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,10 @@ go.work.sum # env file .env + +# ide +.idea +.vscode + +# generated files +dependency_tree.html From d700974cdc25e2e24fb45f4b977612f8bd3d4395 Mon Sep 17 00:00:00 2001 From: Engin Date: Fri, 13 Sep 2024 15:18:06 +0300 Subject: [PATCH 2/5] Create template file and use it --- internal/template.go | 614 +---------------------------------------- internal/template.html | 608 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 611 insertions(+), 611 deletions(-) create mode 100644 internal/template.html diff --git a/internal/template.go b/internal/template.go index 6e1cccc..955f690 100644 --- a/internal/template.go +++ b/internal/template.go @@ -1,614 +1,6 @@ package internal -const ( - Template = ` - - - - - - Dependency Graph - - - - - -
-
-
- Picked dependency by MVS algorithm -
-
-
- Unpicked dependency -
-
-
- - -
-
-
-
- - -
-
Copied!
- - - -` -) +//go:embed template.html +var Template string diff --git a/internal/template.html b/internal/template.html new file mode 100644 index 0000000..6608e82 --- /dev/null +++ b/internal/template.html @@ -0,0 +1,608 @@ + + + + + + Dependency Graph + + + + + +
+
+
+ Picked dependency by MVS algorithm +
+
+
+ Unpicked dependency +
+
+
+ + +
+
+
+
+ + +
+
Copied!
+ + + \ No newline at end of file From 084697a263d9b6b646021bf14ada22655347cf17 Mon Sep 17 00:00:00 2001 From: Engin Date: Fri, 13 Sep 2024 15:18:26 +0300 Subject: [PATCH 3/5] Return error and use it --- internal/app.go | 6 +++--- main.go | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/internal/app.go b/internal/app.go index 4be3dbf..9ac3bdb 100644 --- a/internal/app.go +++ b/internal/app.go @@ -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 @@ -111,7 +111,7 @@ func GenerateHTML(graph *Graph) string { }, }).Parse(Template) if err != nil { - panic(err) + return "", err } var buf bytes.Buffer @@ -119,7 +119,7 @@ func GenerateHTML(graph *Graph) string { panic(err) } - return buf.String() + return buf.String(), nil } func getAllNodes(graph *Graph) []string { diff --git a/main.go b/main.go index be9daab..dd86b89 100644 --- a/main.go +++ b/main.go @@ -38,7 +38,7 @@ func main() { color.Green("✅ Graph data converted successfully.") - htmlContent := internal.GenerateHTML(result) + htmlContent, err := internal.GenerateHTML(result) if err != nil { color.Red("❌ Error generating HTML: %v", err) return From 17236ce3a28af481efcd8288458b626b62940368 Mon Sep 17 00:00:00 2001 From: Engin Date: Fri, 13 Sep 2024 16:08:32 +0300 Subject: [PATCH 4/5] Add flag to open in browser directly --- internal/browser.go | 28 ++++++++++++++++++++++++++++ main.go | 30 ++++++++++++++++++++++++++---- 2 files changed, 54 insertions(+), 4 deletions(-) create mode 100644 internal/browser.go diff --git a/internal/browser.go b/internal/browser.go new file mode 100644 index 0000000..9eb0642 --- /dev/null +++ b/internal/browser.go @@ -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() +} diff --git a/main.go b/main.go index dd86b89..366706d 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,7 @@ package main import ( "bytes" + "flag" "os" "os/exec" "strings" @@ -43,14 +44,35 @@ func main() { color.Red("❌ Error generating HTML: %v", err) return } - color.Green("✅ HTML content generated successfully.") - if err := os.WriteFile("dependency_tree.html", []byte(htmlContent), 0644); err != nil { - color.Red("❌ Error writing HTML file: %v", err) + openInBrowser := flag.Bool("open", false, "Open the temporary file in the default browser") + flag.Parse() + + if openInBrowser != nil && *openInBrowser { + color.Green("🔍 Opening HTML content in the default browser...") + tempFile, err := os.CreateTemp("", "dependency_tree_*.html") + if err != nil { + color.Red("❌ Error creating temporary file: %v", err) + return + } + if _, err := tempFile.Write([]byte(htmlContent)); err != nil { + color.Red("❌ Error writing to temporary file: %v", err) + return + } + if err := internal.OpenInBrowser(tempFile.Name()); err != nil { + color.Red("❌ Error opening HTML content in the browser: %v", err) + return + } + color.Green("✅ HTML content opened in the default browser.") return + } else { + if err := os.WriteFile("dependency_tree.html", []byte(htmlContent), 0644); err != nil { + color.Red("❌ Error writing HTML file: %v", err) + return + } + color.Green("✅ HTML file 'dependency_tree.html' written successfully.") } - color.Green("✅ HTML file 'dependency_tree.html' written successfully.") color.Cyan("🎉 modview completed successfully.") } From d9daf0b960bbd215d9bb7c0b1d5a669fef34751c Mon Sep 17 00:00:00 2001 From: Engin Date: Fri, 13 Sep 2024 16:15:09 +0300 Subject: [PATCH 5/5] Describe open flag into readme --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index 8cd17ad..11153e7 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,11 @@ your module's dependency graph. modview go mod graph dependency tree +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.