-
Notifications
You must be signed in to change notification settings - Fork 64
/
main.go
145 lines (128 loc) · 3.38 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package main
import (
"context"
"flag"
"fmt"
"log"
"os"
"os/signal"
"path/filepath"
"strings"
"github.com/d4l3k/go-pry/generate"
"github.com/pkg/errors"
)
func main() {
log.SetFlags(log.Flags() | log.Lshortfile)
// Catch Ctrl-C
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
for _ = range c {
}
}()
if err := run(); err != nil {
log.Fatalf("%+v", err)
}
}
func run() error {
ctx := context.Background()
// FLAGS
imports := flag.String("i", "fmt,math", "packages to import, comma seperated")
revert := flag.Bool("r", true, "whether to revert changes on exit")
execute := flag.String("e", "", "statements to execute")
generatePath := flag.String("generate", "", "the path to generate a go-pry injected file - EXPERIMENTAL")
debug := flag.Bool("d", false, "display debug statements")
flag.CommandLine.Usage = func() {
if err := generate.NewGenerator(*debug).ExecuteGoCmd(ctx, []string{}, nil); err != nil {
log.Fatal(err)
}
fmt.Println("----")
fmt.Println("go-pry is an interactive REPL and wrapper around the go command.")
fmt.Println("You can execute go commands as normal and go-pry will take care of generating the pry code.")
fmt.Println("Running go-pry with no arguments will drop you into an interactive REPL.")
flag.PrintDefaults()
fmt.Println(" revert: cleans up go-pry generated files if not automatically done")
}
flag.Parse()
g := generate.NewGenerator(*debug)
cmdArgs := flag.Args()
if len(cmdArgs) == 0 {
imports := strings.Split(*imports, ",")
if len(*generatePath) > 0 {
return g.GenerateFile(imports, *execute, *generatePath)
}
return g.GenerateAndExecuteFile(ctx, imports, *execute)
}
goDirs := []string{}
for _, arg := range cmdArgs {
if strings.HasSuffix(arg, ".go") {
goDirs = append(goDirs, filepath.Dir(arg))
}
}
if len(goDirs) == 0 {
dir, err := os.Getwd()
if err != nil {
panic(err)
}
goDirs = []string{dir}
}
processedFiles := []string{}
modifiedFiles := []string{}
if cmdArgs[0] == "revert" {
fmt.Println("REVERTING PRY")
for _, dir := range goDirs {
filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if strings.HasSuffix(path, ".gopry") {
processed := false
for _, file := range processedFiles {
if file == path {
processed = true
}
}
if !processed {
base := filepath.Base(path)
newPath := filepath.Dir(path) + "/" + base[1:len(base)-3]
modifiedFiles = append(modifiedFiles, newPath)
}
}
return nil
})
}
return g.RevertPry(modifiedFiles)
}
testsRequired := cmdArgs[0] == "test"
for _, dir := range goDirs {
if err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if !testsRequired && strings.HasSuffix(path, "_test.go") || !strings.HasSuffix(path, ".go") || strings.Contains(path, "vendor/") {
return nil
}
for _, file := range processedFiles {
if file == path {
return nil
}
}
file, err := g.InjectPry(path)
if err != nil {
return errors.Wrap(err, "inject")
}
if file != "" {
modifiedFiles = append(modifiedFiles, path)
}
return nil
}); err != nil {
return err
}
}
if cmdArgs[0] == "apply" {
return nil
}
if err := g.ExecuteGoCmd(ctx, cmdArgs, nil); err != nil {
return err
}
if *revert {
if err := g.RevertPry(modifiedFiles); err != nil {
return err
}
}
return nil
}