Spaces:
Running
Running
File size: 3,090 Bytes
b110593 |
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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
// _ _
// __ _____ __ ___ ___ __ _| |_ ___
// \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
// \ V V / __/ (_| |\ V /| | (_| | || __/
// \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
//
// Copyright Β© 2016 - 2024 Weaviate B.V. All rights reserved.
//
// CONTACT: [email protected]
//
package main
import (
"bytes"
"fmt"
"log"
"os"
"regexp"
"strings"
"github.com/bmatcuk/doublestar"
)
var (
headerSectionRe *regexp.Regexp
buildTagRe *regexp.Regexp
targetHeader []byte
)
func init() {
headerSectionRe = regexp.MustCompile(`^(//.*\n)*\n`)
buildTagRe = regexp.MustCompile(`^//go:build`)
h, err := os.ReadFile("tools/license_headers/header.txt")
fatal(err)
targetHeader = bytes.TrimSpace(h)
}
func main() {
fileNames, err := doublestar.Glob("**/*.go")
fatal(err)
for _, name := range fileNames {
if len := len(name); len > 5 && name[len-6:] == ".pb.go" || strings.HasPrefix(name, "vendor/") {
continue
}
fatal(processSingleFile(name))
}
}
func processSingleFile(name string) error {
bytes, err := os.ReadFile(name)
if err != nil {
return fmt.Errorf("%s: %v", name, err)
}
if hasNoHeader(bytes, name) {
err := extendWithHeader(name, bytes)
if err != nil {
return fmt.Errorf("%s: %v", name, err)
}
fmt.Printf("ποΈ successfully created header: %s\n", name)
return nil
}
if headerNeedsUpdate(bytes) {
err := updateHeader(name, bytes)
if err != nil {
return fmt.Errorf("%s: %v", name, err)
}
fmt.Printf("π· successfully updated: %s\n", name)
} else {
fmt.Printf("β
already up to date: %s\n", name)
}
return nil
}
func headerNeedsUpdate(content []byte) bool {
current := headerSectionRe.Find(content)
return !bytes.Equal(bytes.TrimSpace(current), targetHeader)
}
func extendWithHeader(name string, content []byte) error {
target := append(targetHeader, []byte("\n\n")...)
updated := append(target, content...)
return os.WriteFile(name, updated, 0)
}
func updateHeader(name string, content []byte) error {
if startsWithBuildTag(content) {
// the document starts with a build tag, this means we don't replace, but
// insert the header at pos 0
return extendWithHeader(name, content)
}
// append a newline so we consistently have one newline regardless of the
// input doc
target := append(targetHeader, []byte("\n\n")...)
replaced := headerSectionRe.ReplaceAll(content, target)
return os.WriteFile(name, replaced, 0)
}
func hasNoHeader(content []byte, name string) bool {
h := headerSectionRe.Find(content)
if h == nil {
// there is no comment section at all
return true
}
lines := bytes.Split(h, []byte("\n"))
// this comment is so short, this is most likely not a header section,
// let's add a header in front of it instead
return len(lines) < 4
}
func startsWithBuildTag(content []byte) bool {
return buildTagRe.Match(content)
}
func fatal(err error) {
if err != nil {
log.Fatalf(err.Error())
}
}
|