github.com/trevoraustin/hub@v2.2.0-preview1.0.20141105230840-96d8bfc654cc+incompatible/commands/utils.go (about) 1 package commands 2 3 import ( 4 "io/ioutil" 5 "os" 6 "path/filepath" 7 "strings" 8 9 "github.com/github/hub/github" 10 "github.com/github/hub/utils" 11 "github.com/octokit/go-octokit/octokit" 12 ) 13 14 type listFlag []string 15 16 func (l *listFlag) String() string { 17 return strings.Join([]string(*l), ",") 18 } 19 20 func (l *listFlag) Set(value string) error { 21 for _, flag := range strings.Split(value, ",") { 22 *l = append(*l, flag) 23 } 24 return nil 25 } 26 27 func isDir(file string) bool { 28 f, err := os.Open(file) 29 if err != nil { 30 return false 31 } 32 defer f.Close() 33 34 fi, err := f.Stat() 35 if err != nil { 36 return false 37 } 38 39 return fi.IsDir() 40 } 41 42 func parseUserBranchFromPR(pullRequest *octokit.PullRequest) (user string, branch string) { 43 userBranch := strings.SplitN(pullRequest.Head.Label, ":", 2) 44 user = userBranch[0] 45 if len(userBranch) > 1 { 46 branch = userBranch[1] 47 } else { 48 branch = pullRequest.Head.Ref 49 } 50 51 return 52 } 53 54 func gitRemoteForProject(project *github.Project) (foundRemote *github.Remote) { 55 remotes, err := github.Remotes() 56 utils.Check(err) 57 for _, remote := range remotes { 58 remoteProject, pErr := remote.Project() 59 if pErr == nil && remoteProject.SameAs(project) { 60 foundRemote = &remote 61 return 62 } 63 } 64 65 return nil 66 } 67 68 func isEmptyDir(path string) bool { 69 fullPath := filepath.Join(path, "*") 70 match, _ := filepath.Glob(fullPath) 71 return match == nil 72 } 73 74 func getTitleAndBodyFromFlags(messageFlag, fileFlag string) (title, body string, err error) { 75 if messageFlag != "" { 76 title, body = readMsg(messageFlag) 77 } else if fileFlag != "" { 78 var ( 79 content []byte 80 err error 81 ) 82 83 if fileFlag == "-" { 84 content, err = ioutil.ReadAll(os.Stdin) 85 } else { 86 content, err = ioutil.ReadFile(fileFlag) 87 } 88 utils.Check(err) 89 90 title, body = readMsg(string(content)) 91 } 92 93 return 94 } 95 96 func readMsg(msg string) (title, body string) { 97 split := strings.SplitN(msg, "\n\n", 2) 98 title = strings.TrimSpace(split[0]) 99 if len(split) > 1 { 100 body = strings.TrimSpace(split[1]) 101 } 102 103 return 104 } 105 106 func runInLocalRepo(fn func(localRepo *github.GitHubRepo, project *github.Project, client *github.Client)) { 107 localRepo, err := github.LocalRepo() 108 utils.Check(err) 109 110 project, err := localRepo.CurrentProject() 111 utils.Check(err) 112 113 client := github.NewClient(project.Host) 114 fn(localRepo, project, client) 115 116 os.Exit(0) 117 }