github.com/cs3org/reva/v2@v2.27.7/tools/check-license/check-license.go (about) 1 // Copyright 2018-2021 CERN 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 // 15 // In applying this license, CERN does not waive the privileges and immunities 16 // granted to it by virtue of its status as an Intergovernmental Organization 17 // or submit itself to any jurisdiction. 18 19 package main 20 21 import ( 22 "bytes" 23 "flag" 24 "fmt" 25 "os" 26 "path/filepath" 27 "strings" 28 ) 29 30 var fix = flag.Bool("fix", false, "add header if not present") 31 32 var licenseText = `// Copyright 2018-2022 CERN 33 // 34 // Licensed under the Apache License, Version 2.0 (the "License"); 35 // you may not use this file except in compliance with the License. 36 // You may obtain a copy of the License at 37 // 38 // http://www.apache.org/licenses/LICENSE-2.0 39 // 40 // Unless required by applicable law or agreed to in writing, software 41 // distributed under the License is distributed on an "AS IS" BASIS, 42 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 43 // See the License for the specific language governing permissions and 44 // limitations under the License. 45 // 46 // In applying this license, CERN does not waive the privileges and immunities 47 // granted to it by virtue of its status as an Intergovernmental Organization 48 // or submit itself to any jurisdiction. 49 50 ` 51 52 const prefix = "// Copyright " 53 54 var toSkip = []string{"vendor/"} 55 56 func skip(path string) bool { 57 for _, v := range toSkip { 58 if strings.HasPrefix(path, v) { 59 return true 60 } 61 } 62 return false 63 } 64 65 func main() { 66 flag.Parse() 67 err := filepath.Walk(".", func(path string, fi os.FileInfo, err error) error { 68 if skip(path) { 69 return nil 70 } 71 72 if err != nil { 73 return err 74 } 75 76 if filepath.Ext(path) != ".go" { 77 return nil 78 } 79 80 src, err := os.ReadFile(path) 81 if err != nil { 82 return nil 83 } 84 85 // Check if license is at the top of the file. 86 if !bytes.HasPrefix(src, []byte(prefix)) { 87 err := fmt.Errorf("%v: license header not present or not at the top, to fix run: go run tools/check-license/check-license.go -fix", path) 88 if *fix { 89 newSrc := licenseText + string(src) 90 if err := os.WriteFile(path, []byte(newSrc), 0644); err != nil { 91 fmt.Println(err) 92 os.Exit(1) 93 } 94 } else { 95 return err 96 } 97 } 98 return nil 99 }) 100 if err != nil { 101 fmt.Println(err) 102 os.Exit(1) 103 } 104 os.Exit(0) 105 }