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

Commit

Permalink
Add main function
Browse files Browse the repository at this point in the history
  • Loading branch information
Florent Biville committed Sep 25, 2018
1 parent 9851e96 commit a934778
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 56 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.idea
/vendor
header
16 changes: 15 additions & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 13 additions & 9 deletions configuration.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package header
package main

import (
"bufio"
Expand All @@ -8,26 +8,30 @@ import (
"strings"
)

type Configuration struct {
HeaderFile string `json:"headerFile"`
CommentStyle string `json:"style"`
Includes []string `json:"includes"`
Excludes []string `json:"excludes"`
TemplateData map[string]string `json:"data"`
}

type configuration struct {
HeaderContents string
Includes []string
Excludes []string
writer io.Writer
}

func NewConfiguration(headerFile string,
style CommentStyle,
includes []string,
excludes []string,
data map[string]string) (*configuration, error) {
contents, err := parseTemplate(headerFile, data, style)
func ParseConfiguration(config Configuration) (*configuration, error) {
contents, err := parseTemplate(config.HeaderFile, config.TemplateData, newCommentStyle(config.CommentStyle))
if err != nil {
return nil, err
}
return &configuration{
HeaderContents: contents,
Includes: includes,
Excludes: excludes,
Includes: config.Includes,
Excludes: config.Excludes,
}, nil
}

Expand Down
36 changes: 18 additions & 18 deletions configuration_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package header_test
package main_test

import (
. "github.com/fbiville/header"
Expand All @@ -9,40 +9,40 @@ import (
func TestConfigurationInitWithLineCommentStyle(t *testing.T) {
I := NewGomegaWithT(t)

configuration, err := NewConfiguration(
"fixtures/license.txt",
SlashSlash,
[]string{"*.txt"},
[]string{},
map[string]string{
configuration, err := ParseConfiguration(Configuration{
HeaderFile: "fixtures/license.txt",
CommentStyle: "SlashSlash",
Includes: []string{"*.txt"},
Excludes: []string{},
TemplateData: map[string]string{
"Year": "2018",
"Owner": "ACME Labs",
})
}})

I.Expect(err).To(BeNil())
I.Expect(configuration.HeaderContents).To(Equal(`// Copyright 2018 ACME Labs
//
//
// Some fictional license`))
I.Expect(configuration.Includes).To(Equal([]string{"*.txt"}))
}

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

configuration, err := NewConfiguration(
"fixtures/license.txt",
SlashStar,
[]string{"*.txt"},
[]string{},
map[string]string{
configuration, err := ParseConfiguration(Configuration{
HeaderFile: "fixtures/license.txt",
CommentStyle: "SlashStar",
Includes: []string{"*.txt"},
Excludes: []string{},
TemplateData: map[string]string{
"Year": "2018",
"Owner": "ACME Labs",
})
}})

I.Expect(err).To(BeNil())
I.Expect(configuration.HeaderContents).To(Equal(`/*
I.Expect(configuration.HeaderContents).To(Equal(`/*
* Copyright 2018 ACME Labs
*
*
* Some fictional license
*/`))
I.Expect(configuration.Includes).To(Equal([]string{"*.txt"}))
Expand Down
42 changes: 25 additions & 17 deletions header.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package header
package main

import (
"bufio"
. "bytes"
"fmt"
"github.com/mattn/go-zglob"
"io/ioutil"
"os"
"path/filepath"
)

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

func matches(str string, exclusionPatterns []string) bool {
for _, exclusionPattern := range exclusionPatterns {
matched, _ := filepath.Match(exclusionPattern, str)
matched, _ := zglob.Match(exclusionPattern, str)
if matched {
return true
}
Expand All @@ -50,18 +50,26 @@ func insertInMatchedFiles(config *configuration, files []string) {
continue
}

newContents := append([]byte(fmt.Sprintf("%s%s", config.HeaderContents, "\n")), bytes...)
var writer = config.writer
if writer == nil {
openFile, err := os.Open(file)
if err != nil {
panic(err)
}
defer openFile.Close()
writer = bufio.NewWriter(openFile)
}

writer.Write(newContents)
newContents := append([]byte(fmt.Sprintf("%s%s", config.HeaderContents, "\n\n")), bytes...)
writeToFile(config, file, newContents)
}
}

func writeToFile(config *configuration, file string, newContents []byte) {
var writer = config.writer
if writer == nil {
openFile, err := os.OpenFile(file, os.O_WRONLY, os.ModeAppend)
if err != nil {
panic(err)
}
_, err = openFile.Write(newContents)
openFile.Close()
if err != nil {
panic(err)
}
} else {
bufferedWriter := bufio.NewWriter(writer)
bufferedWriter.Write(newContents)
bufferedWriter.Flush()
}
}
10 changes: 6 additions & 4 deletions header_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package header
package main

import (
"bufio"
Expand All @@ -18,11 +18,12 @@ func TestHeaderWrite(t *testing.T) {
writer: writer,
}

Insert(&configuration)
InsertHeader(&configuration)
writer.Flush()

I.Expect(stringBuilder.String()).To(Equal(`// some multi-line header
// with some text
hello
world`))
}
Expand All @@ -38,7 +39,7 @@ func TestHeaderDoesNotWriteTwice(t *testing.T) {
writer: writer,
}

Insert(&configuration)
InsertHeader(&configuration)
writer.Flush()

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

Insert(&configuration)
InsertHeader(&configuration)
writer.Flush()

I.Expect(stringBuilder.String()).To(Equal(`// some header
bonjour
le world`))
}
28 changes: 21 additions & 7 deletions line_comment.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package header
package main

import (
"fmt"
Expand All @@ -11,6 +11,17 @@ const (
SlashSlash
)

func newCommentStyle(str string) CommentStyle {
switch str {
case "SlashStar":
return SlashStar
case "SlashSlash":
return SlashSlash
default:
panic(unknownStyleError())
}
}

func (style *CommentStyle) opening() bool {
return *style == SlashStar
}
Expand All @@ -22,7 +33,7 @@ func (style *CommentStyle) closing() bool {
func (style *CommentStyle) open() string {
switch *style {
case SlashStar:
return "/* "
return "/*"
default:
panic(unknownSurroundingCommentError())
}
Expand All @@ -38,21 +49,24 @@ func (style *CommentStyle) close() string {
}

func (style *CommentStyle) apply(line string) string {
return fmt.Sprintf("%s%s", style.commentSymbol(), line)
if line == "" {
return style.commentSymbol()
}
return fmt.Sprintf("%s %s", style.commentSymbol(), line)
}

func (style *CommentStyle) commentSymbol() string {
switch *style {
case SlashSlash:
return "// "
return "//"
case SlashStar:
return " * "
return " *"
default:
panic(unknownCommentError())
panic(unknownStyleError())
}
}

func unknownCommentError() string {
func unknownStyleError() string {
return "Unexpected comment style, must be one of: SlashSlash, SlashStar"
}

Expand Down
28 changes: 28 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package main

import (
"encoding/json"
"flag"
"io/ioutil"
)

func main() {
rawConfiguration := readConfiguration()
configuration, err := ParseConfiguration(rawConfiguration)
if err != nil {
panic(err)
}
InsertHeader(configuration)
}

func readConfiguration() Configuration {
configFile := flag.String("configuration", "license.json", "Path to configuration file")
flag.Parse()
file, err := ioutil.ReadFile(*configFile)
if err != nil {
panic(err)
}
rawConfiguration := Configuration{}
json.Unmarshal(file, &rawConfiguration)
return rawConfiguration
}

0 comments on commit a934778

Please sign in to comment.