From 781f83c66a4a27bd65bd01cc9b93cc7f6e52cff6 Mon Sep 17 00:00:00 2001 From: Yiwei Gong Date: Tue, 9 Nov 2021 23:40:42 +0800 Subject: [PATCH] add command line parser --- args/args.go | 14 ++++++++++++++ go.mod | 7 ++++++- go.sum | 12 ++++++++++++ main.go | 14 ++++++++++---- 4 files changed, 42 insertions(+), 5 deletions(-) create mode 100644 args/args.go diff --git a/args/args.go b/args/args.go new file mode 100644 index 0000000..4173da7 --- /dev/null +++ b/args/args.go @@ -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() +} diff --git a/go.mod b/go.mod index b836ea2..b22d211 100644 --- a/go.mod +++ b/go.mod @@ -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 +) diff --git a/go.sum b/go.sum index bd17606..11874d2 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/main.go b/main.go index b60023a..a8c8af6 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import ( "embed" "fmt" + "gitignore/args" "io/fs" "io/ioutil" "os" @@ -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 @@ -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) }