Last active
February 28, 2016 05:39
-
-
Save andreinechaev/6587db27733e3607f26e to your computer and use it in GitHub Desktop.
1.5 => 1.6
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"io/ioutil" | |
"os" | |
"log" | |
"strings" | |
"sync" | |
) | |
var ( | |
files []string | |
wg sync.WaitGroup | |
) | |
const ( | |
LINE_TO_FIX = "github.com/c-darwin/dcoin-go/vendor/" | |
) | |
func parseDir(path string) ([]os.FileInfo, error) { | |
fInfo, err := ioutil.ReadDir(path) | |
if err != nil { | |
return nil, err | |
} | |
for _, f := range fInfo { | |
if strings.Contains(f.Name(), ".go") { | |
files = append(files, path + "/" + f.Name()) | |
} else if f.IsDir() { | |
fInfo, err = parseDir(path + "/" + f.Name()) | |
if err != nil { | |
panic(err) | |
} | |
} | |
} | |
return fInfo, err | |
} | |
func parseFileAt(path string) error { | |
b, err := ioutil.ReadFile(path) | |
if err != nil { | |
log.Fatalln(err) | |
return err | |
} | |
defer func() { | |
wg.Done() | |
}() | |
lines := strings.Split(string(b), "\n") | |
for i, l := range lines { | |
if strings.Contains(l, "{") { | |
break | |
} | |
lines[i] = fixImport(l, LINE_TO_FIX) | |
} | |
out := strings.Join(lines, "\n") | |
err = ioutil.WriteFile(path, []byte(out), 0644) | |
if err != nil { | |
panic(err) | |
} | |
return err | |
} | |
func fixImport(s, occr string) string { | |
if strings.Contains(s, occr) { | |
result := strings.Replace(s, occr, "", -1) | |
log.Println("\noriginal: " + s + | |
"\nresult: " + result + "\n") | |
return result | |
} | |
return s | |
} | |
func main() { | |
goPath := os.Getenv("GOPATH") | |
rootPath := goPath + "/src/github.com/c-darwin/dcoin-go" | |
_, err := parseDir(rootPath) | |
if err != nil { | |
panic(err) | |
} | |
wg.Add(len(files)) | |
for _, p := range files { | |
go parseFileAt(p) | |
} | |
wg.Wait() | |
log.Println("len files:", len(files)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment