-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
91 lines (80 loc) · 2.43 KB
/
handlers.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
package main
import (
"encoding/json"
"errors"
"fmt"
"go/ast"
"html"
"html/template"
"os"
"strings"
"github.com/Masterminds/sprig"
"github.com/urfave/cli/v2"
"github.com/wilhelm-murdoch/go-collection"
"github.com/wilhelm-murdoch/go-gadget"
)
// actionRootHandler is responsible for running the root command for the cli.
func actionRootHandler(c *cli.Context) error {
// Initialise the collection responsible for collating any discovered top-
// level go packages.
packages := collection.New[*gadget.Package]()
// Recursively walk the directory tree as specified by the `--source` flag. If
// the flag points to a valid go file, begin parsing that instead.
files := gadget.WalkGoFiles(c.String("source"))
if len(files) == 0 {
return errors.New("could not find any matching files ending in *.go")
}
// Start iterating through any discovered go files and start parsing.
for _, path := range files {
f, err := gadget.NewFile(path)
if err != nil {
return err
}
// Upsert any newly-discovered packages into the packages collection. Add
// the current file to the package's `Files` sub-collection and move on.
p := packages.Find(func(i int, p *gadget.Package) bool {
return p.Name == f.Package
})
if p != nil {
p.Files.Push(f)
continue
}
// This is a new package, so let's add it to the packages collection and
// assign the current file to the `Files` sub-collection and move on.
p = gadget.NewPackage(f.Package)
p.Files.Push(f)
packages.Push(p)
}
// This cli supports three output modes: debug ( using spew ), JSON and
// template support using go's `html/template` package in addition to sprig's
// set of template functions using `sprig.FuncMap()`.
switch c.String("format") {
default:
fallthrough
case "json":
encoder := json.NewEncoder(os.Stdout)
if err := encoder.Encode(packages.Items()); err != nil {
return err
}
case "debug":
packages.Each(func(i int, p *gadget.Package) bool {
p.Files.Each(func(i int, f *gadget.File) bool {
astFile, tokenSet := f.GetAstAttributes()
ast.Print(tokenSet, astFile)
return false
})
return false
})
case "template":
tpl, err := template.New(c.String("template")).Funcs(sprig.FuncMap()).ParseFiles(c.String("template"))
if err != nil {
return err
}
var buffer strings.Builder
if err := tpl.Execute(&buffer, packages.Items()); err != nil {
return err
}
fmt.Println(html.UnescapeString(buffer.String()))
}
return nil
}