-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
66 lines (58 loc) · 1.42 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
package main
import (
"code-complexity/calculate"
"code-complexity/options"
"encoding/json"
"fmt"
"log"
"os"
"github.com/urfave/cli/v2"
)
const VERSION = "1.0.8"
func main() {
cli.AppHelpTemplate =
`NAME:
{{.Name}} - {{.Version}} - {{.Usage}}
USAGE:
{{.Name}}{{range .Flags}}{{if and (not (eq .Name "help")) (not (eq .Name "version")) }} {{if .Required}}--{{.Name}} value{{end}}{{end}}{{end}} [optional flags]
OPTIONS:
{{range .Flags}}{{.}}
{{end}}
`
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
log.SetOutput(os.Stderr)
app := &cli.App{
Name: "complexity",
Usage: "Estimate source code complexity",
Flags: options.Flags,
Version: VERSION,
Action: func(ctx *cli.Context) error {
opts, err := options.ParseOptions(ctx)
if err != nil {
return err
}
summary, err := calculate.Complexity(opts)
if err != nil {
return err
}
asJson, err := json.MarshalIndent(summary, "", " ")
if err != nil {
return fmt.Errorf("failed to serialize summary to json: %v", err)
}
log.Printf("completed successfully at %v", opts.CodePath)
println(string(asJson))
if len(opts.OutputPath) > 0 {
err = os.WriteFile(opts.OutputPath, asJson, 0777)
if err != nil {
return fmt.Errorf("failed to write output to %v: %v", opts.OutputPath, err)
}
}
return nil
},
}
err := app.Run(os.Args)
if err != nil {
log.Printf("failed: %v", err)
os.Exit(1)
}
}