summaryrefslogtreecommitdiff
path: root/doWITCOM.go
blob: 83b2662418059d7889091e2caef9c871e807ce74 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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
	var done 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 {
			found = false
		}
		if !done {
			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)
			done = true
		}
		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
}