-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
133 lines (122 loc) · 3.47 KB
/
main.go
File metadata and controls
133 lines (122 loc) · 3.47 KB
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
127
128
129
130
131
132
133
package main
//
//Copyright 2018 Ståle Holberg Dahl
//
//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at
//
//http://www.apache.org/licenses/LICENSE-2.0
//
//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and
//limitations under the License.
//
import (
"bufio"
"flag"
"fmt"
"os"
"path"
"strings"
)
// License is a license to apply
type License interface {
LicenseText() string
FileHeader() string
}
var selectedLicense string
var listLicenses bool
var availableLicenses []License
var owner string
const filePattern = "*.go"
func init() {
flag.StringVar(&selectedLicense, "license", "apache2.0", "The selected license")
flag.StringVar(&owner, "owner", "", "Name of license owner")
flag.Parse()
availableLicenses = append(availableLicenses, NewApache20License("2018", owner))
}
func addCommentBlock(licenseText string) string {
return "//" + strings.Replace(licenseText, "\n", "\n//", -1)
}
func addHeaderToFile(f *os.File, absPath string, license License) {
var output []byte
scanner := bufio.NewScanner(f)
for scanner.Scan() {
if err := scanner.Err(); err != nil {
fmt.Printf("Unable to read file %s: %v\n", f.Name(), err)
return
}
output = append(output, []byte(scanner.Text()+"\n")...)
if strings.HasPrefix(scanner.Text(), "package") {
output = append(output, []byte(addCommentBlock(license.FileHeader()))...)
}
}
if err := os.Remove(absPath); err != nil {
fmt.Printf("Unable to remove original file %s: %v\n", absPath, err)
return
}
newFile, err := os.Create(absPath)
if err != nil {
fmt.Printf("Got error creating %s: %v\n", absPath, err)
return
}
_, err = newFile.Write(output)
if err != nil {
fmt.Printf("Got error writing to %s: %v\n", absPath, err)
return
}
newFile.Close()
}
func applyLicenseToFiles(f *os.File, dirPath string, license License) {
files, err := f.Readdir(-1)
if err != nil {
fmt.Printf("Unable to read directory %s: %v\n", f.Name(), err)
return
}
for _, v := range files {
filePath := path.Join(dirPath, v.Name())
item, err := os.Open(filePath)
if err != nil {
fmt.Printf("Unable to open %s: %v\n", filePath, err)
continue
}
if v.IsDir() {
applyLicenseToFiles(item, filePath, license)
} else {
if path.Ext(v.Name()) == ".go" {
addHeaderToFile(item, filePath, license)
}
}
item.Close()
}
}
func main() {
currentLicense := availableLicenses[0]
// Do this the idiot way and not do anything clever. Create the LICENSE
// file at the root, find all source files, look for the line that starts
// with "package" and insert the header right below it.
_, err := os.Stat("LICENSE")
if err == nil {
fmt.Println("There is already a LICENSE file in the current directory")
os.Exit(1)
}
license, err := os.Create("LICENSE")
if err != nil {
fmt.Println("Unable to create LICENSE file: ", err)
os.Exit(2)
}
if _, err := license.Write([]byte(currentLicense.LicenseText())); err != nil {
fmt.Println("Unable to write the license text to LICENSE: ", err)
os.Exit(3)
}
license.Close()
curDir, err := os.Open(".")
if err != nil {
fmt.Println("Unable to read current directory: ", err)
os.Exit(4)
}
applyLicenseToFiles(curDir, ".", currentLicense)
}