Skip to content

Commit

Permalink
Detect #! in files and fix initial line of license
Browse files Browse the repository at this point in the history
Signed-off-by: Liam White <[email protected]>
  • Loading branch information
liamawhite committed Jun 26, 2019
1 parent a933f52 commit a35a8cc
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 13 deletions.
1 change: 0 additions & 1 deletion .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#
# Copyright 2019 Liam White
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand Down
1 change: 0 additions & 1 deletion pkg/command/apply.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
//
// Copyright 2019 Liam White
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand Down
1 change: 0 additions & 1 deletion pkg/command/root.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
//
// Copyright 2019 Liam White
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand Down
1 change: 0 additions & 1 deletion pkg/command/verify.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
//
// Copyright 2019 Liam White
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand Down
30 changes: 25 additions & 5 deletions pkg/file/mutator.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
//
// Copyright 2019 Liam White
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -23,6 +22,7 @@ import (
"os"
"path/filepath"
"regexp"
"strings"

"github.com/liamawhite/licenser/pkg/license"
)
Expand All @@ -46,7 +46,7 @@ func (m *Mutator) AppendLicense(path string, dryRun bool) bool {
return false
}
if !m.license.IsPresent(bytes.NewReader(contents)) {
newContents := append(styled, contents...)
newContents := merge(styled, contents)
if dryRun {
fmt.Printf("%s\n", newContents)
} else {
Expand Down Expand Up @@ -87,8 +87,6 @@ func (m *Mutator) styledLicense(path string) []byte {

} else {
scanner := bufio.NewScanner(m.license.Reader())
buf.WriteString(style.comment)
buf.WriteString(" \n")
for scanner.Scan() {
buf.WriteString(style.comment)
if len(scanner.Bytes()) != 0 {
Expand All @@ -97,11 +95,33 @@ func (m *Mutator) styledLicense(path string) []byte {
buf.Write(scanner.Bytes())
buf.WriteString("\n")
}
buf.WriteString("\n")
}
return buf.Bytes()
}

// this is also pretty horrible but does the job
func merge(license, file []byte) []byte {
result := bytes.NewBuffer([]byte{})
fileScanner := bufio.NewScanner(bytes.NewReader(file))
licensePlaced := false
for fileScanner.Scan() {
// If we've placed the license just continue to dump out the rest of the file
if licensePlaced {
result.Write(fileScanner.Bytes())
result.WriteString("\n")
continue
}
// If there's a #! preserve it
if strings.Contains(fileScanner.Text(), "#!") {
result.Write(fileScanner.Bytes())
result.WriteString("\n\n")
}
result.Write(license)
licensePlaced = true
}
return result.Bytes()
}

// This function has the potential to become an unwiedly mess, consider rethinking.
// TODO: Create a language interface that can be cycled through]
// in order to identify the file as said language
Expand Down
1 change: 0 additions & 1 deletion pkg/file/style.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
//
// Copyright 2019 Liam White
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand Down
1 change: 0 additions & 1 deletion pkg/license/apache.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
//
// Copyright 2019 Liam White
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand Down
1 change: 0 additions & 1 deletion pkg/license/interface.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
//
// Copyright 2019 Liam White
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand Down
1 change: 0 additions & 1 deletion pkg/processor/processor.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
//
// Copyright 2019 Liam White
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand Down

0 comments on commit a35a8cc

Please sign in to comment.