github.com/ianfoo/lab@v0.9.5-0.20180123060006-5ed79f2ccfc7/cmd/issueCreate.go (about)

     1  package cmd
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"log"
     7  	"runtime"
     8  	"strconv"
     9  	"strings"
    10  	"text/template"
    11  
    12  	"github.com/spf13/cobra"
    13  	"github.com/xanzy/go-gitlab"
    14  	"github.com/zaquestion/lab/internal/git"
    15  	lab "github.com/zaquestion/lab/internal/gitlab"
    16  )
    17  
    18  var issueCreateCmd = &cobra.Command{
    19  	Use:   "create [remote]",
    20  	Short: "Open an issue on GitLab",
    21  	Long:  ``,
    22  	Args:  cobra.MaximumNArgs(1),
    23  	Run: func(cmd *cobra.Command, args []string) {
    24  		msgs, err := cmd.Flags().GetStringSlice("message")
    25  		if err != nil {
    26  			log.Fatal(err)
    27  		}
    28  		remote := forkedFromRemote
    29  		if len(args) > 0 {
    30  			ok, err := git.IsRemote(args[0])
    31  			if err != nil {
    32  				log.Fatal(err)
    33  			}
    34  			if ok {
    35  				remote = args[0]
    36  			}
    37  		}
    38  		rn, err := git.PathWithNameSpace(remote)
    39  		if err != nil {
    40  			log.Fatal(err)
    41  		}
    42  
    43  		title, body, err := issueMsg(msgs)
    44  		if err != nil {
    45  			_, f, l, _ := runtime.Caller(0)
    46  			log.Fatal(f+":"+strconv.Itoa(l)+" ", err)
    47  		}
    48  		if title == "" {
    49  			log.Fatal("aborting issue due to empty issue msg")
    50  		}
    51  
    52  		issueURL, err := lab.IssueCreate(rn, &gitlab.CreateIssueOptions{
    53  			Title:       &title,
    54  			Description: &body,
    55  		})
    56  		if err != nil {
    57  			log.Fatal(err)
    58  		}
    59  		fmt.Println(issueURL)
    60  	},
    61  }
    62  
    63  func issueMsg(msgs []string) (string, string, error) {
    64  	if len(msgs) > 0 {
    65  		return msgs[0], strings.Join(msgs[1:], "\n\n"), nil
    66  	}
    67  
    68  	text, err := issueText()
    69  	if err != nil {
    70  		return "", "", err
    71  	}
    72  	return git.Edit("ISSUE", text)
    73  }
    74  
    75  func issueText() (string, error) {
    76  	const tmpl = `{{.InitMsg}}
    77  {{.CommentChar}} Write a message for this issue. The first block
    78  {{.CommentChar}} of text is the title and the rest is the description.`
    79  
    80  	issueTmpl := lab.LoadGitLabTmpl(lab.TmplIssue)
    81  
    82  	initMsg := "\n"
    83  	if issueTmpl != "" {
    84  		initMsg = "\n\n" + issueTmpl
    85  	}
    86  
    87  	commentChar := git.CommentChar()
    88  
    89  	t, err := template.New("tmpl").Parse(tmpl)
    90  	if err != nil {
    91  		return "", err
    92  	}
    93  
    94  	msg := &struct {
    95  		InitMsg     string
    96  		CommentChar string
    97  	}{
    98  		InitMsg:     initMsg,
    99  		CommentChar: commentChar,
   100  	}
   101  
   102  	var b bytes.Buffer
   103  	err = t.Execute(&b, msg)
   104  	if err != nil {
   105  		return "", err
   106  	}
   107  
   108  	return b.String(), nil
   109  }
   110  
   111  func init() {
   112  	issueCreateCmd.Flags().StringSliceP("message", "m", []string{}, "Use the given <msg>; multiple -m are concatenated as seperate paragraphs")
   113  	issueCmd.AddCommand(issueCreateCmd)
   114  }