-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
655 additions
and
441 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package choose | ||
|
||
import "gopkg.in/AlecAivazis/survey.v1" | ||
|
||
// Action name | ||
const ( | ||
ActionList = "list" | ||
ActionCheckout = "checkout" | ||
ActionRemove = "remove" | ||
ActionProjects = "projects" | ||
) | ||
|
||
type answersAction struct { | ||
Action string | ||
} | ||
|
||
func (a answersAction) isExit() bool { | ||
return a.Action == ExitLabel | ||
} | ||
|
||
// Action get PRM action | ||
func Action() (string, error) { | ||
surveyOpts := []string{ActionList, ActionCheckout, ActionRemove, ActionProjects, ExitLabel} | ||
|
||
var qs = []*survey.Question{ | ||
{ | ||
Name: "action", | ||
Prompt: &survey.Select{ | ||
Message: "Choose the action", | ||
Options: surveyOpts, | ||
}, | ||
}, | ||
} | ||
|
||
answers := &answersAction{} | ||
err := survey.Ask(qs, answers) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
if answers.isExit() { | ||
return ExitLabel, nil | ||
} | ||
|
||
return answers.Action, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package choose | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/ldez/prm/local" | ||
"gopkg.in/AlecAivazis/survey.v1" | ||
) | ||
|
||
type answersGitRemote struct { | ||
Remote string | ||
} | ||
|
||
func (a answersGitRemote) isExit() bool { | ||
return a.Remote == ExitLabel | ||
} | ||
|
||
func (a answersGitRemote) getName() string { | ||
parts := strings.SplitN(a.Remote, "]:", 2) | ||
return strings.TrimPrefix(parts[0], "[") | ||
} | ||
|
||
// GitRemote Choose the remote related to PRs (main remote) | ||
func GitRemote(remotes []local.Remote) (string, error) { | ||
var surveyOpts []string | ||
for _, remote := range remotes { | ||
surveyOpts = append(surveyOpts, fmt.Sprintf("[%s]: %s", remote.Name, remote.URL)) | ||
} | ||
surveyOpts = append(surveyOpts, ExitLabel) | ||
|
||
var qs = []*survey.Question{ | ||
{ | ||
Name: "remote", | ||
Prompt: &survey.Select{ | ||
Message: "Choose the remote related to Pull Requests (main remote)", | ||
Options: surveyOpts, | ||
}, | ||
}, | ||
} | ||
|
||
answers := &answersGitRemote{} | ||
err := survey.Ask(qs, answers) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
if answers.isExit() { | ||
return ExitLabel, nil | ||
} | ||
|
||
return answers.getName(), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package choose | ||
|
||
import ( | ||
"fmt" | ||
"sort" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/google/go-github/github" | ||
"github.com/ldez/prm/types" | ||
"gopkg.in/AlecAivazis/survey.v1" | ||
) | ||
|
||
type answersPR struct { | ||
PR string | ||
} | ||
|
||
func (a answersPR) isExit() bool { | ||
return a.PR == ExitLabel | ||
} | ||
|
||
func (a answersPR) isAll() bool { | ||
return a.PR == allLabel | ||
} | ||
|
||
func (a answersPR) getPRNumber() (int, error) { | ||
parts := strings.SplitN(a.PR, ":", 2) | ||
|
||
number, err := strconv.Atoi(parts[0]) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
return number, nil | ||
} | ||
|
||
// PullRequest Choose a PR in the list | ||
func PullRequest(pulls map[string][]types.PullRequest, all bool) (int, error) { | ||
if len(pulls) == 0 { | ||
fmt.Println("* 0 PR.") | ||
return ExitValue, nil | ||
} | ||
|
||
var surveyOpts []string | ||
for _, prs := range pulls { | ||
for _, pr := range prs { | ||
surveyOpts = append(surveyOpts, fmt.Sprintf("%d: %s - %s", pr.Number, pr.Owner, pr.BranchName)) | ||
} | ||
} | ||
sort.Strings(surveyOpts) | ||
if all { | ||
surveyOpts = append(surveyOpts, allLabel) | ||
} | ||
surveyOpts = append(surveyOpts, ExitLabel) | ||
|
||
var qs = []*survey.Question{ | ||
{ | ||
Name: "pr", | ||
Prompt: &survey.Select{ | ||
Message: "Choose a PR", | ||
Options: surveyOpts, | ||
}, | ||
}, | ||
} | ||
|
||
answers := &answersPR{} | ||
err := survey.Ask(qs, answers) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
if answers.isExit() { | ||
return ExitValue, nil | ||
} | ||
|
||
if answers.isAll() { | ||
return AllValue, nil | ||
} | ||
|
||
return answers.getPRNumber() | ||
} | ||
|
||
// RemotePulRequest Choose a PR in the list from GitHub | ||
func RemotePulRequest(prs []*github.PullRequest) (int, error) { | ||
surveyOpts := []string{ExitLabel} | ||
for _, pr := range prs { | ||
surveyOpts = append(surveyOpts, fmt.Sprintf("%d: %s - %s", pr.GetNumber(), pr.User.GetLogin(), pr.GetTitle())) | ||
} | ||
|
||
var qs = []*survey.Question{ | ||
{ | ||
Name: "pr", | ||
Prompt: &survey.Select{ | ||
Message: "Choose a PR", | ||
Options: surveyOpts, | ||
}, | ||
}, | ||
} | ||
|
||
answers := &answersPR{} | ||
err := survey.Ask(qs, answers) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
if answers.isExit() { | ||
return ExitValue, nil | ||
} | ||
|
||
return answers.getPRNumber() | ||
} |
Oops, something went wrong.