Skip to content

Commit

Permalink
Merge pull request #46 from shihanng/issues-45__out
Browse files Browse the repository at this point in the history
Add -f option
  • Loading branch information
shihanng committed Dec 11, 2019
2 parents d2d570e + 1fda941 commit 6d57494
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 66 deletions.
30 changes: 19 additions & 11 deletions cmd/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,14 @@ THE SOFTWARE.
package cmd

import (
"io"
"path/filepath"

"github.com/shihanng/gig/internal/file"
"github.com/shihanng/gig/internal/order"
"github.com/spf13/cobra"
)

func newGenCmd(w io.Writer, templatePath string) *cobra.Command {
func newGenCmd(c *command) *cobra.Command {
return &cobra.Command{
Use: "gen [template name]",
Short: "Generates .gitignore of the given inputs",
Expand All @@ -40,17 +39,26 @@ Valid names can be obtained from the list subcommand.
At the very first run the program will clone the templates repository
https://github.com/toptal/gitignore.git into $XDG_CACHE_HOME/gig.`,
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
items := args
RunE: c.genRunE,
}
}

func (c *command) genRunE(cmd *cobra.Command, args []string) error {
items := args

orders, err := order.ReadOrder(filepath.Join(templatePath, `templates`, `order`))
if err != nil {
return err
}
orders, err := order.ReadOrder(filepath.Join(c.templatePath, `templates`, `order`))
if err != nil {
return err
}

items = file.Sort(items, orders)
items = file.Sort(items, orders)

return file.Generate(w, filepath.Join(templatePath, `templates`), items...)
},
wc, err := c.newWriteCloser()
if err != nil {
return err
}

defer wc.Close()

return file.Generate(wc, filepath.Join(c.templatePath, `templates`), items...)
}
32 changes: 17 additions & 15 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,26 @@ import (
"github.com/spf13/cobra"
)

func newListCmd(w io.Writer, templatePath string) *cobra.Command {
func newListCmd(c *command) *cobra.Command {
return &cobra.Command{
Use: "list",
Short: "List all supported templates",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
templates, err := file.List(filepath.Join(templatePath, `templates`))
if err != nil {
return err
}

for _, t := range templates {
if _, err := io.WriteString(w, t+"\n"); err != nil {
return errors.Wrap(err, "cmd/list: outputing")
}
}

return nil
},
RunE: c.listRunE,
}
}

func (c *command) listRunE(cmd *cobra.Command, args []string) error {
templates, err := file.List(filepath.Join(c.templatePath, `templates`))
if err != nil {
return err
}

for _, t := range templates {
if _, err := io.WriteString(c.output, t+"\n"); err != nil {
return errors.Wrap(err, "cmd/list: outputing")
}
}

return nil
}
92 changes: 59 additions & 33 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,64 +22,90 @@ THE SOFTWARE.
package cmd

import (
"fmt"
"io"
"os"
"path/filepath"

"github.com/OpenPeeDeeP/xdg"
"github.com/cockroachdb/errors"
"github.com/shihanng/gig/internal/repo"
"github.com/spf13/cobra"
"gopkg.in/src-d/go-git.v4/utils/ioutil"
)

func newRootCmd() *cobra.Command {
func Execute(w io.Writer, version string) {
command := &command{
output: w,
templatePath: filepath.Join(xdg.CacheHome(), `gig`),
version: version,
}

rootCmd := newRootCmd(command)

rootCmd.PersistentFlags().StringVarP(&command.commitHash, "commit-hash", "c", "",
"use templates from a specific commit hash of github.com/toptal/gitignore")

genCmd := newGenCmd(command)

genCmd.Flags().BoolVarP(&command.genIsFile, "file", "f", false,
"if specified will create .gitignore file in the current working directory")

rootCmd.AddCommand(
newListCmd(command),
genCmd,
newVersionCmd(command),
)

if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}
}

func newRootCmd(c *command) *cobra.Command {
return &cobra.Command{
Use: "gig",
Short: "A tool that generates .gitignore",
Long: `gig is a command line tool to help you create useful .gitignore files
for your project. It is inspired by gitignore.io and make use of
the large collection of useful .gitignore templates of the web service.`,
PersistentPreRunE: c.RootRunE,
}
}

func rootCmdRun(templatePath string, commitHash *string) func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, args []string) error {
r, err := repo.New(templatePath, repo.SourceRepo)
if err != nil {
return err
}

ch, err := repo.Checkout(r, *commitHash)
if err != nil {
return err
}
type command struct {
output io.Writer
commitHash string
templatePath string
version string

*commitHash = ch

return nil
}
genIsFile bool
}

func Execute(w io.Writer, version string) {
templatePath := filepath.Join(xdg.CacheHome(), `gig`)

var commitHash string
func (c *command) RootRunE(cmd *cobra.Command, args []string) error {
r, err := repo.New(c.templatePath, repo.SourceRepo)
if err != nil {
return err
}

rootCmd := newRootCmd()
ch, err := repo.Checkout(r, c.commitHash)
if err != nil {
return err
}

rootCmd.PersistentFlags().StringVarP(&commitHash, "commit-hash", "c", "",
"use templates from a specific commit hash of github.com/toptal/gitignore")
c.commitHash = ch

rootCmd.PersistentPreRunE = rootCmdRun(templatePath, &commitHash)
return nil
}

rootCmd.AddCommand(
newListCmd(w, templatePath),
newGenCmd(w, templatePath),
newVersionCmd(w, templatePath, version, &commitHash),
)
func (c *command) newWriteCloser() (io.WriteCloser, error) {
if c.genIsFile {
f, err := os.Create(".gitignore")
if err != nil {
return nil, errors.Wrap(err, "cmd: create new file")
}

if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
return f, nil
}

return ioutil.WriteNopCloser(c.output), nil
}
15 changes: 8 additions & 7 deletions cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,20 @@ package cmd

import (
"fmt"
"io"

"github.com/spf13/cobra"
)

func newVersionCmd(w io.Writer, templatePath, version string, commitHash *string) *cobra.Command {
func newVersionCmd(c *command) *cobra.Command {
return &cobra.Command{
Use: "version",
Short: "Print the version number and other useful info",
Run: func(cmd *cobra.Command, args []string) {
fmt.Fprintf(w, "gig version %s\n", version)
fmt.Fprintf(w, "Cached github.com/toptal/gitignore in: %s\n", templatePath)
fmt.Fprintf(w, "Using github.com/toptal/gitignore commit hash: %s\n", *commitHash)
},
Run: c.versionRunE,
}
}

func (c *command) versionRunE(cmd *cobra.Command, args []string) {
fmt.Fprintf(c.output, "gig version %s\n", c.version)
fmt.Fprintf(c.output, "Cached github.com/toptal/gitignore in: %s\n", c.templatePath)
fmt.Fprintf(c.output, "Using github.com/toptal/gitignore commit hash: %s\n", c.commitHash)
}

0 comments on commit 6d57494

Please sign in to comment.