Add ide and pr commands
ci/woodpecker/push/goreleaser Pipeline was successful Details

Signed-off-by: jolheiser <john.olheiser@gmail.com>
main
jolheiser 2022-09-03 21:06:06 -05:00
parent 9c0fec9616
commit b3d15829a2
Signed by: jolheiser
GPG Key ID: B853ADA5DA7BBF7A
6 changed files with 127 additions and 11 deletions

View File

@ -5,6 +5,7 @@ import (
"errors" "errors"
"flag" "flag"
"fmt" "fmt"
"os"
"path/filepath" "path/filepath"
"strings" "strings"
@ -17,6 +18,8 @@ func (h *Handler) Branch() *ffcli.Command {
fs.BoolVar(noFetchFlag, "nf", *noFetchFlag, "--no-fetch") fs.BoolVar(noFetchFlag, "nf", *noFetchFlag, "--no-fetch")
baseFlag := fs.String("base", "main", "Ref to base from") baseFlag := fs.String("base", "main", "Ref to base from")
fs.StringVar(baseFlag, "b", *baseFlag, "--base") fs.StringVar(baseFlag, "b", *baseFlag, "--base")
listFlag := fs.Bool("list", false, "List branches available")
fs.BoolVar(listFlag, "l", *listFlag, "--list")
return &ffcli.Command{ return &ffcli.Command{
Name: "branch", Name: "branch",
FlagSet: fs, FlagSet: fs,
@ -27,13 +30,27 @@ func (h *Handler) Branch() *ffcli.Command {
return err return err
} }
if *listFlag {
dirs, err := os.ReadDir(h.Config.Workspace())
if err != nil {
return err
}
for _, dir := range dirs {
if !dir.IsDir() {
continue
}
fmt.Println(dir.Name())
}
return nil
}
if len(args) < 1 { if len(args) < 1 {
return errors.New("branch requires a name") return errors.New("branch requires a name")
} }
name := args[0] name := args[0]
base := *baseFlag base := *baseFlag
if !strings.HasPrefix(base, "upstream") { if !strings.HasPrefix(base, "upstream") && !strings.HasPrefix(base, "pr-") {
base = fmt.Sprintf("upstream/%s", base) base = fmt.Sprintf("upstream/%s", base)
} }

View File

@ -4,7 +4,6 @@ import (
"context" "context"
"errors" "errors"
"flag" "flag"
"os"
"github.com/AlecAivazis/survey/v2" "github.com/AlecAivazis/survey/v2"
"github.com/peterbourgon/ff/v3/ffcli" "github.com/peterbourgon/ff/v3/ffcli"
@ -31,19 +30,11 @@ func (h *Handler) Cleanup() *ffcli.Command {
return nil return nil
} }
dirs, err := os.ReadDir(h.Config.Workspace()) opts, err := h.Config.Branches()
if err != nil { if err != nil {
return err return err
} }
opts := make([]string, 0, len(dirs))
for _, dir := range dirs {
if !dir.IsDir() {
continue
}
opts = append(opts, dir.Name())
}
if len(opts) == 0 { if len(opts) == 0 {
return errors.New("no worktrees currently exist") return errors.New("no worktrees currently exist")
} }

View File

@ -48,7 +48,9 @@ func New() (*ffcli.Command, error) {
handler.Backport(), handler.Backport(),
handler.Branch(), handler.Branch(),
handler.Frontport(), handler.Frontport(),
handler.IDE(),
handler.Init(), handler.Init(),
handler.PR(),
}, },
Exec: func(_ context.Context, _ []string) error { Exec: func(_ context.Context, _ []string) error {
if *versionFlag { if *versionFlag {

44
cmd/ide.go 100644
View File

@ -0,0 +1,44 @@
package cmd
import (
"context"
"flag"
"os/exec"
"github.com/AlecAivazis/survey/v2"
"github.com/peterbourgon/ff/v3/ffcli"
)
func (h *Handler) IDE() *ffcli.Command {
fs := flag.NewFlagSet("ide", flag.ContinueOnError)
return &ffcli.Command{
Name: "ide",
FlagSet: fs,
ShortUsage: "ide <branch>",
ShortHelp: "ide starts an IDE for `branch`",
Exec: func(ctx context.Context, args []string) error {
if err := h.checkInit(); err != nil {
return err
}
var branch string
if len(args) > 0 {
branch = args[0]
} else {
opts, err := h.Config.Branches()
if err != nil {
return err
}
if err := survey.AskOne(&survey.Select{
Message: "Branch to open",
Options: opts,
}, &branch); err != nil {
return err
}
}
path := h.Config.WorkspaceBranch(branch)
return exec.Command("goland", path).Start()
},
}
}

47
cmd/pr.go 100644
View File

@ -0,0 +1,47 @@
package cmd
import (
"context"
"errors"
"flag"
"fmt"
"github.com/peterbourgon/ff/v3/ffcli"
)
func (h *Handler) PR() *ffcli.Command {
fs := flag.NewFlagSet("pr", flag.ContinueOnError)
ideFlag := fs.Bool("ide", false, "Start an IDE for this PR")
fs.BoolVar(ideFlag, "i", *ideFlag, "--ide")
return &ffcli.Command{
Name: "pr",
FlagSet: fs,
ShortUsage: "pr <index>",
ShortHelp: "pr pulls down a pull request for testing",
Exec: func(ctx context.Context, args []string) error {
if err := h.checkInit(); err != nil {
return err
}
if len(args) == 0 {
return errors.New("pr requires an index")
}
idx := args[0]
branch := fmt.Sprintf("pr-%s", idx)
if err := h.run(ctx, "git", "fetch", "upstream", fmt.Sprintf("pull/%s/head:%s", idx, branch)); err != nil {
return err
}
if err := h.Branch().ParseAndRun(ctx, []string{"--base", branch, branch}); err != nil {
return err
}
if *ideFlag {
return h.IDE().ParseAndRun(ctx, []string{branch})
}
return nil
},
}
}

View File

@ -24,6 +24,21 @@ func (c *Config) IsInit() bool {
return c.Base != "" return c.Base != ""
} }
func (c *Config) Branches() ([]string, error) {
dirs, err := os.ReadDir(c.Workspace())
if err != nil {
return nil, err
}
branches := make([]string, 0, len(dirs))
for _, dir := range dirs {
if !dir.IsDir() {
continue
}
branches = append(branches, dir.Name())
}
return branches, nil
}
func path() (string, error) { func path() (string, error) {
userCfgDir, err := os.UserConfigDir() userCfgDir, err := os.UserConfigDir()
if err != nil { if err != nil {