Skip to content
This repository was archived by the owner on Jul 18, 2022. It is now read-only.

Commit a934778

Browse files
author
Florent Biville
committed
Add main function
1 parent 9851e96 commit a934778

File tree

8 files changed

+127
-56
lines changed

8 files changed

+127
-56
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.idea
22
/vendor
3+
header

Gopkg.lock

Lines changed: 15 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

configuration.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package header
1+
package main
22

33
import (
44
"bufio"
@@ -8,26 +8,30 @@ import (
88
"strings"
99
)
1010

11+
type Configuration struct {
12+
HeaderFile string `json:"headerFile"`
13+
CommentStyle string `json:"style"`
14+
Includes []string `json:"includes"`
15+
Excludes []string `json:"excludes"`
16+
TemplateData map[string]string `json:"data"`
17+
}
18+
1119
type configuration struct {
1220
HeaderContents string
1321
Includes []string
1422
Excludes []string
1523
writer io.Writer
1624
}
1725

18-
func NewConfiguration(headerFile string,
19-
style CommentStyle,
20-
includes []string,
21-
excludes []string,
22-
data map[string]string) (*configuration, error) {
23-
contents, err := parseTemplate(headerFile, data, style)
26+
func ParseConfiguration(config Configuration) (*configuration, error) {
27+
contents, err := parseTemplate(config.HeaderFile, config.TemplateData, newCommentStyle(config.CommentStyle))
2428
if err != nil {
2529
return nil, err
2630
}
2731
return &configuration{
2832
HeaderContents: contents,
29-
Includes: includes,
30-
Excludes: excludes,
33+
Includes: config.Includes,
34+
Excludes: config.Excludes,
3135
}, nil
3236
}
3337

configuration_test.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package header_test
1+
package main_test
22

33
import (
44
. "github.com/fbiville/header"
@@ -9,40 +9,40 @@ import (
99
func TestConfigurationInitWithLineCommentStyle(t *testing.T) {
1010
I := NewGomegaWithT(t)
1111

12-
configuration, err := NewConfiguration(
13-
"fixtures/license.txt",
14-
SlashSlash,
15-
[]string{"*.txt"},
16-
[]string{},
17-
map[string]string{
12+
configuration, err := ParseConfiguration(Configuration{
13+
HeaderFile: "fixtures/license.txt",
14+
CommentStyle: "SlashSlash",
15+
Includes: []string{"*.txt"},
16+
Excludes: []string{},
17+
TemplateData: map[string]string{
1818
"Year": "2018",
1919
"Owner": "ACME Labs",
20-
})
20+
}})
2121

2222
I.Expect(err).To(BeNil())
2323
I.Expect(configuration.HeaderContents).To(Equal(`// Copyright 2018 ACME Labs
24-
//
24+
//
2525
// Some fictional license`))
2626
I.Expect(configuration.Includes).To(Equal([]string{"*.txt"}))
2727
}
2828

2929
func TestConfigurationInitWithBlockCommentStyle(t *testing.T) {
3030
I := NewGomegaWithT(t)
3131

32-
configuration, err := NewConfiguration(
33-
"fixtures/license.txt",
34-
SlashStar,
35-
[]string{"*.txt"},
36-
[]string{},
37-
map[string]string{
32+
configuration, err := ParseConfiguration(Configuration{
33+
HeaderFile: "fixtures/license.txt",
34+
CommentStyle: "SlashStar",
35+
Includes: []string{"*.txt"},
36+
Excludes: []string{},
37+
TemplateData: map[string]string{
3838
"Year": "2018",
3939
"Owner": "ACME Labs",
40-
})
40+
}})
4141

4242
I.Expect(err).To(BeNil())
43-
I.Expect(configuration.HeaderContents).To(Equal(`/*
43+
I.Expect(configuration.HeaderContents).To(Equal(`/*
4444
* Copyright 2018 ACME Labs
45-
*
45+
*
4646
* Some fictional license
4747
*/`))
4848
I.Expect(configuration.Includes).To(Equal([]string{"*.txt"}))

header.go

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
package header
1+
package main
22

33
import (
44
"bufio"
55
. "bytes"
66
"fmt"
7+
"github.com/mattn/go-zglob"
78
"io/ioutil"
89
"os"
9-
"path/filepath"
1010
)
1111

12-
func Insert(config *configuration) {
12+
func InsertHeader(config *configuration) {
1313
for _, includePattern := range config.Includes {
14-
matches, err := filepath.Glob(includePattern)
14+
matches, err := zglob.Glob(includePattern)
1515
if err != nil {
1616
panic(err)
1717
}
@@ -31,7 +31,7 @@ func exclude(strings []string, exclusionPatterns []string) []string {
3131

3232
func matches(str string, exclusionPatterns []string) bool {
3333
for _, exclusionPattern := range exclusionPatterns {
34-
matched, _ := filepath.Match(exclusionPattern, str)
34+
matched, _ := zglob.Match(exclusionPattern, str)
3535
if matched {
3636
return true
3737
}
@@ -50,18 +50,26 @@ func insertInMatchedFiles(config *configuration, files []string) {
5050
continue
5151
}
5252

53-
newContents := append([]byte(fmt.Sprintf("%s%s", config.HeaderContents, "\n")), bytes...)
54-
var writer = config.writer
55-
if writer == nil {
56-
openFile, err := os.Open(file)
57-
if err != nil {
58-
panic(err)
59-
}
60-
defer openFile.Close()
61-
writer = bufio.NewWriter(openFile)
62-
}
63-
64-
writer.Write(newContents)
53+
newContents := append([]byte(fmt.Sprintf("%s%s", config.HeaderContents, "\n\n")), bytes...)
54+
writeToFile(config, file, newContents)
55+
}
56+
}
6557

58+
func writeToFile(config *configuration, file string, newContents []byte) {
59+
var writer = config.writer
60+
if writer == nil {
61+
openFile, err := os.OpenFile(file, os.O_WRONLY, os.ModeAppend)
62+
if err != nil {
63+
panic(err)
64+
}
65+
_, err = openFile.Write(newContents)
66+
openFile.Close()
67+
if err != nil {
68+
panic(err)
69+
}
70+
} else {
71+
bufferedWriter := bufio.NewWriter(writer)
72+
bufferedWriter.Write(newContents)
73+
bufferedWriter.Flush()
6674
}
6775
}

header_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package header
1+
package main
22

33
import (
44
"bufio"
@@ -18,11 +18,12 @@ func TestHeaderWrite(t *testing.T) {
1818
writer: writer,
1919
}
2020

21-
Insert(&configuration)
21+
InsertHeader(&configuration)
2222
writer.Flush()
2323

2424
I.Expect(stringBuilder.String()).To(Equal(`// some multi-line header
2525
// with some text
26+
2627
hello
2728
world`))
2829
}
@@ -38,7 +39,7 @@ func TestHeaderDoesNotWriteTwice(t *testing.T) {
3839
writer: writer,
3940
}
4041

41-
Insert(&configuration)
42+
InsertHeader(&configuration)
4243
writer.Flush()
4344

4445
I.Expect(stringBuilder.String()).To(Equal(``))
@@ -56,10 +57,11 @@ func TestHeaderWriteWithExcludes(t *testing.T) {
5657
writer: writer,
5758
}
5859

59-
Insert(&configuration)
60+
InsertHeader(&configuration)
6061
writer.Flush()
6162

6263
I.Expect(stringBuilder.String()).To(Equal(`// some header
64+
6365
bonjour
6466
le world`))
6567
}

line_comment.go

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package header
1+
package main
22

33
import (
44
"fmt"
@@ -11,6 +11,17 @@ const (
1111
SlashSlash
1212
)
1313

14+
func newCommentStyle(str string) CommentStyle {
15+
switch str {
16+
case "SlashStar":
17+
return SlashStar
18+
case "SlashSlash":
19+
return SlashSlash
20+
default:
21+
panic(unknownStyleError())
22+
}
23+
}
24+
1425
func (style *CommentStyle) opening() bool {
1526
return *style == SlashStar
1627
}
@@ -22,7 +33,7 @@ func (style *CommentStyle) closing() bool {
2233
func (style *CommentStyle) open() string {
2334
switch *style {
2435
case SlashStar:
25-
return "/* "
36+
return "/*"
2637
default:
2738
panic(unknownSurroundingCommentError())
2839
}
@@ -38,21 +49,24 @@ func (style *CommentStyle) close() string {
3849
}
3950

4051
func (style *CommentStyle) apply(line string) string {
41-
return fmt.Sprintf("%s%s", style.commentSymbol(), line)
52+
if line == "" {
53+
return style.commentSymbol()
54+
}
55+
return fmt.Sprintf("%s %s", style.commentSymbol(), line)
4256
}
4357

4458
func (style *CommentStyle) commentSymbol() string {
4559
switch *style {
4660
case SlashSlash:
47-
return "// "
61+
return "//"
4862
case SlashStar:
49-
return " * "
63+
return " *"
5064
default:
51-
panic(unknownCommentError())
65+
panic(unknownStyleError())
5266
}
5367
}
5468

55-
func unknownCommentError() string {
69+
func unknownStyleError() string {
5670
return "Unexpected comment style, must be one of: SlashSlash, SlashStar"
5771
}
5872

main.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"flag"
6+
"io/ioutil"
7+
)
8+
9+
func main() {
10+
rawConfiguration := readConfiguration()
11+
configuration, err := ParseConfiguration(rawConfiguration)
12+
if err != nil {
13+
panic(err)
14+
}
15+
InsertHeader(configuration)
16+
}
17+
18+
func readConfiguration() Configuration {
19+
configFile := flag.String("configuration", "license.json", "Path to configuration file")
20+
flag.Parse()
21+
file, err := ioutil.ReadFile(*configFile)
22+
if err != nil {
23+
panic(err)
24+
}
25+
rawConfiguration := Configuration{}
26+
json.Unmarshal(file, &rawConfiguration)
27+
return rawConfiguration
28+
}

0 commit comments

Comments
 (0)