diff --git a/cmd/mr_note.go b/cmd/mr_note.go index 7a7d9dca..cd5b545a 100644 --- a/cmd/mr_note.go +++ b/cmd/mr_note.go @@ -8,6 +8,7 @@ import ( "strconv" "strings" "text/template" + "io/ioutil" "github.com/rsteube/carapace" "github.com/spf13/cobra" @@ -34,11 +35,26 @@ var mrCreateNoteCmd = &cobra.Command{ log.Fatal(err) } - body, err := mrNoteMsg(msgs) + filename, err := cmd.Flags().GetString("file") if err != nil { - _, f, l, _ := runtime.Caller(0) - log.Fatal(f+":"+strconv.Itoa(l)+" ", err) + log.Fatal(err) + } + + body := "" + if filename != "" { + content, err := ioutil.ReadFile(filename) + if err != nil { + log.Fatal(err) + } + body = string(content) + } else { + body, err = mrNoteMsg(msgs) + if err != nil { + _, f, l, _ := runtime.Caller(0) + log.Fatal(f+":"+strconv.Itoa(l)+" ", err) + } } + if body == "" { log.Fatal("aborting note due to empty note msg") } @@ -53,7 +69,6 @@ var mrCreateNoteCmd = &cobra.Command{ }, } -// func mrNoteMsg(msgs []string) (string, error) { if len(msgs) > 0 { return strings.Join(msgs[0:], "\n\n"), nil @@ -97,6 +112,7 @@ func mrNoteText() (string, error) { func init() { mrCreateNoteCmd.Flags().StringSliceP("message", "m", []string{}, "Use the given ; multiple -m are concatenated as separate paragraphs") + mrCreateNoteCmd.Flags().StringP("file", "F", "", "Use the given file as the message") mrCmd.AddCommand(mrCreateNoteCmd) carapace.Gen(mrCreateNoteCmd).PositionalCompletion( diff --git a/cmd/mr_note_test.go b/cmd/mr_note_test.go index 28962027..96d10d46 100644 --- a/cmd/mr_note_test.go +++ b/cmd/mr_note_test.go @@ -3,6 +3,8 @@ package cmd import ( "os/exec" "testing" + "io/ioutil" + "path/filepath" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -23,6 +25,27 @@ func Test_mrCreateNote(t *testing.T) { require.Contains(t, string(b), "https://gitlab.com/lab-testing/test/merge_requests/1#note_") } +func Test_mrCreateNote_file(t *testing.T) { + repo := copyTestRepo(t) + + err := ioutil.WriteFile(filepath.Join(repo, "hellolab.txt"), []byte("hello\nlab\n"), 0644) + if err != nil { + t.Fatal(err) + } + + cmd := exec.Command(labBinaryPath, "mr", "note", "lab-testing", "1", + "-F", "hellolab.txt") + cmd.Dir = repo + + b, err := cmd.CombinedOutput() + if err != nil { + t.Log(string(b)) + t.Fatal(err) + } + + require.Contains(t, string(b), "https://gitlab.com/lab-testing/test/merge_requests/1#note_") +} + func Test_mrNoteMsg(t *testing.T) { tests := []struct { Name string