-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.go
85 lines (71 loc) · 2.19 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package main
import (
"bytes"
"flag"
"os"
"os/exec"
"strings"
"github.com/bayraktugrul/modview/internal"
"github.com/fatih/color"
)
func main() {
color.Cyan("🚀 Starting modview...")
logo := `
__ ___ __ _
/ |/ /__ ___/ / __(_)__ _ __
/ /|_/ / _ \/ _ / |/ / / -_) |/|/ /
/_/ /_/\___/\_,_/|___/_/\__/|__,__/`
color.Cyan(logo)
cmd := exec.Command("go", "mod", "graph")
var out bytes.Buffer
cmd.Stdout = &out
if err := cmd.Run(); err != nil {
color.Red("❌ Error running 'go mod graph': %v", err)
return
}
color.Green("✅ 'go mod graph' command executed successfully.")
goModPath := "go.mod"
if _, err := os.Stat(goModPath); os.IsNotExist(err) {
color.Red("❌ 'go.mod' file not found in the current directory.")
return
}
result, err := internal.Convert(strings.NewReader(out.String()), goModPath)
if err != nil {
color.Red("❌ Error converting graph data: %v", err)
return
}
color.Green("✅ Graph data converted successfully.")
htmlContent, err := internal.GenerateHTML(result)
if err != nil {
color.Red("❌ Error generating HTML: %v", err)
return
}
color.Green("✅ HTML content generated successfully.")
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.Cyan("🎉 modview completed successfully.")
}