Skip to content

Commit

Permalink
cmd/note_common: move noteRunFn to the common code
Browse files Browse the repository at this point in the history
noteRunFn() is being used as entry code for both mr_note and issue_note,
therefore it makes more sense to move this function to note_common.go
file instead of let it in issue_note.go.

Signed-off-by: Bruno Meneguele <[email protected]>
  • Loading branch information
bmeneg committed Jun 28, 2021
1 parent e9930eb commit a8e7a56
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 73 deletions.
73 changes: 0 additions & 73 deletions cmd/issue_note.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package cmd

import (
"fmt"
"os"

"github.com/rsteube/carapace"
"github.com/spf13/cobra"
"github.com/zaquestion/lab/internal/action"
Expand All @@ -18,76 +15,6 @@ var issueNoteCmd = &cobra.Command{
Run: noteRunFn,
}

func noteRunFn(cmd *cobra.Command, args []string) {
isMR := false
if os.Args[1] == "mr" {
isMR = true
}

reply, branchArgs, err := filterCommentArg(args)
if err != nil {
log.Fatal(err)
}

var (
rn string
idNum int = 0
)

if isMR {
s, mrNum, _ := parseArgsWithGitBranchMR(branchArgs)
if mrNum == 0 {
fmt.Println("Error: Cannot determine MR id.")
os.Exit(1)
}
idNum = int(mrNum)
rn = s
} else {
s, issueNum, _ := parseArgsRemoteAndID(branchArgs)
if issueNum == 0 {
fmt.Println("Error: Cannot determine issue id.")
os.Exit(1)
}
idNum = int(issueNum)
rn = s
}

msgs, err := cmd.Flags().GetStringArray("message")
if err != nil {
log.Fatal(err)
}

filename, err := cmd.Flags().GetString("file")
if err != nil {
log.Fatal(err)
}

linebreak, err := cmd.Flags().GetBool("force-linebreak")
if err != nil {
log.Fatal(err)
}

if reply != 0 {
resolve, err := cmd.Flags().GetBool("resolve")
if err != nil {
log.Fatal(err)
}
// 'lab mr resolve' always overrides options
if os.Args[2] == "resolve" {
resolve = true
}

quote, err := cmd.Flags().GetBool("quote")
if err != nil {
log.Fatal(err)
}
replyNote(rn, isMR, int(idNum), reply, quote, false, filename, linebreak, resolve, msgs)
return
}

createNote(rn, isMR, int(idNum), msgs, filename, linebreak)
}

func init() {
issueNoteCmd.Flags().StringArrayP("message", "m", []string{}, "use the given <msg>; multiple -m are concatenated as separate paragraphs")
issueNoteCmd.Flags().StringP("file", "F", "", "use the given file as the message")
Expand Down
72 changes: 72 additions & 0 deletions cmd/note_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,89 @@ import (
"bytes"
"fmt"
"io/ioutil"
"os"
"runtime"
"strconv"
"strings"
"text/template"

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

func noteRunFn(cmd *cobra.Command, args []string) {
isMR := false
if os.Args[1] == "mr" {
isMR = true
}

reply, branchArgs, err := filterCommentArg(args)
if err != nil {
log.Fatal(err)
}

var (
rn string
idNum int = 0
)

if isMR {
s, mrNum, _ := parseArgsWithGitBranchMR(branchArgs)
if mrNum == 0 {
fmt.Println("Error: Cannot determine MR id.")
os.Exit(1)
}
idNum = int(mrNum)
rn = s
} else {
s, issueNum, _ := parseArgsRemoteAndID(branchArgs)
if issueNum == 0 {
fmt.Println("Error: Cannot determine issue id.")
os.Exit(1)
}
idNum = int(issueNum)
rn = s
}

msgs, err := cmd.Flags().GetStringArray("message")
if err != nil {
log.Fatal(err)
}

filename, err := cmd.Flags().GetString("file")
if err != nil {
log.Fatal(err)
}

linebreak, err := cmd.Flags().GetBool("force-linebreak")
if err != nil {
log.Fatal(err)
}

if reply != 0 {
resolve, err := cmd.Flags().GetBool("resolve")
if err != nil {
log.Fatal(err)
}
// 'lab mr resolve' always overrides options
if os.Args[2] == "resolve" {
resolve = true
}

quote, err := cmd.Flags().GetBool("quote")
if err != nil {
log.Fatal(err)
}
replyNote(rn, isMR, int(idNum), reply, quote, false, filename, linebreak, resolve, msgs)
return
}

createNote(rn, isMR, int(idNum), msgs, filename, linebreak)
}

func createNote(rn string, isMR bool, idNum int, msgs []string, filename string, linebreak bool) {
var err error

Expand Down

0 comments on commit a8e7a56

Please sign in to comment.