1
0
mirror of https://gitea.com/jolheiser/sip synced 2024-11-22 11:41:59 +01:00

Start work on flags

Signed-off-by: jolheiser <john.olheiser@gmail.com>
This commit is contained in:
jolheiser 2020-09-18 14:28:02 -05:00
parent cce2360f0f
commit 67326a7948
No known key found for this signature in database
GPG Key ID: B853ADA5DA7BBF7A
8 changed files with 325 additions and 112 deletions

@ -2,26 +2,49 @@ package cmd
import (
"gitea.com/jolheiser/sip/config"
"gitea.com/jolheiser/sip/prompt"
"github.com/AlecAivazis/survey/v2"
"github.com/urfave/cli/v2"
"go.jolheiser.com/beaver"
)
var (
originFlag = &cli.StringFlag{
Name: "origin",
Usage: "Default origin name",
Value: "origin",
}
upstreamFlag = &cli.StringFlag{
Name: "upstream",
Usage: "Default upstream name",
Value: "upstream",
}
)
var Config = cli.Command{
Name: "config",
Aliases: []string{"cfg"},
Usage: "Modify Sip config",
Action: doConfig,
Flags: []cli.Flag{
originFlag,
upstreamFlag,
},
Subcommands: []*cli.Command{
{
Name: "origin",
Usage: "Specify default origin name",
Name: "origin",
Usage: "Specify default origin name",
Flags: []cli.Flag{
originFlag,
},
Action: doConfigOrigin,
},
{
Name: "upstream",
Usage: "Specify default upstream name",
Name: "upstream",
Usage: "Specify default upstream name",
Flags: []cli.Flag{
upstreamFlag,
},
Action: doConfigUpstream,
},
},
@ -34,17 +57,13 @@ func doConfig(ctx *cli.Context) error {
return doConfigUpstream(ctx)
}
func doConfigOrigin(_ *cli.Context) error {
question := &survey.Input{
Message: "Default origin name",
Default: "origin",
}
var answer string
if err := survey.AskOne(question, &answer); err != nil {
func doConfigOrigin(ctx *cli.Context) error {
origin, err := prompt.Input(ctx, originFlag, ctx.String("origin"), true)
if err != nil {
return err
}
config.Origin = answer
config.Origin = origin
if err := config.Save(); err != nil {
return err
}
@ -53,17 +72,13 @@ func doConfigOrigin(_ *cli.Context) error {
return nil
}
func doConfigUpstream(_ *cli.Context) error {
question := &survey.Input{
Message: "Default upstream name",
Default: "upstream",
}
var answer string
if err := survey.AskOne(question, &answer); err != nil {
func doConfigUpstream(ctx *cli.Context) error {
upstream, err := prompt.Input(ctx, upstreamFlag, ctx.String("upstream"), true)
if err != nil {
return err
}
config.Upstream = answer
config.Upstream = upstream
if err := config.Save(); err != nil {
return err
}

@ -3,6 +3,7 @@ package cmd
import (
"errors"
"fmt"
"gitea.com/jolheiser/sip/prompt"
"os"
"strconv"
"strings"
@ -19,6 +20,11 @@ import (
"go.jolheiser.com/beaver/color"
)
var queryFlag = &cli.StringFlag{
Name: "query",
Usage: "Search query",
}
var Issues = cli.Command{
Name: "issues",
Aliases: []string{"issue"},
@ -32,6 +38,8 @@ var Issues = cli.Command{
Name: "csv",
Usage: "Output results to a CSV file at `PATH`",
},
queryFlag,
indexFlag,
},
}
@ -52,7 +60,7 @@ func issuesSearch(ctx *cli.Context, pulls bool) (*gitea.Issue, error) {
if pulls {
typ = "pulls"
}
issues, err := queryIssues(client, pulls)
issues, err := queryIssues(ctx, client, pulls)
if err != nil {
return nil, err
}
@ -74,6 +82,7 @@ func issuesSearch(ctx *cli.Context, pulls bool) (*gitea.Issue, error) {
return nil, fi.Close()
}
var selection string
issueMap := make(map[string]*gitea.Issue)
for _, issue := range issues {
index := color.New(color.FgCyan).Format("#" + strconv.Itoa(int(issue.Index)))
@ -92,17 +101,24 @@ func issuesSearch(ctx *cli.Context, pulls bool) (*gitea.Issue, error) {
if len(lbls) > 0 {
labels = color.New(color.FgHiBlack).Format("(" + strings.Join(lbls, ", ") + ")")
}
issueMap[fmt.Sprintf("%s %s %s %s", index, state, title, labels)] = issue
t := fmt.Sprintf("%s %s %s %s", index, state, title, labels)
issueMap[t] = issue
if ctx.Int64("index") == issue.Index {
selection = t
break
}
}
list := make([]string, 0)
for key := range issueMap {
list = append(list, key)
}
sel := &survey.Select{Options: list, Message: "Matching " + typ}
var selection string
if err := survey.AskOne(sel, &selection); err != nil {
return nil, err
if selection == "" {
list := make([]string, 0)
for key := range issueMap {
list = append(list, key)
}
sel := &survey.Select{Options: list, Message: "Matching " + typ}
if err := survey.AskOne(sel, &selection); err != nil {
return nil, err
}
}
body, err := markdown.Render(issueMap[selection].Body)
@ -114,19 +130,24 @@ func issuesSearch(ctx *cli.Context, pulls bool) (*gitea.Issue, error) {
return issueMap[selection], nil
}
func queryIssues(client *gitea.Client, pulls bool) ([]*gitea.Issue, error) {
func queryIssues(ctx *cli.Context, client *gitea.Client, pulls bool) ([]*gitea.Issue, error) {
owner, repo, err := askOwnerRepo()
if err != nil {
return nil, err
}
question := &survey.Input{Message: "Search query"}
var answer string
if err := survey.AskOne(question, &answer); err != nil {
idx := ctx.Int64("index")
if idx > 0 {
issue, _, err := client.GetIssue(owner, repo, idx)
return []*gitea.Issue{issue}, err
}
query, err := prompt.Input(ctx, queryFlag, "", false)
if err != nil {
return nil, err
}
filter := sdk.NewIssueFilter(answer)
filter := sdk.NewIssueFilter(query)
opts := gitea.ListIssueOption{KeyWord: filter.Query, State: "all"}
issues, err := sdk.GetIssues(client, owner, repo, opts)
if err != nil {

@ -2,16 +2,31 @@ package cmd
import (
"fmt"
"gitea.com/jolheiser/sip/prompt"
"gitea.com/jolheiser/sip/flag"
"gitea.com/jolheiser/sip/markdown"
"code.gitea.io/sdk/gitea"
"github.com/AlecAivazis/survey/v2"
"github.com/urfave/cli/v2"
"go.jolheiser.com/beaver/color"
)
var (
titleFlag = &cli.StringFlag{
Name: "title",
Usage: "Title",
}
descriptionFlag = &cli.StringFlag{
Name: "description",
Usage: "Description",
}
confirmFlag = &cli.BoolFlag{
Name: "confirm",
Usage: "Confirm",
}
)
var IssuesCreate = cli.Command{
Name: "create",
Aliases: []string{"new"},
@ -19,7 +34,7 @@ var IssuesCreate = cli.Command{
Action: doIssueCreate,
}
func doIssueCreate(_ *cli.Context) error {
func doIssueCreate(ctx *cli.Context) error {
fmt.Println()
url := color.New(color.FgYellow).Format(flag.FullURL())
fmt.Println(color.New(color.FgCyan).Format("Creating a new issue for"), url)
@ -33,27 +48,18 @@ func doIssueCreate(_ *cli.Context) error {
var title, body string
for !confirmed {
questions := []*survey.Question{
{
Name: "title",
Prompt: &survey.Input{Message: "Title", Default: title},
Validate: survey.Required,
},
{
Name: "body",
Prompt: &survey.Multiline{Message: "Description", Default: body},
},
}
answers := struct {
Title string
Body string
}{}
if err := survey.Ask(questions, &answers); err != nil {
t, err := prompt.Input(ctx, titleFlag, title, true)
if err != nil {
return err
}
title = answers.Title
body = answers.Body
d, err := prompt.Multi(ctx, descriptionFlag, body, false)
if err != nil {
return err
}
title = t
body = d
preview, err := markdown.Render(body)
if err != nil {
@ -61,11 +67,12 @@ func doIssueCreate(_ *cli.Context) error {
}
fmt.Printf("%s\n\n%s\n", title, preview)
confirm := &survey.Confirm{Message: "Preview above, enter to create or 'n' to edit", Default: true}
if err := survey.AskOne(confirm, &confirmed); err != nil {
c, err := prompt.Confirm(ctx, confirmFlag, true, true)
if err != nil {
return err
}
confirmed = c
}
issue, _, err := client.CreateIssue(flag.Owner, flag.Repo, gitea.CreateIssueOption{Title: title, Body: body})

@ -19,6 +19,8 @@ var Pulls = cli.Command{
Name: "csv",
Usage: "Output results to a CSV file at `PATH`",
},
queryFlag,
indexFlag,
},
}

@ -3,6 +3,7 @@ package cmd
import (
"errors"
"fmt"
"gitea.com/jolheiser/sip/prompt"
"os"
"os/exec"
"strconv"
@ -17,10 +18,20 @@ import (
"go.jolheiser.com/beaver"
)
var indexFlag = &cli.Int64Flag{
Name: "index",
Usage: "PR Index",
Value: -1,
}
var PullsCheckout = cli.Command{
Name: "checkout",
Usage: "Checkout a pull request for testing",
Action: doPullCheckout,
Flags: []cli.Flag{
indexFlag,
confirmFlag,
},
}
func doPullCheckout(ctx *cli.Context) error {
@ -30,21 +41,12 @@ func doPullCheckout(ctx *cli.Context) error {
}
var issue *gitea.Issue
questions := []*survey.Question{
{
Name: "index",
Prompt: &survey.Input{Message: "Pull request number", Help: "Don't worry if you aren't sure! Just say -1 and we'll search for it inssipd!"},
Validate: validatePRNum,
},
}
prNum := struct {
Index int64
}{}
if err := survey.Ask(questions, &prNum); err != nil {
return err
}
prIndex, err := prompt.Custom(ctx, "index",
&survey.Input{Message: "Pull request number", Help: "Don't worry if you aren't sure! Just say -1 and we'll search for it instead!"},
validatePRNum,
)
if prNum.Index < 0 {
if prIndex.(int64) < 0 {
var confirmed bool
for !confirmed {
iss, err := issuesSearch(ctx, true)
@ -58,7 +60,7 @@ func doPullCheckout(ctx *cli.Context) error {
}
}
} else {
iss, _, err := client.GetIssue(flag.Upstream.Owner, flag.Upstream.Repo, prNum.Index)
iss, _, err := client.GetIssue(flag.Upstream.Owner, flag.Upstream.Repo, prIndex.(int64))
if err != nil {
return err
}

@ -2,6 +2,7 @@ package cmd
import (
"fmt"
"gitea.com/jolheiser/sip/prompt"
"strings"
"gitea.com/jolheiser/sip/flag"
@ -9,19 +10,35 @@ import (
"gitea.com/jolheiser/sip/markdown"
"code.gitea.io/sdk/gitea"
"github.com/AlecAivazis/survey/v2"
"github.com/urfave/cli/v2"
"go.jolheiser.com/beaver/color"
)
var (
baseFlag = &cli.StringFlag{
Name: "base",
Usage: "Base target",
}
headFlag = &cli.StringFlag{
Name: "head",
Usage: "Head target",
}
)
var PullsCreate = cli.Command{
Name: "create",
Aliases: []string{"new"},
Usage: "Create a new pull request",
Action: doPullCreate,
Flags: []cli.Flag{
titleFlag,
descriptionFlag,
baseFlag,
headFlag,
},
}
func doPullCreate(_ *cli.Context) error {
func doPullCreate(ctx *cli.Context) error {
fmt.Println()
url := color.New(color.FgYellow).Format(flag.FullURL())
fmt.Println(color.New(color.FgCyan).Format("Creating a new pull request for"), url)
@ -69,41 +86,30 @@ func doPullCreate(_ *cli.Context) error {
head := defOrigin
for !confirmed {
questions := []*survey.Question{
{
Name: "title",
Prompt: &survey.Input{Message: "Title", Default: title},
Validate: survey.Required,
},
{
Name: "body",
Prompt: &survey.Multiline{Message: "Description", Default: body},
},
{
Name: "base",
Prompt: &survey.Select{Message: "Base target", Options: bases, Default: base},
Validate: survey.Required,
},
{
Name: "head",
Prompt: &survey.Select{Message: "Head target", Options: heads, Default: head},
Validate: survey.Required,
},
}
answers := struct {
Title string
Body string
Base string
Head string
}{}
if err := survey.Ask(questions, &answers); err != nil {
t, err := prompt.Input(ctx, titleFlag, title, true)
if err != nil {
return err
}
title = answers.Title
body = answers.Body
base = answers.Base
head = answers.Head
b, err := prompt.Multi(ctx, descriptionFlag, body, false)
if err != nil {
return err
}
ba, err := prompt.Select(ctx, baseFlag, bases, base, true)
if err != nil {
return err
}
h, err := prompt.Select(ctx, headFlag, heads, head, true)
if err != nil {
return err
}
title = t
body = b
base = ba
head = h
preview, err := markdown.Render(body)
if err != nil {
@ -111,11 +117,11 @@ func doPullCreate(_ *cli.Context) error {
}
fmt.Printf("%s\n\n%s\n", title, preview)
confirm := &survey.Confirm{Message: "Preview above, enter to create or 'n' to edit", Default: true}
if err := survey.AskOne(confirm, &confirmed); err != nil {
c, err := prompt.Confirm(ctx, confirmFlag, true, true)
if err != nil {
return err
}
confirmed = c
}
pull, _, err := client.CreatePullRequest(flag.Owner, flag.Repo, gitea.CreatePullRequestOption{Title: title, Body: body, Base: base, Head: head})

@ -36,12 +36,14 @@ var (
Name: "origin",
Usage: "The origin remote",
Value: config.Origin,
DefaultText: "Configured origin",
Destination: &Origin.Name,
},
&cli.StringFlag{
Name: "upstream",
Usage: "The upstream remote",
Value: config.Upstream,
DefaultText: "Configured upstream",
Destination: &Upstream.Name,
},
&cli.StringFlag{
@ -49,6 +51,7 @@ var (
Aliases: []string{"u"},
Usage: "The base URL to the Gitea instance",
Value: getUpstream().URL,
DefaultText: "Upstream Gitea instance",
Destination: &URL,
},
&cli.StringFlag{
@ -56,6 +59,7 @@ var (
Aliases: []string{"o"},
Usage: "The owner to target",
Value: getUpstream().Owner,
DefaultText: "Upstream owner",
Destination: &Owner,
},
&cli.StringFlag{
@ -63,6 +67,7 @@ var (
Aliases: []string{"r"},
Usage: "The repo to target",
Value: getUpstream().Repo,
DefaultText: "Upstream repository",
Destination: &Repo,
},
&cli.StringFlag{

155
prompt/prompt.go Normal file

@ -0,0 +1,155 @@
package prompt
import (
"github.com/AlecAivazis/survey/v2"
"github.com/urfave/cli/v2"
)
func noValidate(_ interface{}) error {
return nil
}
func Input(ctx *cli.Context, flag *cli.StringFlag, def string, required bool) (string, error) {
for _, c := range ctx.Lineage() {
if c.IsSet(flag.Name) {
return c.String(flag.Name), nil
}
}
validator := noValidate
if required {
validator = survey.Required
}
q := []*survey.Question{
{
Name: "q",
Prompt: &survey.Input{Message: flag.Usage, Default: def},
Validate: validator,
},
}
a := struct {
Q string
}{}
if err := survey.Ask(q, &a); err != nil {
return "", err
}
return a.Q, nil
}
func Multi(ctx *cli.Context, flag *cli.StringFlag, def string, required bool) (string, error) {
for _, c := range ctx.Lineage() {
if c.IsSet(flag.Name) {
return c.String(flag.Name), nil
}
}
validator := noValidate
if required {
validator = survey.Required
}
q := []*survey.Question{
{
Name: "q",
Prompt: &survey.Multiline{Message: flag.Usage, Default: def},
Validate: validator,
},
}
a := struct {
Q string
}{}
if err := survey.Ask(q, &a); err != nil {
return "", err
}
return a.Q, nil
}
func Select(ctx *cli.Context, flag *cli.StringFlag, options []string, def string, required bool) (string, error) {
for _, c := range ctx.Lineage() {
if c.IsSet(flag.Name) {
return c.String(flag.Name), nil
}
}
validator := noValidate
if required {
validator = survey.Required
}
q := []*survey.Question{
{
Name: "q",
Prompt: &survey.Select{Message: flag.Usage, Options: options, Default: def},
Validate: validator,
},
}
a := struct {
Q string
}{}
if err := survey.Ask(q, &a); err != nil {
return "", err
}
return a.Q, nil
}
func Confirm(ctx *cli.Context, flag *cli.BoolFlag, def bool, required bool) (bool, error) {
for _, c := range ctx.Lineage() {
if c.IsSet(flag.Name) {
return c.Bool(flag.Name), nil
}
}
validator := noValidate
if required {
validator = survey.Required
}
q := []*survey.Question{
{
Name: "q",
Prompt: &survey.Confirm{Message: flag.Usage, Default: def},
Validate: validator,
},
}
a := struct {
Q bool
}{}
if err := survey.Ask(q, &a); err != nil {
return false, err
}
return a.Q, nil
}
func Custom(ctx *cli.Context, flag string, prompt survey.Prompt, validator func(val interface{}) error) (interface{}, error) {
for _, c := range ctx.Lineage() {
if c.IsSet(flag) {
return c.Generic(flag), nil
}
}
q := []*survey.Question{
{
Name: "q",
Prompt: prompt,
Validate: validator,
},
}
a := struct {
Q interface{}
}{}
if err := survey.Ask(q, &a); err != nil {
return false, err
}
return a.Q, nil
}