-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
110 lines (95 loc) · 2.77 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package main
import (
"embed"
"fmt"
"github.com/ismailbayram/bigpicture/internal/browser"
"github.com/ismailbayram/bigpicture/internal/config"
"github.com/ismailbayram/bigpicture/internal/graph"
"github.com/ismailbayram/bigpicture/internal/server"
"github.com/ismailbayram/bigpicture/internal/validators"
"os"
)
//go:embed web/*
var staticFiles embed.FS
type BigPicture struct {
cfg *config.Configuration
tree *graph.Tree
}
func (bp *BigPicture) Validate() error {
for _, validatorConf := range bp.cfg.Validators {
validator, err := validators.NewValidator(validatorConf.Type, validatorConf.Args, bp.tree)
if err != nil {
return err
}
if err := validator.Validate(); err != nil {
return err
}
}
return nil
}
func main() {
bp := BigPicture{
cfg: config.Init(),
tree: graph.NewTree("root"),
}
lang := detectLanguage()
if bp.cfg.Lang != "" {
checkSupportedLang(bp.cfg.Lang)
lang = bp.cfg.Lang
}
if lang == "" && bp.cfg.Lang == "" {
fmt.Printf("Could not detect the project language. Please run the command in the root directory of the project"+
" or you can set `lang` parameter in .bigpicture.json file.\n"+
"%s\n", supportedLangs())
os.Exit(1)
}
brow := browser.NewBrowser(lang, bp.tree, bp.cfg.IgnoredPaths, bp.cfg.RootDir)
brow.Browse(".")
bp.tree.GenerateLinks()
bp.tree.CalculateInstability()
switch os.Args[1] {
case "server":
server.RunServer(staticFiles, bp.cfg.Port, bp.tree.ToJSON())
case "validate":
if err := bp.Validate(); err != nil {
fmt.Println("validation failed")
fmt.Println(err)
os.Exit(1)
}
case "help":
printHelp()
default:
fmt.Println("Invalid command. Use 'bigpicture help' to see available commands.")
os.Exit(1)
}
}
func printHelp() {
fmt.Println("Usage:")
fmt.Println("server")
fmt.Println("\tRuns the web server on port which is defined in .big.picture.json file. Default port is 44525.")
fmt.Println("\nvalidate")
fmt.Println("\tValidates the project structure. It checks if the project structure is valid according to the validators which are defined in .big.picture.json file.")
fmt.Println("\nhelp")
fmt.Println("\tPrints this help message.")
}
func detectLanguage() string {
if _, err := os.Stat("go.mod"); !os.IsNotExist(err) {
return browser.LangGo
}
if _, err := os.Stat("requirements.txt"); !os.IsNotExist(err) {
return browser.LangPy
}
if _, err := os.Stat("pom.xml"); !os.IsNotExist(err) {
return browser.LangJava
}
return ""
}
func checkSupportedLang(lang string) {
if lang != browser.LangGo && lang != browser.LangPy && lang != browser.LangJava {
fmt.Printf("Unsupported language: %s\n%s", lang, supportedLangs())
os.Exit(1)
}
}
func supportedLangs() string {
return fmt.Sprintf("Supported languages: %s, %s, %s\n", browser.LangGo, browser.LangPy, browser.LangJava)
}