Skip to content

Commit 6f0f241

Browse files
authored
Merge pull request #49 from shihanng/issues-40__search
Add search subcommand
2 parents 99c2a05 + 0828371 commit 6f0f241

File tree

6 files changed

+135
-43
lines changed

6 files changed

+135
-43
lines changed

.github/images/search.gif

231 KB
Loading

.golangci.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,8 @@ issues:
2929
linters:
3030
- lll
3131
text: "line is 146 characters"
32+
33+
- path: cmd/search.go
34+
linters:
35+
- gosec
36+
text: "G204: Subprocess launched with function call as argument or cmd arguments"

README.md

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ go get github.com/shihanng/gig
3939

4040
# Usage
4141

42-
Use the supported language as input arguments, e.g. `Go Elm`.
42+
1. Use the supported language as input arguments, e.g. `Go Elm`.
4343

4444
```
45-
gig gen Go Elm
45+
$ gig gen Go Elm
4646
4747
### Elm ###
4848
# elm-package generated files
@@ -52,25 +52,16 @@ repl-temp-*
5252
5353
### Go ###
5454
# Binaries for programs and plugins
55-
*.exe
56-
*.exe~
57-
*.dll
58-
*.so
59-
*.dylib
60-
61-
# Test binary, built with `go test -c`
62-
*.test
63-
64-
# Output of the go coverage tool, specifically when used with LiteIDE
65-
*.out
55+
...
56+
```
6657

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

70-
### Go Patch ###
71-
/vendor/
72-
/Godeps/
7360
```
61+
$ gig search
62+
```
63+
64+
![gig search demo](./.github/images/search.gif)
7465

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

85-
# Contributing
76+
# Contribute
8677

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

cmd/gen.go

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@ THE SOFTWARE.
2222
package cmd
2323

2424
import (
25-
"path/filepath"
26-
27-
"github.com/shihanng/gig/internal/file"
28-
"github.com/shihanng/gig/internal/order"
2925
"github.com/spf13/cobra"
3026
)
3127

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

4642
func (c *command) genRunE(cmd *cobra.Command, args []string) error {
47-
items := args
48-
49-
orders, err := order.ReadOrder(filepath.Join(c.templatePath(), `order`))
50-
if err != nil {
51-
return err
52-
}
53-
54-
items = file.Sort(items, orders)
55-
56-
wc, err := c.newWriteCloser()
57-
if err != nil {
58-
return err
59-
}
60-
61-
defer wc.Close()
62-
63-
return file.Generate(wc, c.templatePath(), items...)
43+
return c.generateIgnoreFile(args)
6444
}

cmd/root.go

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,19 @@ import (
2828

2929
"github.com/OpenPeeDeeP/xdg"
3030
"github.com/cockroachdb/errors"
31+
"github.com/shihanng/gig/internal/file"
32+
"github.com/shihanng/gig/internal/order"
3133
"github.com/shihanng/gig/internal/repo"
3234
"github.com/spf13/cobra"
3335
"gopkg.in/src-d/go-git.v4/utils/ioutil"
3436
)
3537

3638
func Execute(w io.Writer, version string) {
3739
command := &command{
38-
output: w,
39-
cachePath: filepath.Join(xdg.CacheHome(), `gig`),
40-
version: version,
40+
output: w,
41+
cachePath: filepath.Join(xdg.CacheHome(), `gig`),
42+
version: version,
43+
searchTool: "fzf -m",
4144
}
4245

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

59+
searchCmd := newSearchCmd(command)
60+
61+
searchCmd.Flags().BoolVarP(&command.genIsFile, "file", "f", false,
62+
"if specified will create .gitignore file in the current working directory")
63+
5664
rootCmd.AddCommand(
5765
newListCmd(command),
5866
genCmd,
5967
newVersionCmd(command),
68+
searchCmd,
6069
)
6170

6271
if err := rootCmd.Execute(); err != nil {
@@ -80,6 +89,7 @@ type command struct {
8089
commitHash string
8190
cachePath string
8291
version string
92+
searchTool string
8393

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

113+
func (c *command) generateIgnoreFile(items []string) error {
114+
orders, err := order.ReadOrder(filepath.Join(c.templatePath(), `order`))
115+
if err != nil {
116+
return err
117+
}
118+
119+
items = file.Sort(items, orders)
120+
121+
wc, err := c.newWriteCloser()
122+
if err != nil {
123+
return err
124+
}
125+
126+
defer wc.Close()
127+
128+
return file.Generate(wc, c.templatePath(), items...)
129+
}
130+
103131
func (c *command) newWriteCloser() (io.WriteCloser, error) {
104132
if c.genIsFile {
105133
f, err := os.Create(".gitignore")

cmd/search.go

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
Copyright © 2019 Shi Han NG <shihanng@gmail.com>
3+
4+
Permission is hereby granted, free of charge, to any person obtaining a copy
5+
of this software and associated documentation files (the "Software"), to deal
6+
in the Software without restriction, including without limitation the rights
7+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
copies of the Software, and to permit persons to whom the Software is
9+
furnished to do so, subject to the following conditions:
10+
11+
The above copyright notice and this permission notice shall be included in
12+
all copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
THE SOFTWARE.
21+
*/
22+
package cmd
23+
24+
import (
25+
"bufio"
26+
"bytes"
27+
"os"
28+
"os/exec"
29+
"runtime"
30+
"strings"
31+
32+
"github.com/cockroachdb/errors"
33+
"github.com/shihanng/gig/internal/file"
34+
"github.com/spf13/cobra"
35+
)
36+
37+
func newSearchCmd(c *command) *cobra.Command {
38+
return &cobra.Command{
39+
Use: "search",
40+
Short: "Search and select supported templates list",
41+
Long: `Search and select supported templates list.
42+
43+
This subcommand depends on fzf (https://github.com/junegunn/fzf)
44+
for the search functionality.`,
45+
RunE: func(cmd *cobra.Command, args []string) error {
46+
templates, err := file.List(c.templatePath())
47+
if err != nil {
48+
return err
49+
}
50+
templateStr := strings.Join(templates, "\n")
51+
52+
var selected bytes.Buffer
53+
54+
cmdName, cmdArgs := "sh", []string{"-c"}
55+
if runtime.GOOS == "windows" {
56+
cmdName = "cmd"
57+
cmdArgs[0] = "/c"
58+
}
59+
60+
shCmd := exec.Command(cmdName, append(cmdArgs, c.searchTool)...)
61+
62+
shCmd.Stdin = strings.NewReader(templateStr)
63+
shCmd.Stdout = &selected
64+
shCmd.Stderr = os.Stderr
65+
66+
if err := shCmd.Run(); err != nil {
67+
var ee *exec.ExitError
68+
if errors.As(err, &ee) && ee.ExitCode() == 130 {
69+
return nil
70+
}
71+
return errors.Wrap(err, "cmd: searching")
72+
}
73+
74+
scanner := bufio.NewScanner(&selected)
75+
76+
var items []string
77+
for scanner.Scan() {
78+
items = append(items, strings.TrimSpace(scanner.Text()))
79+
}
80+
81+
if err := scanner.Err(); err != nil {
82+
return errors.Wrap(err, "cmd: read search result")
83+
}
84+
85+
return c.generateIgnoreFile(items)
86+
},
87+
}
88+
}

0 commit comments

Comments
 (0)