├── LICENSE ├── README.md ├── brownfox └── brownfox.go ├── cmd ├── lazydog └── lazydog.go ├── example ├── example_main.go └── expdir │ ├── a.go │ └── expdir2 │ ├── a.go │ ├── expdir2_1 │ └── a.go │ └── expdir3 │ ├── a.go │ └── expdir4 │ └── a.go ├── example2 ├── example_main.go └── expdir │ ├── a.go │ └── expdir2 │ ├── a.go │ ├── expdir2_1 │ └── a.go │ └── expdir3 │ ├── a.go │ └── expdir4 │ └── a.go ├── file ├── file.go └── file_test.go ├── go.mod ├── go.sum ├── inject ├── dogHelper.go ├── dogHelper_test.go ├── helper.go ├── injector.go ├── injector_test.go └── parser.go ├── main.go └── makefile /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 zhujiafeng 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Lazydog 2 | ====== 3 | 4 | ### Introduction 5 | 6 | Just one thing , inject golang source code and print where is the code now ! 7 | 8 | Sometimes, l need to read some opensource golang project like tidb, jaeger, consul or other things. However, l'm too stupid to follow the code, especially when interface{} flies ! 9 | 10 | ### Impl way 11 | 12 | l use golang standard lib to parse itself (A language can parse itself, amazing!), and inject what l want to do to the ast file.This is simple actually. 13 | 14 | ### Install 15 | 16 | ```shell 17 | make install 18 | ``` 19 | 20 | ### run test 21 | 22 | ```shell 23 | make jumptest 24 | make overtest 25 | ``` 26 | 27 | ### how to use 28 | 29 | Inject the source code in dir recursively; never mind what the code will look like, just run the fucking opensource project and read the log. 30 | 31 | ```shell 32 | lazydog jump -d src 33 | ``` 34 | 35 | And of course, lt can be restored 36 | 37 | ```shell 38 | lazydog over -d src 39 | ``` 40 | 41 | ### what the log contains 42 | 43 | ```text 44 | [lazydog][{{goroutine id}}] {{pkgname}}:{{lineNo}} caller={{function}} 45 | ``` -------------------------------------------------------------------------------- /brownfox/brownfox.go: -------------------------------------------------------------------------------- 1 | package brownfox 2 | 3 | import ( 4 | "bytes" 5 | "go/ast" 6 | "go/printer" 7 | "go/token" 8 | "io/ioutil" 9 | "os" 10 | 11 | "github.com/JodeZer/lazydog/file" 12 | "github.com/JodeZer/lazydog/inject" 13 | ) 14 | 15 | type BrownFox struct { 16 | path string 17 | dirs []string 18 | deepth int 19 | inject *inject.Injector 20 | file.Jumper 21 | } 22 | 23 | func NewBrownFox(path string, deepth int) *BrownFox { 24 | return &BrownFox{ 25 | path: path, 26 | deepth: deepth, 27 | inject: inject.NewInjector(), 28 | dirs: file.TreeDir(path, deepth), 29 | } 30 | } 31 | 32 | func (b *BrownFox) Backup() error { 33 | for _, dir := range b.dirs { 34 | b.BackupPath(dir) 35 | } 36 | return nil 37 | } 38 | 39 | func (b *BrownFox) Restore() error { 40 | for _, dir := range b.dirs { 41 | if err := b.RestorePath(dir); err != nil { 42 | return err 43 | } 44 | gofiles := file.ListGoFile(dir, false) 45 | if len(gofiles) == 0 { 46 | continue 47 | } 48 | parser := inject.NewParser(token.NewFileSet(), gofiles[0]) 49 | if err := parser.Parse(); err != nil { 50 | return err 51 | } 52 | dgHelper := inject.NewDogHelper(dir, parser.PkgName()) 53 | if err := dgHelper.EraseDogHelper(); err != nil && !os.IsNotExist(err) { 54 | return err 55 | } 56 | 57 | } 58 | 59 | return nil 60 | } 61 | 62 | func (b *BrownFox) Inject() error { 63 | for _, dir := range b.dirs { 64 | for _, gofile := range file.ListGoFile(dir, false) { 65 | 66 | fset := token.NewFileSet() 67 | parser := inject.NewParser(fset, gofile) 68 | err := parser.Parse() 69 | if err != nil { 70 | panic(err) 71 | } 72 | 73 | // inject 74 | parser.ForEachDecl(func(decl ast.Decl) { 75 | b.inject.InjectFunc(decl) 76 | }) 77 | 78 | // write to new file 79 | var buf bytes.Buffer 80 | printer.Fprint(&buf, fset, parser.GetAst()) 81 | //fmt.Println(buf.String()) 82 | if err := ioutil.WriteFile(gofile, buf.Bytes(), os.ModeExclusive); err != nil { 83 | panic(err) 84 | } 85 | 86 | // write helper 87 | dgHelper := inject.NewDogHelper(dir, parser.PkgName()) 88 | dgHelper.WriteDogHelper() 89 | } 90 | } 91 | 92 | return nil 93 | } 94 | -------------------------------------------------------------------------------- /cmd/lazydog: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JodeZer/lazydog/75916e0875341fde3288aabaf85bca254090fe45/cmd/lazydog -------------------------------------------------------------------------------- /cmd/lazydog.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | 7 | "github.com/JodeZer/lazydog/brownfox" 8 | "github.com/spf13/cobra" 9 | ) 10 | 11 | var Root = &cobra.Command{ 12 | Use: "lazydog", 13 | } 14 | 15 | var dir string 16 | 17 | var jump = &cobra.Command{ 18 | Use: "jump", 19 | Run: func(cmd *cobra.Command, args []string) { 20 | bf := brownfox.NewBrownFox(dir, -1) 21 | if err := bf.Backup(); err != nil { 22 | panic(err) 23 | } 24 | if err := bf.Inject(); err != nil { 25 | panic(err) 26 | } 27 | }, 28 | } 29 | 30 | var over = &cobra.Command{ 31 | Use: "over", 32 | Run: func(cmd *cobra.Command, args []string) { 33 | bf := brownfox.NewBrownFox(dir, -1) 34 | if err := bf.Restore(); err != nil { 35 | panic(err) 36 | } 37 | }, 38 | } 39 | 40 | func init() { 41 | jump.Flags().StringVarP(&dir, "dir", "d", "", "source code root dir") 42 | over.Flags().StringVarP(&dir, "dir", "d", "", "source code root dir") 43 | } 44 | func main() { 45 | Root.AddCommand(jump) 46 | Root.AddCommand(over) 47 | if err := Root.Execute(); err != nil { 48 | fmt.Println(err) 49 | os.Exit(-1) 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /example/example_main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "github.com/JodeZer/lazydog/example/expdir/expdir2" 5 | "github.com/JodeZer/lazydog/example/expdir/expdir2/expdir2_1" 6 | "github.com/JodeZer/lazydog/example/expdir/expdir2/expdir3" 7 | "github.com/JodeZer/lazydog/example/expdir/expdir2/expdir3/expdir4" 8 | "github.com/jodezer/lazydog/example/expdir" 9 | ) 10 | 11 | func main() { 12 | expdir.Foo() 13 | expdir2.Foo() 14 | expdir2_1.Foo() 15 | expdir3.Foo() 16 | expdir4.Foo() 17 | } 18 | -------------------------------------------------------------------------------- /example/expdir/a.go: -------------------------------------------------------------------------------- 1 | package expdir 2 | 3 | func Foo() { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /example/expdir/expdir2/a.go: -------------------------------------------------------------------------------- 1 | package expdir2 2 | 3 | func Foo() { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /example/expdir/expdir2/expdir2_1/a.go: -------------------------------------------------------------------------------- 1 | package expdir2_1 2 | 3 | func Foo() { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /example/expdir/expdir2/expdir3/a.go: -------------------------------------------------------------------------------- 1 | package expdir3 2 | 3 | func Foo() { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /example/expdir/expdir2/expdir3/expdir4/a.go: -------------------------------------------------------------------------------- 1 | package expdir4 2 | 3 | func Foo() { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /example2/example_main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "github.com/JodeZer/lazydog/example/expdir/expdir2" 5 | "github.com/JodeZer/lazydog/example/expdir/expdir2/expdir2_1" 6 | "github.com/JodeZer/lazydog/example/expdir/expdir2/expdir3" 7 | "github.com/JodeZer/lazydog/example/expdir/expdir2/expdir3/expdir4" 8 | "github.com/jodezer/lazydog/example/expdir" 9 | ) 10 | 11 | func main() { 12 | expdir.Foo() 13 | expdir2.Foo() 14 | expdir2_1.Foo() 15 | expdir3.Foo() 16 | expdir4.Foo() 17 | } 18 | -------------------------------------------------------------------------------- /example2/expdir/a.go: -------------------------------------------------------------------------------- 1 | package expdir 2 | 3 | func Foo() { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /example2/expdir/expdir2/a.go: -------------------------------------------------------------------------------- 1 | package expdir2 2 | 3 | func Foo() { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /example2/expdir/expdir2/expdir2_1/a.go: -------------------------------------------------------------------------------- 1 | package expdir2_1 2 | 3 | func Foo() { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /example2/expdir/expdir2/expdir3/a.go: -------------------------------------------------------------------------------- 1 | package expdir3 2 | 3 | func Foo() { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /example2/expdir/expdir2/expdir3/expdir4/a.go: -------------------------------------------------------------------------------- 1 | package expdir4 2 | 3 | func Foo() { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /file/file.go: -------------------------------------------------------------------------------- 1 | package file 2 | 3 | import ( 4 | "io/ioutil" 5 | "os" 6 | "strings" 7 | ) 8 | 9 | const BackupSuffix = ".ld" 10 | const HelperSuffix = "_lzdgen.go" 11 | 12 | //===== funcs 13 | 14 | func hiddenDir(path string) bool { // just tmp impl 15 | sps := strings.Split(path, `/`) 16 | if len(sps) == 0 { 17 | return true 18 | } 19 | last := sps[len(sps)-1] 20 | 21 | if len(last) == 0 { 22 | return true 23 | } 24 | 25 | if len(last) == 1 { 26 | return false 27 | } 28 | 29 | return last[0] == '.' && last[1] != '.' 30 | } 31 | 32 | func ListGoFile(path string, jumpBacked bool) []string { 33 | return listSuffixFile(path, []string{".go"}, jumpBacked, "_test.go", HelperSuffix) 34 | } 35 | 36 | func listSuffixFile(path string, include []string, jumpBacked bool, exclude ...string) []string { 37 | fis, err := ioutil.ReadDir(path) 38 | if err != nil { 39 | panic(err) 40 | } 41 | farray := []string{} 42 | for _, fi := range fis { 43 | if fi.IsDir() { 44 | continue 45 | } 46 | for _, exclu := range exclude { 47 | if strings.HasSuffix(fi.Name(), exclu) { 48 | continue 49 | } 50 | } 51 | 52 | // backup file exists 53 | if jumpBacked { 54 | if _, err := os.Stat(path + "/" + fi.Name() + BackupSuffix); !os.IsNotExist(err) { 55 | continue 56 | } 57 | } 58 | 59 | for _, inclu := range include { 60 | if strings.HasSuffix(fi.Name(), inclu) { 61 | farray = append(farray, path+"/"+fi.Name()) 62 | } 63 | } 64 | } 65 | return farray 66 | } 67 | 68 | func ListGoFileByPaths(paths []string, jumpBacked bool) []string { 69 | ret := []string{} 70 | for _, path := range paths { 71 | fs := ListGoFile(path, jumpBacked) 72 | ret = append(ret, fs...) 73 | } 74 | return ret 75 | } 76 | 77 | func TreeDir(path string, deepth int) []string { 78 | if hiddenDir(path) { 79 | return []string{} 80 | } 81 | paths := []string{path} 82 | fis, err := ioutil.ReadDir(path) 83 | if err != nil { 84 | panic(err) 85 | } 86 | for _, fi := range fis { 87 | if fi.IsDir() && deepth != 0 { 88 | nextDeepth := -1 89 | if deepth != -1 { 90 | nextDeepth = deepth - 1 91 | } 92 | paths = append(paths, TreeDir(path+"/"+fi.Name(), nextDeepth)...) 93 | } 94 | } 95 | 96 | return paths 97 | } 98 | 99 | func backupFileName(fileName string) string { 100 | sps := strings.Split(fileName, `/`) 101 | backupFileName := sps[len(sps)-1] + BackupSuffix 102 | sps[len(sps)-1] = backupFileName 103 | return strings.Join(sps, "/") 104 | } 105 | 106 | func restoreFileName(fileName string) string { 107 | sps := strings.Split(fileName, `/`) 108 | if !strings.HasSuffix(fileName, BackupSuffix) { 109 | return "" 110 | } 111 | restoreFileName := strings.Replace(sps[len(sps)-1], BackupSuffix, "", 1) 112 | sps[len(sps)-1] = restoreFileName 113 | return strings.Join(sps, "/") 114 | } 115 | 116 | func copyFile(src string, dst string) error { 117 | 118 | data, err := ioutil.ReadFile(src) 119 | if err != nil { 120 | return err 121 | } 122 | return ioutil.WriteFile(dst, data, 0644) 123 | } 124 | 125 | //=========== 126 | type Jumper struct { 127 | } 128 | 129 | func (j *Jumper) BackupPath(path string) error { 130 | files := ListGoFile(path, true) 131 | for _, fn := range files { 132 | if err := copyFile(fn, backupFileName(fn)); err != nil { 133 | return err 134 | } 135 | } 136 | return nil 137 | } 138 | 139 | func (j *Jumper) RestorePath(path string) error { 140 | files := listSuffixFile(path, []string{".go" + BackupSuffix}, false) 141 | for _, fn := range files { 142 | if err := copyFile(fn, restoreFileName(fn)); err != nil { 143 | return err 144 | } 145 | 146 | if err := os.Remove(fn); err != nil { 147 | return err 148 | } 149 | } 150 | return nil 151 | } 152 | -------------------------------------------------------------------------------- /file/file_test.go: -------------------------------------------------------------------------------- 1 | package file 2 | 3 | import "testing" 4 | 5 | func TestTreeDir(t *testing.T) { 6 | t.Logf("%+v", treeDir("/Users/ezbuy/Projects/ezbuy/goflow/src/github.com/JodeZer/lazydog", -1)) 7 | t.Logf("%+v", treeDir("/Users/ezbuy/Projects/ezbuy/goflow/src/github.com/JodeZer/lazydog", 2)) 8 | } 9 | 10 | func TestListFile(t *testing.T) { 11 | t.Logf("%+v", listGofileByPaths(treeDir("/Users/ezbuy/Projects/ezbuy/goflow/src/github.com/JodeZer/lazydog", -1))) 12 | } 13 | 14 | func TestJumperBackup(t *testing.T) { 15 | jp := &jumper{} 16 | jp.backupPath("../example") 17 | } 18 | 19 | func TestJumperRestore(t *testing.T) { 20 | jp := &jumper{} 21 | jp.restorePath("../example") 22 | } 23 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/JodeZer/lazydog 2 | 3 | require github.com/spf13/cobra v0.0.3 // indirect 4 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/spf13/cobra v0.0.3 h1:ZlrZ4XsMRm04Fr5pSFxBgfND2EBVa1nLpiy1stUsX/8= 2 | github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ= 3 | -------------------------------------------------------------------------------- /inject/dogHelper.go: -------------------------------------------------------------------------------- 1 | package inject // replacable 2 | import ( 3 | "fmt" 4 | "os" 5 | "runtime" 6 | "strings" 7 | ) 8 | 9 | var gopaths []string 10 | 11 | func init() { 12 | gopath := os.Getenv("GOPATH") 13 | gopaths = strings.Split(gopath, ";") 14 | for i := range gopaths { 15 | gopaths[i] += "/src/" 16 | } 17 | } 18 | 19 | // must only depend on standard 20 | // dont edit this file 21 | func __traceStack() { 22 | caller, file, line := __caller() 23 | fmt.Printf("[lazydog][%s] %s:%d caller= %s \n", __curGid(), __prettyFile(file), line, __prettyCaller(caller)) 24 | } 25 | 26 | // from https://stackoverflow.com/questions/35212985/is-it-possible-get-information-about-caller-function-in-golang 27 | func __caller() (string, string, int) { 28 | 29 | // we get the callers as uintptrs - but we just need 1 30 | fpcs := make([]uintptr, 1) 31 | 32 | // skip 3 levels to get to the caller of whoever called Caller() 33 | n := runtime.Callers(3, fpcs) 34 | if n == 0 { 35 | return "", "n/a", 0 // proper error her would be better 36 | } 37 | 38 | // get the info of the actual function that's in the pointer 39 | fun := runtime.FuncForPC(fpcs[0] - 1) 40 | if fun == nil { 41 | return "", "n/a", 0 42 | } 43 | 44 | file, line := fun.FileLine(0) 45 | // return its name 46 | return fun.Name(), file, line 47 | } 48 | 49 | func __prettyCaller(caller string) string { 50 | return string(string(caller[strings.LastIndex(caller, ".")+1:])) 51 | } 52 | 53 | func __prettyFile(file string) string { 54 | for _, gopath := range gopaths { 55 | if strings.Contains(file, gopath) { 56 | return strings.Replace(file, gopath, "", -1) 57 | } 58 | } 59 | return file 60 | } 61 | 62 | func __curGid() string { 63 | var buf [64]byte 64 | runtime.Stack(buf[:], false) 65 | return strings.Fields(strings.TrimPrefix(string(buf[:]), "goroutine "))[0] 66 | } 67 | -------------------------------------------------------------------------------- /inject/dogHelper_test.go: -------------------------------------------------------------------------------- 1 | package inject 2 | 3 | import "testing" 4 | 5 | func TestTrace(t *testing.T) { 6 | __traceStack() 7 | } 8 | 9 | // func TestWriteDogHelper(t *testing.T) { 10 | // WriteDogHelper("", "heisshei") 11 | // } 12 | 13 | func TestGetGid(t *testing.T) { 14 | t.Log(__curGid()) 15 | } 16 | -------------------------------------------------------------------------------- /inject/helper.go: -------------------------------------------------------------------------------- 1 | package inject 2 | 3 | import ( 4 | "bytes" 5 | "go/parser" 6 | "go/printer" 7 | "go/token" 8 | "io/ioutil" 9 | "os" 10 | pkgPath "path" 11 | "runtime" 12 | "strings" 13 | 14 | "github.com/JodeZer/lazydog/file" 15 | ) 16 | 17 | type DogHelper struct { 18 | path string 19 | pkg string 20 | } 21 | 22 | func NewDogHelper(path, pkg string) *DogHelper { 23 | return &DogHelper{ 24 | path: path, 25 | pkg: pkg, 26 | } 27 | } 28 | func (d *DogHelper) WriteDogHelper() { 29 | 30 | _, filename, _, _ := runtime.Caller(0) 31 | 32 | fset := token.NewFileSet() 33 | fbytes, err := ioutil.ReadFile(pkgPath.Join(pkgPath.Dir(filename)) + "/dogHelper.go") 34 | if err != nil { 35 | panic(err) 36 | } 37 | f, err := parser.ParseFile(fset, "dogHelper.go", fbytes, 0) 38 | if err != nil { 39 | panic(err) 40 | } 41 | 42 | f.Name.Name = d.pkg 43 | 44 | var buf bytes.Buffer 45 | printer.Fprint(&buf, fset, f) 46 | if err := ioutil.WriteFile(genPath(d.path, d.pkg), buf.Bytes(), os.ModePerm); err != nil { 47 | panic(err) 48 | } 49 | 50 | } 51 | 52 | func (d *DogHelper) EraseDogHelper() error { 53 | return os.Remove(genPath(d.path, d.pkg)) 54 | } 55 | 56 | func genPath(path, pkg string) string { 57 | suffix := "" 58 | if !strings.HasSuffix(path, "/") && len(path) != 0 { 59 | suffix = "/" 60 | } 61 | return path + suffix + "gen_" + pkg + file.HelperSuffix 62 | } 63 | -------------------------------------------------------------------------------- /inject/injector.go: -------------------------------------------------------------------------------- 1 | package inject 2 | 3 | import ( 4 | "fmt" 5 | "go/ast" 6 | "go/parser" 7 | "go/token" 8 | "io/ioutil" 9 | "strings" 10 | ) 11 | 12 | const INJECT = ` 13 | package main 14 | 15 | //import "fmt" 16 | 17 | func a(){ 18 | __traceStack() 19 | } 20 | ` 21 | 22 | type Injector struct { 23 | MyImport *ast.ImportSpec 24 | Mystate ast.Stmt 25 | } 26 | 27 | func (i *Injector) InjectFunc(f ast.Decl) error { 28 | 29 | fd, ok := f.(*ast.FuncDecl) 30 | if !ok { 31 | return fmt.Errorf("not func") 32 | } 33 | newList := make([]ast.Stmt, 0, len(fd.Body.List)+1) 34 | 35 | newList = append(newList, i.Mystate) 36 | 37 | newList = append(newList, fd.Body.List...) 38 | 39 | fd.Body.List = newList 40 | return nil 41 | } 42 | 43 | func (i *Injector) InjectFile(path string) error { 44 | fset := token.NewFileSet() 45 | fbytes, err := ioutil.ReadFile(path) 46 | index := strings.LastIndex(path, `/`) 47 | f, err := parser.ParseFile(fset, string(path[index+1:]), fbytes, 0) 48 | if err != nil { 49 | return err 50 | } 51 | 52 | for _, decl := range f.Decls { 53 | i.InjectFunc(decl) 54 | } 55 | 56 | return nil 57 | } 58 | 59 | func NewInjector() *Injector { 60 | i := &Injector{} 61 | fset := token.NewFileSet() 62 | f, err := parser.ParseFile(fset, "", INJECT, 0) 63 | if err != nil { 64 | panic(err) 65 | } 66 | 67 | for _, d := range f.Decls { 68 | if fd, ok := d.(*ast.FuncDecl); ok { 69 | i.Mystate = fd.Body.List[0] 70 | } 71 | } 72 | return i 73 | } 74 | -------------------------------------------------------------------------------- /inject/injector_test.go: -------------------------------------------------------------------------------- 1 | package inject 2 | 3 | import "testing" 4 | 5 | func TestNewInjector(t *testing.T) { 6 | _ = NewInjector() 7 | } 8 | -------------------------------------------------------------------------------- /inject/parser.go: -------------------------------------------------------------------------------- 1 | package inject 2 | 3 | import ( 4 | "go/ast" 5 | "go/parser" 6 | "go/token" 7 | "io/ioutil" 8 | "strings" 9 | ) 10 | 11 | type Parser struct { 12 | filename string 13 | astF *ast.File 14 | tokenFS *token.FileSet 15 | } 16 | 17 | func NewParser(tokenFS *token.FileSet, fn string) *Parser { 18 | return &Parser{ 19 | filename: fn, 20 | tokenFS: tokenFS, 21 | } 22 | } 23 | 24 | func (p *Parser) Parse() error { 25 | fbytes, err := ioutil.ReadFile(p.filename) 26 | index := strings.LastIndex(p.filename, `/`) 27 | f, err := parser.ParseFile(p.tokenFS, string(p.filename[index+1:]), fbytes, 0) 28 | if err != nil { 29 | return err 30 | } 31 | p.astF = f 32 | return nil 33 | } 34 | 35 | func (p *Parser) PkgName() string { 36 | return p.astF.Name.Name 37 | } 38 | 39 | func (p *Parser) ForEachDecl(f func(ast.Decl)) { 40 | for _, decl := range p.astF.Decls { 41 | f(decl) 42 | } 43 | } 44 | 45 | func (p *Parser) GetAst() *ast.File { 46 | return p.astF 47 | } 48 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "github.com/JodeZer/lazydog/brownfox" 5 | ) 6 | 7 | func main() { 8 | 9 | // inject.WriteDogHelper("example", "main") 10 | 11 | // fset := token.NewFileSet() 12 | // fbytes, err := ioutil.ReadFile("example/example_main.go") 13 | // f, err := parser.ParseFile(fset, "example_main.go", fbytes, 0) 14 | // if err != nil { 15 | // log.Fatal(err) // parse error 16 | // } 17 | 18 | // inj := inject.NewInjector() 19 | // for _, d := range f.Decls { 20 | 21 | // if fd, ok := d.(*ast.FuncDecl); ok { 22 | 23 | // inj.Inject(fd) 24 | 25 | // } 26 | // } 27 | 28 | // var buf bytes.Buffer 29 | // printer.Fprint(&buf, fset, f) 30 | // fmt.Println(buf.String()) 31 | // if err := ioutil.WriteFile("example/example_main.go", buf.Bytes(), os.ModeExclusive); err != nil { 32 | // panic(err) 33 | // } 34 | 35 | bf := brownfox.NewBrownFox("example", -1) 36 | // if err := bf.Backup(); err != nil { 37 | // panic(err) 38 | // } 39 | // if err := bf.Inject(); err != nil { 40 | // panic(err) 41 | // } 42 | if err := bf.Restore(); err != nil { 43 | panic(err) 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /makefile: -------------------------------------------------------------------------------- 1 | build: 2 | go build -o bin/lazydog cmd/lazydog.go 3 | 4 | install: build 5 | cp bin/lazydog /usr/local/bin 6 | 7 | jumptest: build 8 | ./bin/lazydog jump -d example 9 | go run example/*.go 10 | 11 | overtest: build 12 | ./bin/lazydog over -d example 13 | go run example/*.go 14 | --------------------------------------------------------------------------------