Skip to content

Commit

Permalink
feat: Fully interactive command.
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed Apr 14, 2018
1 parent a332760 commit 11920b3
Show file tree
Hide file tree
Showing 19 changed files with 655 additions and 441 deletions.
46 changes: 46 additions & 0 deletions choose/action.go
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
}
112 changes: 5 additions & 107 deletions choose/choose.go
Original file line number Diff line number Diff line change
@@ -1,100 +1,28 @@
package choose

import (
"fmt"
"math"
"sort"
"strconv"
"strings"

"github.com/google/go-github/github"
"github.com/ldez/prm/config"
"github.com/ldez/prm/types"
"gopkg.in/AlecAivazis/survey.v1"
)

const (
exitLabel = "exit"
allLabel = "all"
allLabel = "all"
// ExitLabel name
ExitLabel = "exit"
// ExitValue representation
ExitValue = 0
// AllValue representation
AllValue = math.MaxInt32
)

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
}

type answersProject struct {
Directory string
}

func (a answersProject) isExit() bool {
return a.Directory == exitLabel
}

// PullRequest Choose a PR in the list
func PullRequest(pulls map[string][]types.PullRequest) (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)
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()
return a.Directory == ExitLabel
}

// Project Choose a project in the list
Expand All @@ -103,7 +31,7 @@ func Project(configs []config.Configuration) (*config.Configuration, error) {
for _, conf := range configs {
surveyOpts = append(surveyOpts, conf.Directory)
}
surveyOpts = append(surveyOpts, "exit")
surveyOpts = append(surveyOpts, ExitLabel)

var qs = []*survey.Question{
{
Expand All @@ -126,33 +54,3 @@ func Project(configs []config.Configuration) (*config.Configuration, error) {
}
return config.Find(configs, answers.Directory)
}

// 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()
}
53 changes: 53 additions & 0 deletions choose/gitremote.go
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
}
111 changes: 111 additions & 0 deletions choose/pullrequest.go
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()
}
Loading

0 comments on commit 11920b3

Please sign in to comment.