1// Copyright (c) The Thanos Authors.
2// Licensed under the Apache License 2.0.
3
4package main
5
6import (
7	"bytes"
8	"io/ioutil"
9	"log"
10	"os"
11	"path/filepath"
12	"strings"
13)
14
15var (
16	// license compatible for Go and Proto files.
17	license = []byte(`// Copyright (c) The Thanos Authors.
18// Licensed under the Apache License 2.0.
19
20`)
21)
22
23func applyLicenseToProtoAndGo() error {
24	return filepath.Walk(".", func(path string, info os.FileInfo, err error) error {
25		if err != nil {
26			return err
27		}
28
29		// Filter out stuff that does not need copyright.
30		if info.IsDir() {
31			switch path {
32			case "vendor":
33				return filepath.SkipDir
34			}
35			return nil
36		}
37		if strings.HasSuffix(path, ".pb.go") {
38			return nil
39		}
40		if (filepath.Ext(path) != ".proto" && filepath.Ext(path) != ".go") ||
41			// We copied this file and we want maintain its license (MIT).
42			path == "pkg/testutil/testutil.go" ||
43			// Generated file.
44			path == "pkg/ui/bindata.go" {
45			return nil
46		}
47
48		b, err := ioutil.ReadFile(path)
49		if err != nil {
50			return err
51		}
52
53		if !strings.HasPrefix(string(b), string(license)) {
54			log.Println("file", path, "is missing Copyright header. Adding.")
55
56			var bb bytes.Buffer
57			_, _ = bb.Write(license)
58			_, _ = bb.Write(b)
59			if err = ioutil.WriteFile(path, bb.Bytes(), 0666); err != nil {
60				return err
61			}
62		}
63		return nil
64	})
65}
66
67func main() {
68	if err := applyLicenseToProtoAndGo(); err != nil {
69		log.Fatal(err)
70	}
71}
72