From a934778a1f2f85e9396129e635a794f91803327a Mon Sep 17 00:00:00 2001 From: Florent Biville Date: Mon, 24 Sep 2018 23:34:04 -0400 Subject: [PATCH] Add main function --- .gitignore | 1 + Gopkg.lock | 16 +++++++++++++++- configuration.go | 22 +++++++++++++--------- configuration_test.go | 36 ++++++++++++++++++------------------ header.go | 42 +++++++++++++++++++++++++----------------- header_test.go | 10 ++++++---- line_comment.go | 28 +++++++++++++++++++++------- main.go | 28 ++++++++++++++++++++++++++++ 8 files changed, 127 insertions(+), 56 deletions(-) create mode 100644 main.go diff --git a/.gitignore b/.gitignore index 9e5d3c3..3240f13 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .idea /vendor +header diff --git a/Gopkg.lock b/Gopkg.lock index ad616d8..6d54883 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -1,6 +1,17 @@ # This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'. +[[projects]] + branch = "master" + digest = "1:36aebe90a13cf9128280ac834399b8bebf83685283c78df279d61c46bb2a8d83" + name = "github.com/mattn/go-zglob" + packages = [ + ".", + "fastwalk", + ] + pruneopts = "UT" + revision = "2ea3427bfa539cca900ca2768d8663ecc8a708c1" + [[projects]] digest = "1:ab54eea8d482272009e9e4af07d4d9b5236c27b4d8c54a3f2c99d163be883eca" name = "github.com/onsi/gomega" @@ -71,6 +82,9 @@ [solve-meta] analyzer-name = "dep" analyzer-version = 1 - input-imports = ["github.com/onsi/gomega"] + input-imports = [ + "github.com/mattn/go-zglob", + "github.com/onsi/gomega", + ] solver-name = "gps-cdcl" solver-version = 1 diff --git a/configuration.go b/configuration.go index 36e655d..ee68d13 100644 --- a/configuration.go +++ b/configuration.go @@ -1,4 +1,4 @@ -package header +package main import ( "bufio" @@ -8,6 +8,14 @@ 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 @@ -15,19 +23,15 @@ type configuration struct { 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 } diff --git a/configuration_test.go b/configuration_test.go index 9566f7b..955a73f 100644 --- a/configuration_test.go +++ b/configuration_test.go @@ -1,4 +1,4 @@ -package header_test +package main_test import ( . "github.com/fbiville/header" @@ -9,19 +9,19 @@ 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"})) } @@ -29,20 +29,20 @@ func TestConfigurationInitWithLineCommentStyle(t *testing.T) { 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"})) diff --git a/header.go b/header.go index 5608b2c..8fbf4e4 100644 --- a/header.go +++ b/header.go @@ -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) } @@ -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 } @@ -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() } } diff --git a/header_test.go b/header_test.go index a627df6..6c9cc53 100644 --- a/header_test.go +++ b/header_test.go @@ -1,4 +1,4 @@ -package header +package main import ( "bufio" @@ -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`)) } @@ -38,7 +39,7 @@ func TestHeaderDoesNotWriteTwice(t *testing.T) { writer: writer, } - Insert(&configuration) + InsertHeader(&configuration) writer.Flush() I.Expect(stringBuilder.String()).To(Equal(``)) @@ -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`)) } diff --git a/line_comment.go b/line_comment.go index 9891b67..98f6815 100644 --- a/line_comment.go +++ b/line_comment.go @@ -1,4 +1,4 @@ -package header +package main import ( "fmt" @@ -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 } @@ -22,7 +33,7 @@ func (style *CommentStyle) closing() bool { func (style *CommentStyle) open() string { switch *style { case SlashStar: - return "/* " + return "/*" default: panic(unknownSurroundingCommentError()) } @@ -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" } diff --git a/main.go b/main.go new file mode 100644 index 0000000..7c9a431 --- /dev/null +++ b/main.go @@ -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 +}