Skip to content

Commit

Permalink
(snippet) check for input on standard in when deciding to create a sn…
Browse files Browse the repository at this point in the history
…ippet with `lab snippet`

This is better than just checking if its a global snippet since it works for project and personal snippets. It also forces snippets created from scratch (editor workflow) to explicitly use `lab snippet create`
  • Loading branch information
zaquestion committed Jan 17, 2018
1 parent e275c7a commit c6ed853
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 7 deletions.
11 changes: 7 additions & 4 deletions cmd/snippet.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cmd

import (
"os"

"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -28,16 +30,17 @@ var snippetCmd = &cobra.Command{
return
}

if global, _ := cmd.Flags().GetBool("global"); global {
if stat, _ := os.Stdin.Stat(); (stat.Mode() & os.ModeCharDevice) == 0 {
snippetCreateCmd.Run(cmd, args)
return
}

if len(args) == 0 && file == "" {
cmd.Help()
if !(len(args) == 0 || file == "") {
snippetCreateCmd.Run(cmd, args)
return
}
snippetCreateCmd.Run(cmd, args)

cmd.Help()
},
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/snippetCreate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func Test_snippetCreate_Global_Editor(t *testing.T) {
t.Fatal(err)
}

cmd = exec.Command(os.ExpandEnv("$GOPATH/src/github.com/zaquestion/lab/lab_bin"), "snippet", "-g")
cmd = exec.Command(os.ExpandEnv("$GOPATH/src/github.com/zaquestion/lab/lab_bin"), "snippet", "create", "-g")
cmd.Env = []string{"PATH=/usr/local/bin:/usr/bin:/bin", "EDITOR=test -f"}
cmd.Dir = repo

Expand Down
2 changes: 1 addition & 1 deletion cmd/snippetList_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func Test_snippetList(t *testing.T) {
func Test_snippetList_Global(t *testing.T) {
t.Parallel()
repo := copyTestRepo(t)
cmd := exec.Command("../lab_bin", "snippet", "list", "-g")
cmd := exec.Command("../lab_bin", "snippet", "-l", "-g")
cmd.Dir = repo

b, err := cmd.CombinedOutput()
Expand Down
2 changes: 1 addition & 1 deletion cmd/snippet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func Test_snippetCmd(t *testing.T) {
var snipID string
t.Run("create_personal", func(t *testing.T) {
repo := copyTestRepo(t)
cmd := exec.Command("../lab_bin", "snippet", "create", "-g",
cmd := exec.Command("../lab_bin", "snippet", "-g",
"-m", "personal snippet title",
"-m", "personal snippet description")
cmd.Dir = repo
Expand Down

0 comments on commit c6ed853

Please sign in to comment.