summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--argv.go1
-rw-r--r--doWITCOM.go101
-rw-r--r--main.go5
3 files changed, 107 insertions, 0 deletions
diff --git a/argv.go b/argv.go
index b539c90..cbf6693 100644
--- a/argv.go
+++ b/argv.go
@@ -26,6 +26,7 @@ type args struct {
Verbose bool `arg:"--verbose" help:"be loud about it"`
Recursive bool `arg:"--recursive" help:"go-clone --recursive"`
Test bool `arg:"--test" help:"test build after everything else"`
+ WITCOM bool `arg:"--witcom" help:"add the GPL header"`
Bash bool `arg:"--bash" help:"generate bash completion"`
BashAuto []string `arg:"--auto-complete" help:"does the actual autocompletion"`
}
diff --git a/doWITCOM.go b/doWITCOM.go
new file mode 100644
index 0000000..221602a
--- /dev/null
+++ b/doWITCOM.go
@@ -0,0 +1,101 @@
+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)
+ }
+}
+
+// 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
+ }
+
+ pf, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
+ defer pf.Close()
+ if err != nil {
+ log.Info("file open error. permissions?", filename, err)
+ return err
+ }
+
+ var start bool = true
+ var found bool
+
+ // 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
+ continue
+ } else {
+ fmt.Fprintln(pf, line)
+ }
+ }
+
+ // dump every other comment
+ if strings.HasPrefix(line, "//") && found {
+ continue
+ } else {
+ fmt.Fprintln(pf, "// Copyright 2017-2025 WIT.COM Inc. All rights reserved.")
+ fmt.Fprintln(pf, "// Use of this source code is governed by the GPL 3.0")
+ fmt.Fprintln(pf, "")
+ fmt.Fprintln(pf, line)
+ found = false
+ }
+ fmt.Fprintln(pf, line)
+ }
+
+ 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
+}
diff --git a/main.go b/main.go
index 7c52b7d..430db25 100644
--- a/main.go
+++ b/main.go
@@ -54,6 +54,11 @@ func main() {
}
}
+ if argv.WITCOM {
+ doWITCOM()
+ okExit("")
+ }
+
if argv.Upgrade != nil {
doAptUpgrade()
}