Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "bytes"
- "io/ioutil"
- "log"
- "os"
- "path/filepath"
- "regexp"
- "strings"
- )
- var checker = regexp.MustCompile(`^// Fill out[^\r\n]*`)
- var checkerForNew = regexp.MustCompile(`^// Copyright 2014[^\r\n]*`)
- var newCopyright = []byte(`// Copyright 2014-2018 9M Interactive, Inc. All Rights Reserved.`) // modify this line to what you desire
- var bom = "\xef\xbb\xbf"
- func main() {
- dir := os.Args[1]
- log.Println("path", dir)
- err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
- if info.IsDir() {
- return nil
- }
- ext := filepath.Ext(info.Name())
- if ext != ".cpp" && ext != ".h" && ext != ".cs" {
- return nil
- }
- fileContent, err := ioutil.ReadFile(path)
- if err != nil {
- log.Println(err)
- return nil
- }
- // trim bom
- fileContent = bytes.TrimLeft(fileContent, bom)
- var newContent []byte
- if idx := checker.FindIndex(fileContent); idx != nil {
- // has copyright notice
- log.Println("has copyright")
- newContent = checker.ReplaceAll(fileContent, newCopyright)
- } else if !checkerForNew.Match(fileContent) {
- // no copyright notice
- log.Println("no copyright")
- var builder strings.Builder
- builder.Write(newCopyright)
- builder.Write([]byte("\r\n\r\n"))
- builder.Write(fileContent)
- newContent = []byte(builder.String())
- }
- if newContent != nil {
- if err := ioutil.WriteFile(path, newContent, 0644); err != nil {
- log.Println("overwrite error:", err)
- }
- log.Println("fixed", path)
- } else {
- log.Println("skipping", path)
- }
- return nil
- })
- log.Println("done", err)
- }
Add Comment
Please, Sign In to add comment