Skip to content

Commit

Permalink
(#9) Command line flags (#22)
Browse files Browse the repository at this point in the history
* Filters and repo path are specified via command
  line flags
* Can also specify flags via '.gitlint' file in
  $PWD (command line flags take precedence).
  The format of this file is the same as command
  line flags, where each is on a separate line.
  • Loading branch information
llorllale committed Mar 2, 2019
1 parent 76bb489 commit 4be918f
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 8 deletions.
36 changes: 28 additions & 8 deletions cmd/go-gitlint/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,46 @@ import (
"github.com/llorllale/go-gitlint/internal/commits"
"github.com/llorllale/go-gitlint/internal/issues"
"github.com/llorllale/go-gitlint/internal/repo"
kingpin "gopkg.in/alecthomas/kingpin.v2"
)

// @todo #4 The path should be passed in as a command line
// flag instead of being hard coded. All other configuration
// options should be passed in through CLI as well.
// @todo #9 Global variables are a code smell (especially those in filterse.go).
// They promote coupling across different components inside the same package.
// Figure out a way to remove these global variables. Whatever command line
// parser we choose should be able to auto-generate usage.
var path = kingpin.Flag("path", `Path to the git repo ("." by default).`).Default(".").String() //nolint[gochecknoglobals]

func main() {
configure()
os.Exit(
len(
issues.Printed(
os.Stdout, "\n",
issues.Collected(
[]issues.Filter{
issues.OfSubjectRegex(".{,1}"),
issues.OfBodyRegex(".{,1}"),
},
issues.Filters(),
commits.In(
repo.Filesystem("."),
repo.Filesystem(*path),
),
),
)(),
),
)
}

func configure() {
var args []string
if len(os.Args) > 1 {
args = append(args, os.Args[1:]...)
}
const file = ".gitlint"
if _, err := os.Stat(file); err == nil {
config, err := kingpin.ExpandArgsFromFile(file)
if err != nil {
panic(err)
}
args = append(args, config...)
}
if _, err := kingpin.CommandLine.Parse(args); err != nil {
panic(err)
}
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
module github.com/llorllale/go-gitlint

require (
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc // indirect
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf // indirect
github.com/google/uuid v1.1.0
github.com/stretchr/testify v1.2.2
gopkg.in/alecthomas/kingpin.v2 v2.2.6
gopkg.in/src-d/go-git.v4 v4.10.0
)
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
github.com/alcortesm/tgz v0.0.0-20161220082320-9c5fe88206d7 h1:uSoVVbwJiQipAclBbw+8quDsfcvFjOpI5iCf4p/cqCs=
github.com/alcortesm/tgz v0.0.0-20161220082320-9c5fe88206d7/go.mod h1:6zEj6s6u/ghQa61ZWa/C2Aw3RkjiTBOix7dkqa1VLIs=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc h1:cAKDfWh5VpdgMhJosfJnn5/FoN2SRZ4p7fJNX58YPaU=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf h1:qet1QNfXsQxTZqLG4oE62mJzwPIB8+Tee4RNCL9ulrY=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239 h1:kFOfPq6dUM1hTo4JG6LR5AXSUEsOjtdm0kw0FtQtMJA=
github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYUyUczH0OGQWaF5ceTx0UBShxjsH6f8oGKYe2c=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
Expand Down Expand Up @@ -48,6 +52,8 @@ golang.org/x/sys v0.0.0-20180903190138-2b024373dcd9 h1:lkiLiLBHGoH3XnqSLUIaBsilG
golang.org/x/sys v0.0.0-20180903190138-2b024373dcd9/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
gopkg.in/alecthomas/kingpin.v2 v2.2.6 h1:jMFz6MfLP0/4fUyZle81rXUoxOBFi19VUFKVDOQfozc=
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/src-d/go-billy.v4 v4.2.1 h1:omN5CrMrMcQ+4I8bJ0wEhOBPanIRWzFC953IiXKdYzo=
Expand Down
18 changes: 18 additions & 0 deletions internal/issues/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,31 @@ import (
"regexp"

"github.com/llorllale/go-gitlint/internal/commits"
kingpin "gopkg.in/alecthomas/kingpin.v2"
)

var (
subjectRegex = kingpin.Flag("subject-regex", "Filters commit subjects based on a regular expression.").String() //nolint[gochecknoglobals]
bodyRegex = kingpin.Flag("body-regex", "Filters commit message bodies based on a regular expression.").String() //nolint[gochecknoglobals]
)

// Filter identifies an issue with a commit.
// A filter returning a zero-valued Issue signals that it found no issue
// with the commit.
type Filter func(*commits.Commit) Issue

// Filters returns all filters configured by the user.
func Filters() []Filter {
filters := make([]Filter, 0)
if subjectRegex != nil {
filters = append(filters, OfSubjectRegex(*subjectRegex))
}
if bodyRegex != nil {
filters = append(filters, OfBodyRegex(*bodyRegex))
}
return filters
}

// OfSubjectRegex tests a commit's subject with the regex.
func OfSubjectRegex(regex string) Filter {
return func(c *commits.Commit) Issue {
Expand Down
8 changes: 8 additions & 0 deletions internal/issues/filters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ import (
"github.com/stretchr/testify/assert"
)

func TestFilters(t *testing.T) {
sr := "abc123"
br := "bodyRegex"
subjectRegex = &sr
bodyRegex = &br
assert.Len(t, Filters(), 2)
}

func TestOfSubjectRegexMatch(t *testing.T) {
assert.Zero(t,
OfSubjectRegex(`\(#\d+\) [\w ]{10,50}`)(
Expand Down

2 comments on commit 4be918f

@0pdd
Copy link

@0pdd 0pdd commented on 4be918f Mar 2, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 4-2f6700f4 disappeared from cmd/go-gitlint/main.go, that's why I closed #9. Please, remember that the puzzle was not necessarily removed in this particular commit. Maybe it happened earlier, but we discovered this fact only now.

@0pdd
Copy link

@0pdd 0pdd commented on 4be918f Mar 2, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 9-05e94b28 discovered in cmd/go-gitlint/main.go and submitted as #23. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

Please sign in to comment.