Skip to content

Commit

Permalink
edit_common: add func to get title and body from file
Browse files Browse the repository at this point in the history
Move the code being used in mr_create for the -F flag, to get the MR title
and description from a file, to edit_common, letting other commands to use
the same functionality.

Signed-off-by: Bruno Meneguele <[email protected]>
  • Loading branch information
bmeneg committed Mar 4, 2021
1 parent 50add0a commit eba88e6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
33 changes: 31 additions & 2 deletions cmd/edit_common.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cmd

import (
"bufio"
"os"
"strings"

"github.com/zaquestion/lab/internal/git"
Expand Down Expand Up @@ -40,8 +42,9 @@ func getUpdateAssignees(currentAssignees []string, assignees []string, unassigne
return assigneeIDs, assigneesChanged, nil
}

// editGetTitleDescription returns a title and description based on the current
// issue title and description and various flags from the command line
// editGetTitleDescription returns a title and description based on the
// current issue title and description and various flags from the command
// line
func editGetTitleDescription(title string, body string, msgs []string, nFlag int) (string, string, error) {
if len(msgs) > 0 {
title = msgs[0]
Expand All @@ -66,3 +69,29 @@ func editGetTitleDescription(title string, body string, msgs []string, nFlag int
}
return git.Edit("EDIT", text)
}

// editGetTitleDescFromFile returns the new title and description based on
// the content of a file. The first line is considered the title, the
// remaining is the description.
func editGetTitleDescFromFile(filename string) (string, string, error) {
var title, body string

file, err := os.Open(filename)
if err != nil {
return "", "", nil
}
defer file.Close()

fileScan := bufio.NewScanner(file)
fileScan.Split(bufio.ScanLines)

// The first line in the file is the title.
fileScan.Scan()
title = fileScan.Text()

for fileScan.Scan() {
body = body + fileScan.Text() + "\n"
}

return title, body, nil
}
16 changes: 1 addition & 15 deletions cmd/mr_create.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cmd

import (
"bufio"
"bytes"
"fmt"
"log"
Expand Down Expand Up @@ -192,23 +191,10 @@ func runMRCreate(cmd *cobra.Command, args []string) {
log.Fatal("option -F cannot be combined with -m/-c")
}

file, err := os.Open(filename)
title, body, err = editGetTitleDescFromFile(filename)
if err != nil {
log.Fatal(err)
}

fileScan := bufio.NewScanner(file)
fileScan.Split(bufio.ScanLines)

// The first line in the file is the title.
fileScan.Scan()
title = fileScan.Text()

for fileScan.Scan() {
body = body + fileScan.Text() + "\n"
}

file.Close()
} else if len(msgs) > 0 {
if coverLetterFormat {
log.Fatal("option -m cannot be combined with -c/-F")
Expand Down

0 comments on commit eba88e6

Please sign in to comment.