-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
130 lines (119 loc) · 3.41 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package main
import (
"bytes"
"fmt"
"go/ast"
"go/parser"
"go/token"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"strings"
"github.com/spf13/cobra"
"github.com/tamayika/gaq/pkg/gaq"
"github.com/tamayika/gaq/pkg/gaq/query"
)
var version = "dev"
func printText(source []byte, fset *token.FileSet, nodes []ast.Node) {
for _, node := range nodes {
pos := fset.Position(node.Pos())
end := fset.Position(node.End())
fmt.Println(string(source[pos.Offset:end.Offset]))
}
}
func printPos(nodes []ast.Node) {
for _, node := range nodes {
fmt.Printf("%d,%d\n", node.Pos(), node.End())
}
}
func replaceByCommand(source []byte, fset *token.FileSet, nodes []ast.Node, commands []string) []byte {
ret := []byte{}
var lastNode ast.Node
for _, node := range nodes {
pos := fset.Position(node.Pos())
end := fset.Position(node.End())
nodeText := source[pos.Offset:end.Offset]
var stderr bytes.Buffer
cmd := exec.Command(commands[0], commands[1:]...)
cmd.Stderr = &stderr
stdin, err := cmd.StdinPipe()
if err != nil {
log.Fatalf("Cannot get stdin pipe. %v", err)
}
io.WriteString(stdin, string(nodeText))
stdin.Close()
replacedText, err := cmd.Output()
if err != nil {
log.Fatalf("Command failed.\nerr: %v\nstderr: %s\nnodeText: %s", err, strings.TrimSuffix(string(stderr.String()), "\n"), string(nodeText))
}
if lastNode == nil {
ret = append(ret, source[:pos.Offset]...)
} else {
ret = append(ret, source[fset.Position(lastNode.End()).Offset:pos.Offset]...)
}
ret = append(ret, replacedText...)
lastNode = node
}
if lastNode == nil {
ret = source
} else {
ret = append(ret, source[fset.Position(lastNode.End()).Offset:]...)
}
return ret
}
func main() {
var format string
var mode string
rootCmd := &cobra.Command{
Use: "gaq <Query>",
Short: "gaq is the cli tool to query ast node. STDIN needed as go code.",
Long: `gaq is the cli tool to query ast node.
Typical usage is
cat <go file path> | gaq <Query>
cat <go file path> | gaq -m replace <Query> <Replace command>
Please see details at https://github.com/tamayika/gaq`,
Args: cobra.MinimumNArgs(1),
Version: version,
Run: func(cmd *cobra.Command, args []string) {
data, err := ioutil.ReadAll(os.Stdin)
if err != nil {
log.Fatalf("Cannot read data from stdin. %v", err)
}
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, "", string(data), parser.ParseComments)
if err != nil {
log.Fatalf("Cannot parse source. %v", err)
}
node := gaq.MustParseNode(f)
q := query.MustParse(args[0])
nodes := node.QuerySelectorAll(q)
switch mode {
case "filter":
switch format {
case "text":
printText(data, fset, nodes)
case "pos":
printPos(nodes)
default:
log.Fatalf("Format: %s is not supported.", format)
}
case "replace":
if len(args) < 2 {
log.Fatalf("One or more command and args are expected in replace mode.")
}
fmt.Println(string(replaceByCommand(data, fset, nodes, args[1:])))
default:
log.Fatalf("Mode: %s is not supported.", mode)
}
},
}
rootCmd.PersistentFlags().StringVarP(&format, "format", "f", "text", "Output format, 'text' or 'pos'. Default is 'text'")
rootCmd.PersistentFlags().StringVarP(&mode, "mode", "m", "filter", "Execution mode, 'filter' or 'replace'. Default is 'filter'")
rootCmd.SetVersionTemplate(`{{printf "%s" .Version}}`)
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}