Skip to content

Commit 78c33e6

Browse files
authored
[Maintenace] Add license range rewrite command (#1248)
1 parent 09a28f0 commit 78c33e6

File tree

3 files changed

+86
-2
lines changed

3 files changed

+86
-2
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- (Improvement) Update arangosync-client package for new API capabilities and better HTTP handling
1010
- (Maintenance) Fix generated license dates
1111
- (Improvement) Reduce CI on Commit Travis runs
12+
- (Maintenance) Add license range rewrite command
1213

1314
## [1.2.24](https://github.com/arangodb/kube-arangodb/tree/1.2.24) (2023-01-25)
1415
- (Bugfix) Fix deployment creation on ARM64

Makefile

+9-2
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,10 @@ license-verify:
232232
license-range-verify:
233233
@GOBIN=$(GOPATH)/bin go run "$(ROOT)/tools/license/" $(SOURCES)
234234

235+
.PHONY: license-range
236+
license-range:
237+
@GOBIN=$(GOPATH)/bin go run "$(ROOT)/tools/license/" -w $(SOURCES)
238+
235239
.PHONY: fmt
236240
fmt:
237241
@echo ">> Ensuring style of files"
@@ -250,11 +254,11 @@ fmt-verify: license-verify
250254

251255
.PHONY: linter
252256
linter:
253-
$(GOPATH)/bin/golangci-lint run --build-tags "$(RELEASE_MODE)" $(foreach LINT_EXCLUDE,$(LINT_EXCLUDES),--exclude '$(LINT_EXCLUDE)') ./...
257+
@$(GOPATH)/bin/golangci-lint run --build-tags "$(RELEASE_MODE)" $(foreach LINT_EXCLUDE,$(LINT_EXCLUDES),--exclude '$(LINT_EXCLUDE)') ./...
254258

255259
.PHONY: linter-fix
256260
linter-fix:
257-
$(GOPATH)/bin/golangci-lint run --fix --build-tags "$(RELEASE_MODE)" $(foreach LINT_EXCLUDE,$(LINT_EXCLUDES),--exclude '$(LINT_EXCLUDE)') ./...
261+
@$(GOPATH)/bin/golangci-lint run --fix --build-tags "$(RELEASE_MODE)" $(foreach LINT_EXCLUDE,$(LINT_EXCLUDES),--exclude '$(LINT_EXCLUDE)') ./...
258262

259263
.PHONY: build
260264
build: docker manifests
@@ -607,3 +611,6 @@ generate-proto:
607611
--go_out=. --go_opt=paths=source_relative \
608612
--go-grpc_out=. --go-grpc_opt=paths=source_relative \
609613
$(PROTOSOURCES)
614+
615+
.PHONY: fix
616+
fix: license-range fmt license

tools/license/license.go

+76
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,24 @@ type dateRange struct {
2929
func mainE() error {
3030
// Ensure that all files have proper license dates
3131

32+
rewrite := false
33+
34+
for _, a := range os.Args[1:] {
35+
if a == "-w" {
36+
rewrite = true
37+
}
38+
}
39+
3240
files := map[string]int{}
3341

3442
currentHeaders := map[string]dateRange{}
3543

3644
// Extract current dates
3745
for _, file := range os.Args[1:] {
46+
if file == "-w" {
47+
continue
48+
}
49+
3850
var out bytes.Buffer
3951

4052
cmd := exec.Command("git", "log", "-n", "1", "--pretty=format:%cd", file)
@@ -79,6 +91,21 @@ func mainE() error {
7991
valid = false
8092
} else if date < c.from || date > c.to {
8193
println(fmt.Sprintf("Date %d not in range %d-%d for %s. File has beed modified", date, c.from, c.to, file))
94+
if rewrite {
95+
println("Rewrite file")
96+
97+
q := fmt.Sprintf("// Copyright %d-%d ArangoDB GmbH, Cologne, Germany", c.from, c.to)
98+
if c.from == c.to {
99+
q = fmt.Sprintf("// Copyright %d ArangoDB GmbH, Cologne, Germany", c.to)
100+
}
101+
102+
changed, err := rewriteLicenseDates(file, q, fmt.Sprintf("// Copyright %d-%d ArangoDB GmbH, Cologne, Germany", c.from, date))
103+
if err != nil {
104+
return err
105+
} else if changed {
106+
continue
107+
}
108+
}
82109
valid = false
83110
}
84111
}
@@ -90,6 +117,55 @@ func mainE() error {
90117
return nil
91118
}
92119

120+
func rewriteLicenseDates(file string, from, to string) (bool, error) {
121+
data, changed, err := readNewLicenseDates(file, from, to)
122+
if err != nil {
123+
return false, err
124+
}
125+
126+
if !changed {
127+
return false, nil
128+
}
129+
130+
if err := os.WriteFile(file, data, 0644); err != nil {
131+
if err != nil {
132+
return false, err
133+
}
134+
}
135+
136+
return true, nil
137+
}
138+
139+
func readNewLicenseDates(file string, from, to string) ([]byte, bool, error) {
140+
readFile, err := os.Open(file)
141+
if err != nil {
142+
return nil, false, err
143+
}
144+
145+
defer readFile.Close()
146+
147+
fileScanner := bufio.NewScanner(readFile)
148+
149+
fileScanner.Split(bufio.ScanLines)
150+
151+
q := bytes.NewBuffer(nil)
152+
153+
got := false
154+
155+
for fileScanner.Scan() {
156+
t := fileScanner.Text()
157+
if t == from {
158+
got = true
159+
q.WriteString(to)
160+
} else {
161+
q.WriteString(t)
162+
}
163+
q.WriteString("\n")
164+
}
165+
166+
return q.Bytes(), got, nil
167+
}
168+
93169
func extractFileLicenseData(file string) (int, int, error) {
94170
readFile, err := os.Open(file)
95171
if err != nil {

0 commit comments

Comments
 (0)