-
Notifications
You must be signed in to change notification settings - Fork 139
/
main.go
112 lines (100 loc) · 2.46 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
111
112
package main
import (
"fmt"
"os"
"time"
"github.com/rodrigo-brito/gocity/pkg/lib"
"github.com/rodrigo-brito/gocity/pkg/server"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)
const (
defaultPort = 4000
)
func main() {
log.SetLevel(log.InfoLevel)
app := cli.NewApp()
app.Version = "1.0.6"
app.Description = "Code City metaphor for visualizing Go source code in 3D"
app.Copyright = "Rodrigo Brito (https://github.com/rodrigo-brito)"
app.Commands = []*cli.Command{
{
Name: "server",
Description: "Start a local server to analyze projects",
Flags: []cli.Flag{
&cli.IntFlag{
Name: "port",
Aliases: []string{"b"},
Value: defaultPort,
Usage: "Local server port",
EnvVars: []string{"PORT"},
},
&cli.DurationFlag{
Name: "cache",
Aliases: []string{"c"},
Value: time.Hour,
Usage: "Cache's, TTL e.g.: --cache 4h",
EnvVars: []string{"CACHE_TTL"},
},
},
Action: func(c *cli.Context) error {
analyzer := server.AnalyzerHandle{
Cache: lib.NewCache(),
TmpFolder: os.TempDir(),
CacheTTL: c.Duration("cache"),
Port: c.Int("port"),
}
return analyzer.Serve()
},
},
{
Name: "open",
Description: "Open a given project in local server",
Flags: []cli.Flag{
&cli.IntFlag{
Name: "port",
Aliases: []string{"p"},
Value: defaultPort,
Usage: "Local server port",
EnvVars: []string{"PORT"},
},
&cli.StringFlag{
Name: "branch",
Aliases: []string{"b"},
Value: "master",
Usage: "Specify a custom branch",
},
},
Action: func(c *cli.Context) error {
var local bool
projectAddress := c.Args().First()
stat, err := os.Stat(projectAddress)
if os.IsNotExist(err) {
url, ok := lib.GetGithubBaseURL(c.Args().First())
if !ok {
return fmt.Errorf("project path not found")
}
projectAddress = url
} else if !stat.IsDir() || projectAddress == "" {
return fmt.Errorf("invalid project path")
} else {
local = true
}
analyzer := server.AnalyzerHandle{
Cache: lib.NewCache(),
TmpFolder: os.TempDir(),
Port: c.Int("port"),
ProjectPath: &projectAddress,
Local: local,
}
if branch := c.String("branch"); branch != "" {
analyzer.Branch = &branch
}
return analyzer.Serve()
},
},
}
if err := app.Run(os.Args); err != nil {
log.Error(err)
}
}