Skip to content

Commit

Permalink
cmd/edit: move editText to the common file
Browse files Browse the repository at this point in the history
editText() is being used not only for issues, cmd/edit_common.go is a
better place for it. Its commit description was also misleading.

Signed-off-by: Bruno Meneguele <[email protected]>
  • Loading branch information
bmeneg committed Feb 2, 2022
1 parent e5c1c1f commit 32a1bd3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 34 deletions.
34 changes: 34 additions & 0 deletions cmd/edit_common.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package cmd

import (
"bytes"
"fmt"
"io/ioutil"
"strings"
"text/template"

"github.com/MakeNowJust/heredoc/v2"
"github.com/zaquestion/lab/internal/git"
)

Expand Down Expand Up @@ -81,3 +84,34 @@ func editGetTitleDescFromFile(filename string) (string, string, error) {

return title, body, nil
}

// editText places the text title and body in a specific template following Git
// standards.
func editText(title string, body string) (string, error) {
tmpl := heredoc.Doc(`
{{.InitMsg}}
{{.CommentChar}} Edit the title and/or description. The first block of text
{{.CommentChar}} is the title and the rest is the description.`)

msg := &struct {
InitMsg string
CommentChar string
}{
InitMsg: title + "\n\n" + body,
CommentChar: git.CommentChar(),
}

t, err := template.New("tmpl").Parse(tmpl)
if err != nil {
return "", err
}

var b bytes.Buffer
err = t.Execute(&b, msg)
if err != nil {
return "", err
}

return b.String(), nil
}
34 changes: 0 additions & 34 deletions cmd/issue_edit.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
package cmd

import (
"bytes"
"fmt"
"runtime"
"strconv"
"strings"
"text/template"

"github.com/MakeNowJust/heredoc/v2"
"github.com/rsteube/carapace"
"github.com/spf13/cobra"
gitlab "github.com/xanzy/go-gitlab"
"github.com/zaquestion/lab/internal/action"
"github.com/zaquestion/lab/internal/git"
lab "github.com/zaquestion/lab/internal/gitlab"
)

Expand Down Expand Up @@ -190,37 +187,6 @@ func issueGetCurrentAssignees(issue *gitlab.Issue) []string {
return currentAssignees
}

// editText returns an issue editing template that is suitable for loading
// into an editor
func editText(title string, body string) (string, error) {
tmpl := heredoc.Doc(`
{{.InitMsg}}
{{.CommentChar}} Edit the title and/or description. The first block of text
{{.CommentChar}} is the title and the rest is the description.`)

msg := &struct {
InitMsg string
CommentChar string
}{
InitMsg: title + "\n\n" + body,
CommentChar: git.CommentChar(),
}

t, err := template.New("tmpl").Parse(tmpl)
if err != nil {
return "", err
}

var b bytes.Buffer
err = t.Execute(&b, msg)
if err != nil {
return "", err
}

return b.String(), nil
}

func init() {
issueEditCmd.Flags().StringArrayP("message", "m", []string{}, "use the given <msg>; multiple -m are concatenated as separate paragraphs")
issueEditCmd.Flags().StringSliceP("label", "l", []string{}, "add the given label(s) to the issue")
Expand Down

0 comments on commit 32a1bd3

Please sign in to comment.