Skip to content

Commit

Permalink
Merge pull request #49 from shihanng/issues-40__search
Browse files Browse the repository at this point in the history
Add search subcommand
  • Loading branch information
shihanng committed Dec 13, 2019
2 parents 99c2a05 + 0828371 commit 6f0f241
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 43 deletions.
Binary file added .github/images/search.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,8 @@ issues:
linters:
- lll
text: "line is 146 characters"

- path: cmd/search.go
linters:
- gosec
text: "G204: Subprocess launched with function call as argument or cmd arguments"
29 changes: 10 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ go get github.com/shihanng/gig

# Usage

Use the supported language as input arguments, e.g. `Go Elm`.
1. Use the supported language as input arguments, e.g. `Go Elm`.

```
gig gen Go Elm
$ gig gen Go Elm
### Elm ###
# elm-package generated files
Expand All @@ -52,25 +52,16 @@ repl-temp-*
### Go ###
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib
# Test binary, built with `go test -c`
*.test
# Output of the go coverage tool, specifically when used with LiteIDE
*.out
...
```

# Dependency directories (remove the comment below to include it)
# vendor/
2. Use the search functionality (depends on [fzf](https://github.com/junegunn/fzf))

### Go Patch ###
/vendor/
/Godeps/
```
$ gig search
```

![gig search demo](./.github/images/search.gif)

At the very first run the program will clone the templates repository <https://github.com/toptal/gitignore.git>
into `$XDG_CACHE_HOME/gig`.
Expand All @@ -82,7 +73,7 @@ For more information:
gig --help
```

# Contributing
# Contribute

Found a bug or want to hack around? Clone the repository:

Expand Down
22 changes: 1 addition & 21 deletions cmd/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ THE SOFTWARE.
package cmd

import (
"path/filepath"

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

Expand All @@ -44,21 +40,5 @@ https://github.com/toptal/gitignore.git into $XDG_CACHE_HOME/gig.`,
}

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

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

items = file.Sort(items, orders)

wc, err := c.newWriteCloser()
if err != nil {
return err
}

defer wc.Close()

return file.Generate(wc, c.templatePath(), items...)
return c.generateIgnoreFile(args)
}
34 changes: 31 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,19 @@ import (

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

func Execute(w io.Writer, version string) {
command := &command{
output: w,
cachePath: filepath.Join(xdg.CacheHome(), `gig`),
version: version,
output: w,
cachePath: filepath.Join(xdg.CacheHome(), `gig`),
version: version,
searchTool: "fzf -m",
}

rootCmd := newRootCmd(command)
Expand All @@ -53,10 +56,16 @@ func Execute(w io.Writer, version string) {
genCmd.Flags().BoolVarP(&command.genIsFile, "file", "f", false,
"if specified will create .gitignore file in the current working directory")

searchCmd := newSearchCmd(command)

searchCmd.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),
searchCmd,
)

if err := rootCmd.Execute(); err != nil {
Expand All @@ -80,6 +89,7 @@ type command struct {
commitHash string
cachePath string
version string
searchTool string

genIsFile bool
}
Expand All @@ -100,6 +110,24 @@ func (c *command) rootRunE(cmd *cobra.Command, args []string) error {
return nil
}

func (c *command) generateIgnoreFile(items []string) error {
orders, err := order.ReadOrder(filepath.Join(c.templatePath(), `order`))
if err != nil {
return err
}

items = file.Sort(items, orders)

wc, err := c.newWriteCloser()
if err != nil {
return err
}

defer wc.Close()

return file.Generate(wc, c.templatePath(), items...)
}

func (c *command) newWriteCloser() (io.WriteCloser, error) {
if c.genIsFile {
f, err := os.Create(".gitignore")
Expand Down
88 changes: 88 additions & 0 deletions cmd/search.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
Copyright © 2019 Shi Han NG <shihanng@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package cmd

import (
"bufio"
"bytes"
"os"
"os/exec"
"runtime"
"strings"

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

func newSearchCmd(c *command) *cobra.Command {
return &cobra.Command{
Use: "search",
Short: "Search and select supported templates list",
Long: `Search and select supported templates list.
This subcommand depends on fzf (https://github.com/junegunn/fzf)
for the search functionality.`,
RunE: func(cmd *cobra.Command, args []string) error {
templates, err := file.List(c.templatePath())
if err != nil {
return err
}
templateStr := strings.Join(templates, "\n")

var selected bytes.Buffer

cmdName, cmdArgs := "sh", []string{"-c"}
if runtime.GOOS == "windows" {
cmdName = "cmd"
cmdArgs[0] = "/c"
}

shCmd := exec.Command(cmdName, append(cmdArgs, c.searchTool)...)

shCmd.Stdin = strings.NewReader(templateStr)
shCmd.Stdout = &selected
shCmd.Stderr = os.Stderr

if err := shCmd.Run(); err != nil {
var ee *exec.ExitError
if errors.As(err, &ee) && ee.ExitCode() == 130 {
return nil
}
return errors.Wrap(err, "cmd: searching")
}

scanner := bufio.NewScanner(&selected)

var items []string
for scanner.Scan() {
items = append(items, strings.TrimSpace(scanner.Text()))
}

if err := scanner.Err(); err != nil {
return errors.Wrap(err, "cmd: read search result")
}

return c.generateIgnoreFile(items)
},
}
}

0 comments on commit 6f0f241

Please sign in to comment.