提交 0d383181 编写于 作者: J Jingwen Owen Ou

Move GetTitleAndBodyFromFlags to commands/utils

上级 dea049f2
......@@ -41,7 +41,7 @@ func init() {
$ gh issue
*/
func issue(cmd *Command, args *Args) {
RunInLocalRepo(func(localRepo *github.GitHubRepo, project *github.Project, gh *github.Client) {
runInLocalRepo(func(localRepo *github.GitHubRepo, project *github.Project, gh *github.Client) {
if args.Noop {
fmt.Printf("Would request list of issues for %s\n", project)
} else {
......@@ -63,11 +63,11 @@ func issue(cmd *Command, args *Args) {
}
func createIssue(cmd *Command, args *Args) {
RunInLocalRepo(func(localRepo *github.GitHubRepo, project *github.Project, gh *github.Client) {
runInLocalRepo(func(localRepo *github.GitHubRepo, project *github.Project, gh *github.Client) {
if args.Noop {
fmt.Printf("Would create an issue for %s\n", project)
} else {
title, body, err := github.GetTitleAndBodyFromFlags(flagIssueMessage, flagIssueFile)
title, body, err := getTitleAndBodyFromFlags(flagIssueMessage, flagIssueFile)
utils.Check(err)
if title == "" {
......
......@@ -132,7 +132,7 @@ func pullRequest(cmd *Command, args *Args) {
}
}
title, body, err := github.GetTitleAndBodyFromFlags(flagPullRequestMessage, flagPullRequestFile)
title, body, err := getTitleAndBodyFromFlags(flagPullRequestMessage, flagPullRequestFile)
utils.Check(err)
fullBase := fmt.Sprintf("%s:%s", baseProject.Owner, base)
......
......@@ -54,7 +54,7 @@ func init() {
}
func release(cmd *Command, args *Args) {
RunInLocalRepo(func(localRepo *github.GitHubRepo, project *github.Project, gh *github.Client) {
runInLocalRepo(func(localRepo *github.GitHubRepo, project *github.Project, gh *github.Client) {
if args.Noop {
fmt.Printf("Would request list of releases for %s\n", project)
} else {
......@@ -82,12 +82,12 @@ func createRelease(cmd *Command, args *Args) {
assetsDir, err := getAssetsDirectory(flagReleaseAssetsDir, tag)
utils.Check(err)
RunInLocalRepo(func(localRepo *github.GitHubRepo, project *github.Project, gh *github.Client) {
runInLocalRepo(func(localRepo *github.GitHubRepo, project *github.Project, gh *github.Client) {
currentBranch, err := localRepo.CurrentBranch()
utils.Check(err)
branchName := currentBranch.ShortName()
title, body, err := github.GetTitleAndBodyFromFlags(flagReleaseMessage, flagReleaseFile)
title, body, err := getTitleAndBodyFromFlags(flagReleaseMessage, flagReleaseFile)
utils.Check(err)
if title == "" {
......
......@@ -4,11 +4,25 @@ import (
"github.com/jingweno/gh/github"
"github.com/jingweno/gh/utils"
"github.com/jingweno/go-octokit/octokit"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
type listFlag []string
func (l *listFlag) String() string {
return strings.Join([]string(*l), ",")
}
func (l *listFlag) Set(value string) error {
for _, flag := range strings.Split(value, ",") {
*l = append(*l, flag)
}
return nil
}
func isDir(file string) bool {
f, err := os.Open(file)
if err != nil {
......@@ -54,7 +68,39 @@ func isEmptyDir(path string) bool {
return match == nil
}
func RunInLocalRepo(fn func(localRepo *github.GitHubRepo, project *github.Project, client *github.Client)) {
func getTitleAndBodyFromFlags(messageFlag, fileFlag string) (title, body string, err error) {
if messageFlag != "" {
title, body = readMsg(messageFlag)
} else if fileFlag != "" {
var (
content []byte
err error
)
if fileFlag == "-" {
content, err = ioutil.ReadAll(os.Stdin)
} else {
content, err = ioutil.ReadFile(fileFlag)
}
utils.Check(err)
title, body = readMsg(string(content))
}
return
}
func readMsg(msg string) (title, body string) {
split := strings.SplitN(msg, "\n\n", 2)
title = strings.TrimSpace(split[0])
if len(split) > 1 {
body = strings.TrimSpace(split[1])
}
return
}
func runInLocalRepo(fn func(localRepo *github.GitHubRepo, project *github.Project, client *github.Client)) {
localRepo := github.LocalRepo()
project, err := localRepo.CurrentProject()
utils.Check(err)
......@@ -64,16 +110,3 @@ func RunInLocalRepo(fn func(localRepo *github.GitHubRepo, project *github.Projec
os.Exit(0)
}
type listFlag []string
func (l *listFlag) String() string {
return strings.Join([]string(*l), ",")
}
func (l *listFlag) Set(value string) error {
for _, flag := range strings.Split(value, ",") {
*l = append(*l, flag)
}
return nil
}
......@@ -6,7 +6,6 @@ import (
"fmt"
"github.com/jingweno/gh/cmd"
"github.com/jingweno/gh/git"
"github.com/jingweno/gh/utils"
"io"
"io/ioutil"
"os"
......@@ -15,27 +14,6 @@ import (
"strings"
)
func GetTitleAndBodyFromFlags(messageFlag, fileFlag string) (title, body string, err error) {
if messageFlag != "" {
title, body = readMsg(messageFlag)
} else if fileFlag != "" {
var (
content []byte
err error
)
if fileFlag == "-" {
content, err = ioutil.ReadAll(os.Stdin)
} else {
content, err = ioutil.ReadFile(fileFlag)
}
utils.Check(err)
title, body = readMsg(string(content))
}
return
}
func NewEditor(topic, message string) (editor *Editor, err error) {
messageFile, err := getMessageFile(topic)
if err != nil {
......@@ -155,16 +133,6 @@ func readLine(r *bufio.Reader) (string, error) {
return string(ln), err
}
func readMsg(msg string) (title, body string) {
split := strings.SplitN(msg, "\n\n", 2)
title = strings.TrimSpace(split[0])
if len(split) > 1 {
body = strings.TrimSpace(split[1])
}
return
}
func getMessageFile(about string) (string, error) {
gitDir, err := git.Dir()
if err != nil {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册