|
| 1 | +// mongodb_exporter |
| 2 | +// Copyright (C) 2017 Percona LLC |
| 3 | +// |
| 4 | +// This program is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU Affero General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | +// |
| 9 | +// This program is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU Affero General Public License for more details. |
| 13 | +// |
| 14 | +// You should have received a copy of the GNU Affero General Public License |
| 15 | +// along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +//go:build ignore |
| 18 | +// +build ignore |
| 19 | + |
| 20 | +// check-license checks that AGPL license header in all files matches header in this file. |
| 21 | +package main |
| 22 | + |
| 23 | +import ( |
| 24 | + "flag" |
| 25 | + "fmt" |
| 26 | + "io" |
| 27 | + "log" |
| 28 | + "os" |
| 29 | + "path/filepath" |
| 30 | + "regexp" |
| 31 | +) |
| 32 | + |
| 33 | +var ( |
| 34 | + generatedHeader = regexp.MustCompile(`^// Code generated .* DO NOT EDIT\.`) |
| 35 | + |
| 36 | + copyrightText = `// mongodb_exporter |
| 37 | +// Copyright (C) 2022 Percona LLC |
| 38 | +// |
| 39 | +// This program is free software: you can redistribute it and/or modify |
| 40 | +// it under the terms of the GNU Affero General Public License as published by |
| 41 | +// the Free Software Foundation, either version 3 of the License, or |
| 42 | +// (at your option) any later version. |
| 43 | +// |
| 44 | +// This program is distributed in the hope that it will be useful, |
| 45 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 46 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 47 | +// GNU Affero General Public License for more details. |
| 48 | +// |
| 49 | +// You should have received a copy of the GNU Affero General Public License |
| 50 | +// along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 51 | +` |
| 52 | + |
| 53 | + copyrightPattern = regexp.MustCompile(`^// mongodb_exporter |
| 54 | +// Copyright \(C\) 20\d{2} Percona LLC |
| 55 | +// |
| 56 | +// This program is free software: you can redistribute it and/or modify |
| 57 | +// it under the terms of the GNU Affero General Public License as published by |
| 58 | +// the Free Software Foundation, either version 3 of the License, or |
| 59 | +// \(at your option\) any later version. |
| 60 | +// |
| 61 | +// This program is distributed in the hope that it will be useful, |
| 62 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 63 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 64 | +// GNU Affero General Public License for more details. |
| 65 | +// |
| 66 | +// You should have received a copy of the GNU Affero General Public License |
| 67 | +// along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 68 | +`) |
| 69 | +) |
| 70 | + |
| 71 | +func checkHeader(path string) bool { |
| 72 | + f, err := os.Open(path) |
| 73 | + if err != nil { |
| 74 | + log.Fatal(err) |
| 75 | + } |
| 76 | + defer f.Close() |
| 77 | + |
| 78 | + actual := make([]byte, len(copyrightText)) |
| 79 | + _, err = io.ReadFull(f, actual) |
| 80 | + if err == io.ErrUnexpectedEOF { |
| 81 | + err = nil // some files are shorter than license header |
| 82 | + } |
| 83 | + if err != nil { |
| 84 | + log.Printf("%s - %s", path, err) |
| 85 | + return false |
| 86 | + } |
| 87 | + |
| 88 | + if generatedHeader.Match(actual) { |
| 89 | + return true |
| 90 | + } |
| 91 | + |
| 92 | + if !copyrightPattern.Match(actual) { |
| 93 | + log.Print(path) |
| 94 | + return false |
| 95 | + } |
| 96 | + return true |
| 97 | +} |
| 98 | + |
| 99 | +func main() { |
| 100 | + log.SetFlags(0) |
| 101 | + flag.Usage = func() { |
| 102 | + fmt.Fprintln(flag.CommandLine.Output(), "Usage: go run .github/check-license.go") |
| 103 | + flag.CommandLine.PrintDefaults() |
| 104 | + } |
| 105 | + flag.Parse() |
| 106 | + |
| 107 | + ok := true |
| 108 | + filepath.Walk(".", func(path string, info os.FileInfo, err error) error { |
| 109 | + if err != nil { |
| 110 | + return err |
| 111 | + } |
| 112 | + if info.IsDir() { |
| 113 | + switch info.Name() { |
| 114 | + case ".git", "vendor": |
| 115 | + return filepath.SkipDir |
| 116 | + default: |
| 117 | + return nil |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + if filepath.Ext(info.Name()) == ".go" { |
| 122 | + if !checkHeader(path) { |
| 123 | + ok = false |
| 124 | + } |
| 125 | + } |
| 126 | + return nil |
| 127 | + }) |
| 128 | + |
| 129 | + if ok { |
| 130 | + os.Exit(0) |
| 131 | + } |
| 132 | + log.Print("Please update license header in those files.") |
| 133 | + os.Exit(1) |
| 134 | +} |
0 commit comments