wit-test/doWITCOM.go

117 lines
2.2 KiB
Go
Raw Normal View History

2025-02-01 11:38:22 -06:00
// Copyright 2017-2025 WIT.COM Inc. All rights reserved.
// Use of this source code is governed by the GPL 3.0
2025-02-01 11:22:46 -06:00
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"go.wit.com/log"
)
func doWITCOM() {
pwd, err := os.Getwd()
if err != nil {
return
}
files, err := scanForGoFiles(pwd)
if err != nil {
return
}
for _, file := range files {
addCommonHeader(file)
}
}
2025-02-01 11:38:22 -06:00
// add a common header for WIT files
2025-02-01 11:22:46 -06:00
// add a common header for WIT files
func addCommonHeader(filename string) error {
// read in the .proto file
data, err := os.ReadFile(filename)
if err != nil {
log.Info("file read failed", filename, err)
return err
}
2025-02-01 11:38:22 -06:00
var newfile string
2025-02-01 11:22:46 -06:00
var start bool = true
var found bool
2025-02-01 11:26:07 -06:00
var done bool
2025-02-01 11:22:46 -06:00
// drop the old copywrite lines
for _, line := range strings.Split(string(data), "\n") {
// first line must have WIT.COM
if strings.HasPrefix(line, "//") && start {
start = false
if strings.Contains(line, "WIT.COM") {
found = true
} else {
2025-02-01 11:38:22 -06:00
newfile += fmt.Sprintln(line)
2025-02-01 11:22:46 -06:00
}
2025-02-01 11:38:22 -06:00
continue
2025-02-01 11:22:46 -06:00
}
// dump every other comment
if strings.HasPrefix(line, "//") && found {
continue
} else {
2025-02-01 11:26:07 -06:00
found = false
}
2025-02-01 11:38:22 -06:00
// print the header once
2025-02-01 11:26:07 -06:00
if !done {
2025-02-01 11:38:22 -06:00
newfile += fmt.Sprintln("// Copyright 2017-2025 WIT.COM Inc. All rights reserved.")
newfile += fmt.Sprintln("// Use of this source code is governed by the GPL 3.0")
newfile += fmt.Sprintln("")
2025-02-01 11:26:07 -06:00
done = true
2025-02-01 11:22:46 -06:00
}
2025-02-01 11:38:22 -06:00
newfile += fmt.Sprintln(line)
2025-02-01 11:22:46 -06:00
}
2025-02-01 11:38:22 -06:00
pf, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
log.Info("file open error. permissions?", filename, err)
return err
}
// trim trailing empty lines from the new file
newfile = strings.TrimSpace(newfile)
fmt.Fprintln(pf, newfile)
pf.Close()
2025-02-01 11:22:46 -06:00
return nil
}
// look for any .go files. do not enter directories
func scanForGoFiles(startDir string) ([]string, error) {
var files []string
err := filepath.Walk(startDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
// ignore the start dir
if startDir == path {
return nil
}
if strings.HasSuffix(path, ".go") {
files = append(files, path)
}
// don't go into any directories
if info.IsDir() {
return filepath.SkipDir
}
return nil
})
return files, err
}