git-ea/cmd/ide.go

64 lines
1.2 KiB
Go

package cmd
import (
"context"
"flag"
"github.com/AlecAivazis/survey/v2"
"github.com/peterbourgon/ff/v3/ffcli"
"os"
"os/exec"
)
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)
cmd := exec.Command(getIDE(), path)
cmd.Dir = path
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
},
}
}
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")
}