Skip to content

Instantly share code, notes, and snippets.

@swarley
Created February 8, 2013 03:06
Show Gist options
  • Save swarley/4736308 to your computer and use it in GitHub Desktop.
Save swarley/4736308 to your computer and use it in GitHub Desktop.
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
type Template struct {
content string
length int
}
var template Template
func add_header(path, content string) {
file, _ := os.OpenFile(path, os.O_RDWR, 0777)
file.WriteAt([]byte(template.content+content), 0)
file.Close()
}
func walk_directory(path string, f os.FileInfo, err error) error {
if !strings.HasSuffix(path, ".go") {
return nil
}
obytes, _ := ioutil.ReadFile(path)
output := string(obytes)
if template.length > len(output) {
add_header(path, output)
fmt.Printf("Modified file %s.", path)
return nil
}
if output[0:template.length] != template.content {
return nil
}
return nil
}
func mkpath(entity string) string {
wd, _ := os.Getwd()
return strings.Join([]string{wd, entity}, string(os.PathSeparator))
}
func main() {
btemp, _ := ioutil.ReadFile(mkpath(".template.go"))
template = Template{content: string(btemp), length: len(string(btemp))}
flag.Parse()
if flag.Arg(0) == "" {
return
} else {
err := filepath.Walk(mkpath(flag.Arg(0)), walk_directory)
if err != nil {
fmt.Printf("filepath.Walk() returned %v\n", err)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment