Skip to content

Commit

Permalink
chore: resolve comments, refactor code
Browse files Browse the repository at this point in the history
Signed-off-by: Nikita Korolev <[email protected]>
  • Loading branch information
universal-itengineer committed Jun 13, 2024
1 parent 98b79ea commit 302aba2
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 22 deletions.
6 changes: 3 additions & 3 deletions tools/addlicense/testdata/somef2.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#!/bin/bash

# Copyright 2024 Flant JSC
#
#
# 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.
Expand Down
22 changes: 3 additions & 19 deletions tools/addlicense/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,6 @@ func addLicenseToFile(filePath, license string) Message {
defer closeFile(f)

reader := bufio.NewReader(f)
//firstLine, err := reader.ReadString('\n')
//if err != nil && err.Error() != "EOF" {
// message := NewError(filepath.Base(filePath), "Failed to read first line", err.Error())
// return message
//}

fullContent, err := io.ReadAll(reader)
if err != nil {
Expand All @@ -79,24 +74,15 @@ func addLicenseToFile(filePath, license string) Message {
}

firstLine, restOfFile, _ := strings.Cut(string(fullContent), "\n")
//fullContent := append([]byte(firstLine), restOfFile...)

message, lic := checkLicense(fullContent, filePath)
//var newContent []byte
var newContent bytes.Buffer

if !lic {
if isShebangLine(firstLine) {
newContent.Write([]byte(firstLine + "\n\n"))
newContent.Write([]byte(license))
newContent.Write([]byte(restOfFile))
//newContent = append([]byte(firstLine+"\n"), append([]byte(license), restOfFile...)...)
newContent.Write([]byte(firstLine + "\n\n" + license + restOfFile))
} else {
newContent.Write([]byte(license))
newContent.Write([]byte(restOfFile))
//newContent.Write([]byte(firstLine + "\n"))
//newContent = append([]byte(license), append([]byte(firstLine+"\n"), restOfFile...)...)
//newContent = append([]byte(license), append([]byte(firstLine+"\n"), restOfFile...)...)
newContent.Write([]byte(license + restOfFile))
}
} else {
return message
Expand All @@ -105,10 +91,8 @@ func addLicenseToFile(filePath, license string) Message {
err = os.WriteFile(filePath, newContent.Bytes(), 0644)

if err != nil {
message = NewError(filepath.Base(filePath), "Failed to write file", err.Error())
return message
return NewError(filepath.Base(filePath), "Failed to write file", err.Error())
}

return NewAdd(filepath.Base(filePath))
//return os.WriteFile(filePath, newContent, 0644)
}
9 changes: 9 additions & 0 deletions tools/addlicense/variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,16 @@ limitations under the License.
var goLicense = "/*\n" + licenseText + "*/\n\n"
var bashPythonLicense = "# " + strings.ReplaceAll(strings.TrimSpace(licenseText), "\n", "\n# ") + "\n\n"

// fileToCheckRe matches files that will be checked for adding license, meet the following conditions:
// - Ends with .go, .sh, .py
// - Is inside a .github directory: scripts, workflows, or workflow_templates subdirectories,
// and ends with .js, .yml, .yaml, or .sh
var fileToCheckRe = regexp.MustCompile(`\.go$|/[^/.]+$|\.sh$|\.py$|^\.github/(scripts|workflows|workflow_templates)/.+\.(js|yml|yaml|sh)$`)

// fileToSkipRe matches filenames that will be skipped for adding license, meet the following conditions:
// - Directories .github/CODEOWNERS, /docs/
// - Filename contains Dockerfile, Makefile, Taskfile, LICENSE
// - Ends with geohash.lua, bashrc, inputrc, modules_menu_skip
var fileToSkipRe = regexp.MustCompile(`geohash.lua$|\.
github/CODEOWNERS|Dockerfile$|Makefile$|Taskfile|/docs/|bashrc$|inputrc$|modules_menu_skip$
|LICENSE$`)
Expand Down

0 comments on commit 302aba2

Please sign in to comment.