Skip to content

Commit

Permalink
[#99] (snippet) remove editor workflow for writing title/description
Browse files Browse the repository at this point in the history
In practice its largely been confusing. Instead `-m` will be the sole way for setting title/description and title will default to `-` when not provided
  • Loading branch information
zaquestion committed Jan 16, 2018
1 parent e5de37d commit 51ea66c
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 45 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ test:
bash -c "trap 'trap - SIGINT SIGTERM ERR; mv testdata/.git testdata/test.git; exit 1' SIGINT SIGTERM ERR; $(MAKE) internal-test"

internal-test:
dep ensure
rm coverage-* 2>&1 > /dev/null || true
mv testdata/test.git testdata/.git
go test -coverprofile=coverage-git.out -covermode=count github.com/zaquestion/lab/internal/git
Expand Down
41 changes: 5 additions & 36 deletions cmd/snippetCreate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cmd
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"log"
"os"
Expand Down Expand Up @@ -33,7 +32,7 @@ var snippetCreateCmd = &cobra.Command{
Short: "Create a personal or project snippet",
Long: `
Source snippets from stdin, file, or in editor from scratch
Write title & description in editor, or using -m`,
Optionally add a title & description with -m`,
Run: func(cmd *cobra.Command, args []string) {
remote := forkedFromRemote
if len(args) > 0 {
Expand All @@ -56,7 +55,7 @@ Write title & description in editor, or using -m`,
if strings.TrimSpace(code) == "" {
log.Fatal("aborting snippet due to empty contents")
}
title, body, err := snipMsg(msgs, code)
title, body := snipMsg(msgs)
if title == "" {
log.Fatal("aborting snippet due to empty msg")
}
Expand Down Expand Up @@ -106,38 +105,8 @@ Write title & description in editor, or using -m`,
},
}

func snipMsg(msgs []string, code string) (string, string, error) {
if len(msgs) > 0 {
return msgs[0], strings.Join(msgs[1:], "\n\n"), nil
}

// Read up to the first 30 chars
buf := bytes.NewBufferString(code)
reader := io.LimitReader(buf, 30)
b, err := ioutil.ReadAll(reader)
if err != nil {
return "", "", nil
}
i := bytes.IndexByte(b, '\n')
if i != -1 {
b = b[:i]
}

var tmpl = string(b) + `
{{.CommentChar}} Write a message for this snippet. The first block
{{.CommentChar}} is the title and the rest is the description.`

msg, err := snipText(tmpl)
if err != nil {
log.Fatal(err)
}

title, body, err := git.Edit("SNIPMSG", msg)
if err != nil {
_, f, l, _ := runtime.Caller(0)
log.Fatal(f+":"+strconv.Itoa(l)+" ", err)
}
return title, body, err
func snipMsg(msgs []string) (string, string) {
return msgs[0], strings.Join(msgs[1:], "\n\n")
}

func snipCode(path string) (string, error) {
Expand Down Expand Up @@ -196,7 +165,7 @@ func init() {
snippetCreateCmd.Flags().BoolVarP(&private, "private", "p", false, "Make snippet private; visible only to project members (default: internal)")
snippetCreateCmd.Flags().BoolVar(&public, "public", false, "Make snippet public; can be accessed without any authentication (default: internal)")
snippetCreateCmd.Flags().StringVarP(&name, "name", "n", "", "(optional) Name snippet to add code highlighting, e.g. potato.go for GoLang")
snippetCreateCmd.Flags().StringSliceVarP(&msgs, "message", "m", []string{}, "Use the given <msg>; multiple -m are concatenated as seperate paragraphs")
snippetCreateCmd.Flags().StringSliceVarP(&msgs, "message", "m", []string{"-"}, "Use the given <msg>; multiple -m are concatenated as seperate paragraphs")
snippetCmd.Flags().AddFlagSet(snippetCreateCmd.Flags())
snippetCmd.AddCommand(snippetCreateCmd)
}
12 changes: 3 additions & 9 deletions cmd/snippetCreate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func Test_snippetCreate_Global_Editor(t *testing.T) {
// Write the editor file here, since its tricky to get a file with
// contents in it otherwise. We need a file with contents to
// successfully create the snippet
err = ioutil.WriteFile("/tmp/SNIPCODE_EDITMSG", []byte("test personal snippet contents outside repo"), 0644)
err = ioutil.WriteFile("/tmp/SNIPCODE_EDITMSG", []byte("personal snippet title outside repo"), 0644)
if err != nil {
t.Fatal(err)
}
Expand All @@ -129,14 +129,8 @@ func Test_snippetCreate_Global_Editor(t *testing.T) {
}

func Test_snipMsg(t *testing.T) {
title, desc, err := snipMsg(nil, "snip title\nthis should be dropped")
if err != nil {
t.Fatal(err)
}
// This title was defaulted from the snippet contents/code because no
// msgs -m title was provided
assert.Equal(t, "snip title", title)
// This is the body created in during editing or with provided msgs -m
title, desc := snipMsg(msgs)
assert.Equal(t, "-", title)
assert.Equal(t, "", desc)
}

Expand Down

0 comments on commit 51ea66c

Please sign in to comment.