Skip to content

Instantly share code, notes, and snippets.

@holmeszyx
Created July 7, 2015 16:15
Show Gist options
  • Save holmeszyx/9c1d1d1a51a302d476d8 to your computer and use it in GitHub Desktop.
Save holmeszyx/9c1d1d1a51a302d476d8 to your computer and use it in GitHub Desktop.
Create a rebuild all libs sh script
package main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
const (
LIB_DIR = "src"
)
var (
LIB_LEN int = 0
)
func main() {
gopath := os.Getenv("GOPATH")
if gopath == "" {
fmt.Println("GOPATH not found")
os.Exit(1)
}
checkSrc := filepath.Join(gopath, LIB_DIR)
LIB_LEN = len(checkSrc) + 1
pkgs := make([]string, 0, 16)
checkDir(checkSrc, &pkgs)
//fmt.Println(pkgs)
outFile, err := os.Create("reinstall_all.sh")
if err != nil {
fmt.Println("can not open output file", "reinstall_all.sh")
return
}
defer outFile.Close()
for _, pkg := range pkgs {
outFile.WriteString(fmt.Sprintf("go install -a %s \n", pkg))
}
}
func checkDir(dir string, pkgs *[]string) {
//fmt.Println("check dir:", dir)
files, err := ioutil.ReadDir(dir)
if err != nil {
return
}
if len(files) == 0 {
output(dir[LIB_LEN:], pkgs)
return
}
if hasGo(files) {
output(dir[LIB_LEN:], pkgs)
return
}
for _, file := range files {
if file.IsDir() && strings.Index(file.Name(), ".") != 0 {
next := filepath.Join(dir, file.Name())
checkDir(next, pkgs)
}
}
return
}
func output(pkg string, pkgs *[]string) {
fmt.Println("go install -a", pkg)
if pkgs != nil {
*pkgs = append(*pkgs, pkg)
}
}
// 文件里面有没有.go的文件
func hasGo(files []os.FileInfo) bool {
for _, v := range files {
if !v.IsDir() && strings.LastIndex(v.Name(), ".go") != -1 {
return true
}
}
return false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment