Skip to content

Commit

Permalink
feat: add multiline support
Browse files Browse the repository at this point in the history
This should fix elastic#6

A new flag is added `-multiline` which uses multiline variants of the existing headers.
  • Loading branch information
sruehl committed Apr 6, 2023
1 parent a6a3235 commit b1269c4
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ Options:
sets the license type to check: ASL2, ASL2-Short, Cloud, Elastic, Elasticv2 (default "ASL2")
-licensor string
sets the name of the licensor (default "Elasticsearch B.V.")
-multiline
uses multiline comments
-version
prints out the binary version.
```
Expand Down
15 changes: 15 additions & 0 deletions licensing/headers.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,18 @@ var Headers = map[string][]string{
`// permission is obtained from Elasticsearch B.V.`,
},
}

// HeadersMultiline is a multiline variant map of Headers
var HeadersMultiline = func() map[string][]string {
headersMultiline := map[string][]string{}
for licence, headerLines := range Headers {
multiLineHeaderLines := []string{`/*`}
for _, headerLine := range headerLines {
multiHeaderLine := ` *` + headerLine[2:]
multiLineHeaderLines = append(multiLineHeaderLines, multiHeaderLine)
}
multiLineHeaderLines = append(multiLineHeaderLines, ` */`)
headersMultiline[licence] = multiLineHeaderLines
}
return headersMultiline
}()
10 changes: 10 additions & 0 deletions licensing/license.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ func init() {
l += len(v2)
}

if l > defaulBufSize {
defaulBufSize = l
}
}
for _, v := range HeadersMultiline {
var l int
for _, v2 := range v {
l += len(v2)
}

if l > defaulBufSize {
defaulBufSize = l
}
Expand Down
7 changes: 6 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ var (
license string
licensor string
exclude sliceFlag
multiline bool
defaultExludedDirs = []string{"vendor", ".git"}
)

Expand Down Expand Up @@ -101,6 +102,7 @@ func initFlags() {
flag.StringVar(&extension, "ext", defaultExt, "sets the file extension to scan for.")
flag.StringVar(&license, "license", defaultLicense, fmt.Sprintf("sets the license type to check: %s", strings.Join(licenseTypes, ", ")))
flag.StringVar(&licensor, "licensor", defaultLicensor, "sets the name of the licensor")
flag.BoolVar(&multiline, "multiline", false, `uses multiline comments`)
flag.Usage = usageFlag
flag.Parse()
args = flag.Args()
Expand All @@ -127,10 +129,13 @@ func run(args []string, license, licensor string, exclude []string, ext string,
if !ok {
return &Error{err: fmt.Errorf("unknown license: %s", license), code: errUnknownLicense}
}
if multiline {
header = licensing.HeadersMultiline[license]
}

var headerBytes []byte
if copyright {
year, _, _ := time.Now().Date()
year, _, _ := time.Now().Date()
headerBytes = append(headerBytes, []byte(fmt.Sprintf("// Copyright %d %s\n", year, licensor))...)
}
for i, line := range header {
Expand Down

0 comments on commit b1269c4

Please sign in to comment.