Skip to content

Commit

Permalink
add command line parser
Browse files Browse the repository at this point in the history
  • Loading branch information
imwithye committed Nov 9, 2021
1 parent f60c46f commit 781f83c
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 5 deletions.
14 changes: 14 additions & 0 deletions args/args.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package args

import (
"gopkg.in/alecthomas/kingpin.v2"
)

var (
Outfile = kingpin.Flag("output", "The output file. Default is .gitignore and use - for stdout.").Default(".gitignore").String()
Templates = kingpin.Arg("templates", "Gitignore templates").Required().Strings()
)

func Parse() {
kingpin.Parse()
}
7 changes: 6 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,9 @@ module gitignore

go 1.16

require github.com/sahilm/fuzzy v0.1.0 // indirect
require (
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 // indirect
github.com/alecthomas/units v0.0.0-20210927113745-59d0afb8317a // indirect
github.com/sahilm/fuzzy v0.1.0 // indirect
gopkg.in/alecthomas/kingpin.v2 v2.2.6 // indirect
)
12 changes: 12 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,14 @@
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 h1:JYp7IbQjafoB+tBA3gMyHYHrpOtNuDiK/uB5uXxq5wM=
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20210927113745-59d0afb8317a h1:E/8AP5dFtMhl5KPJz66Kt9G0n+7Sn41Fy1wv9/jHOrc=
github.com/alecthomas/units v0.0.0-20210927113745-59d0afb8317a/go.mod h1:OMCwj8VM1Kc9e19TLln2VL61YJF0x1XFtfdL4JdbSyE=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sahilm/fuzzy v0.1.0 h1:FzWGaw2Opqyu+794ZQ9SYifWv2EIXpwP4q8dY1kDAwI=
github.com/sahilm/fuzzy v0.1.0/go.mod h1:VFvziUEIMCrT6A6tw2RFIXPXXmzXbOsSHF0DOI8ZK9Y=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
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 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
14 changes: 10 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"embed"
"fmt"
"gitignore/args"
"io/fs"
"io/ioutil"
"os"
Expand Down Expand Up @@ -80,9 +81,9 @@ func fuzzyMatch(arg string, all IgnoreFiles) *IgnoreFile {
}

func main() {
args := os.Args[1:]
args.Parse()
matches := []string{}
for _, arg := range args {
for _, arg := range *args.Templates {
f := fuzzyMatch(arg, getAllIgnores())
if f == nil {
continue
Expand All @@ -93,6 +94,11 @@ func main() {
fmt.Fprintf(os.Stderr, "No gitignore template found.\n")
return
}
fmt.Printf("Gitignore of [%s] writes to .gitignore file.\n", strings.Join(matches, ", "))
ioutil.WriteFile(".gitignore", []byte(gitignore(matches)), 0666)
output := strings.TrimSpace(*args.Outfile)
if output == "-" {
fmt.Println(gitignore(matches))
return
}
fmt.Printf("Gitignore of [%s] writes to %s file.\n", strings.Join(matches, ", "), output)
ioutil.WriteFile(output, []byte(gitignore(matches)), 0666)
}

0 comments on commit 781f83c

Please sign in to comment.