2022-09-04 02:06:06 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"flag"
|
2022-12-27 03:14:53 +00:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
2022-12-28 04:40:45 +00:00
|
|
|
|
|
|
|
"github.com/AlecAivazis/survey/v2"
|
|
|
|
"github.com/peterbourgon/ff/v3/ffcli"
|
2022-09-04 02:06:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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)
|
2022-12-27 03:14:53 +00:00
|
|
|
cmd := exec.Command(getIDE(), path)
|
|
|
|
cmd.Dir = path
|
|
|
|
cmd.Stdin = os.Stdin
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
return cmd.Run()
|
2022-09-04 02:06:06 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2022-12-27 03:14:53 +00:00
|
|
|
|
|
|
|
func getIDE() string {
|
|
|
|
for _, ide := range []string{
|
|
|
|
"hx",
|
|
|
|
"nvim",
|
|
|
|
"goland",
|
|
|
|
"vi",
|
|
|
|
} {
|
|
|
|
if i, err := exec.LookPath(ide); err == nil {
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return os.Getenv("EDITOR")
|
|
|
|
}
|