git-roast/cmd/request.go

71 lines
1.3 KiB
Go
Raw Permalink Normal View History

2025-01-05 06:30:54 +01:00
package cmd
import (
"errors"
"fmt"
"git-roast/internal/roast"
"os"
"github.com/spf13/cobra"
)
var requestCmd = &cobra.Command{
Use: "request",
Short: "Request a merge from one branch into another.",
Example: "git roast req from feat1 into main",
RunE: func(cmd *cobra.Command, args []string) error {
roast, err := roast.Open()
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
return nil
}
var from string
var into string
switch len(args) {
case 2:
if args[0] == "into" {
if current, ok := roast.CurrentBranchName(); ok {
from = current
}
} else {
from = args[0]
}
into = args[1]
case 3:
if args[1] == "into" {
from = args[0]
into = args[2]
} else if args[1] == "from" {
into = args[0]
from = args[2]
}
case 4:
if args[0] == "from" && args[2] == "into" {
from = args[1]
into = args[3]
} else if args[0] == "into" && args[2] == "from" {
into = args[1]
from = args[3]
}
}
if len(from) == 0 || len(into) == 0 {
return errors.New("invalid `from` and `into` branch names")
}
if err := roast.Request(from, into); err != nil {
_, _ = fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
return nil
}
return nil
},
Aliases: []string{"req"},
}
func init() {
rootCmd.AddCommand(requestCmd)
}