зеркало из https://github.com/mislav/hub.git
Fallthrough to git if no commands match
This commit is contained in:
Родитель
24eec6ccfc
Коммит
ff5b5ce6d7
|
@ -18,6 +18,12 @@ func (cmd *Cmd) WithArg(arg string) *Cmd {
|
|||
return cmd
|
||||
}
|
||||
|
||||
func (cmd *Cmd) WithArgs(args ...string) *Cmd {
|
||||
cmd.Args = append(cmd.Args, args...)
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
||||
func (cmd *Cmd) ExecOutput() (string, error) {
|
||||
output, err := exec.Command(cmd.Name, cmd.Args...).CombinedOutput()
|
||||
|
||||
|
|
|
@ -2,10 +2,33 @@ package commands
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/jingweno/gh/cmd"
|
||||
)
|
||||
|
||||
type Args struct {
|
||||
args []string
|
||||
args []string
|
||||
beforeChain []*cmd.Cmd
|
||||
afterChain []*cmd.Cmd
|
||||
}
|
||||
|
||||
func (a *Args) Before(command ...string) {
|
||||
a.beforeChain = append(a.beforeChain, cmd.NewWithArray(command))
|
||||
}
|
||||
|
||||
func (a *Args) After(command ...string) {
|
||||
a.afterChain = append(a.afterChain, cmd.NewWithArray(command))
|
||||
}
|
||||
|
||||
func (a *Args) Commands() []*cmd.Cmd {
|
||||
result := a.beforeChain
|
||||
result = append(result, a.ToCmd())
|
||||
result = append(result, a.afterChain...)
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
func (a *Args) ToCmd() *cmd.Cmd {
|
||||
return cmd.New("git").WithArgs(a.Array()...)
|
||||
}
|
||||
|
||||
func (a *Args) Get(i int) string {
|
||||
|
@ -57,8 +80,12 @@ func (a *Args) Append(args ...string) {
|
|||
a.args = append(a.args, args...)
|
||||
}
|
||||
|
||||
func (a *Args) Prepend(args ...string) {
|
||||
a.args = append(args, a.args...)
|
||||
}
|
||||
|
||||
func NewArgs(args []string) *Args {
|
||||
return &Args{args}
|
||||
return &Args{args, make([]*cmd.Cmd, 0), make([]*cmd.Cmd, 0)}
|
||||
}
|
||||
|
||||
func removeItem(slice []string, index int) (newSlice []string, item string) {
|
||||
|
|
|
@ -3,6 +3,7 @@ package commands
|
|||
import (
|
||||
"github.com/jingweno/gh/github"
|
||||
"github.com/jingweno/gh/utils"
|
||||
"os"
|
||||
)
|
||||
|
||||
var cmdBrowse = &Command{
|
||||
|
@ -39,4 +40,6 @@ func browse(command *Command, args *Args) {
|
|||
url := project.WebURL(flagBrowseRepo, flagBrowseUser, subpage)
|
||||
err := browserCommand(url)
|
||||
utils.Check(err)
|
||||
|
||||
os.Exit(0)
|
||||
}
|
||||
|
|
|
@ -28,9 +28,6 @@ func checkout(command *Command, args *Args) {
|
|||
err = transformCheckoutArgs(args)
|
||||
utils.Fatal(err)
|
||||
}
|
||||
|
||||
err = git.SysExec("checkout", args.Array()...)
|
||||
utils.Check(err)
|
||||
}
|
||||
|
||||
func transformCheckoutArgs(args *Args) error {
|
||||
|
@ -55,12 +52,12 @@ func transformCheckoutArgs(args *Args) error {
|
|||
}
|
||||
|
||||
if remoteExists {
|
||||
err = updateExistingRemote(user, branch)
|
||||
updateExistingRemote(args, user, branch)
|
||||
} else {
|
||||
err = addRmote(user, branch, url, pullRequest.Head.Repo.Private)
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
err = addRmote(args, user, branch, url, pullRequest.Head.Repo.Private)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
var newBranchName string
|
||||
|
@ -103,23 +100,20 @@ func checkIfRemoteExists(remote string) (bool, error) {
|
|||
return false, nil
|
||||
}
|
||||
|
||||
func updateExistingRemote(user, branch string) error {
|
||||
err := git.Spawn("remote", "set-branches", "--add", user, branch)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
func updateExistingRemote(args *Args, user, branch string) {
|
||||
args.Before("git", "remote", "set-branches", "--add", user, branch)
|
||||
remoteURL := fmt.Sprintf("+refs/heads/%s:refs/remotes/%s/%s", branch, user, branch)
|
||||
|
||||
return git.Spawn("fetch", user, remoteURL)
|
||||
args.Before("git", "fetch", user, remoteURL)
|
||||
}
|
||||
|
||||
func addRmote(user, branch, url string, isPrivate bool) error {
|
||||
func addRmote(args *Args, user, branch, url string, isPrivate bool) error {
|
||||
project, err := github.ParseProjectFromURL(url)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
sshURL := project.GitURL("", user, isPrivate)
|
||||
args.Before("git", "remote", "add", "-f", "-t", branch, user, sshURL)
|
||||
|
||||
return git.Spawn("remote", "add", "-f", "-t", branch, user, sshURL)
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ import (
|
|||
"fmt"
|
||||
"github.com/jingweno/gh/github"
|
||||
"github.com/jingweno/gh/utils"
|
||||
"os"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
|
@ -41,6 +42,8 @@ func compare(command *Command, args *Args) {
|
|||
url := project.WebURL("", flagCompareUser, subpage)
|
||||
err := browserCommand(url)
|
||||
utils.Check(err)
|
||||
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
func transformToTripleDots(r string) string {
|
||||
|
|
|
@ -4,6 +4,7 @@ import (
|
|||
"fmt"
|
||||
"github.com/jingweno/gh/github"
|
||||
"github.com/jingweno/gh/utils"
|
||||
"os"
|
||||
)
|
||||
|
||||
var cmdFork = &Command{
|
||||
|
@ -31,4 +32,6 @@ func fork(cmd *Command, args *Args) {
|
|||
if !flagForkNoRemote && newRemote != "" {
|
||||
fmt.Printf("New remote: %s\n", newRemote)
|
||||
}
|
||||
|
||||
os.Exit(0)
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@ package commands
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"github.com/jingweno/gh/utils"
|
||||
"os"
|
||||
"text/template"
|
||||
)
|
||||
|
@ -19,17 +19,18 @@ func init() {
|
|||
|
||||
func runHelp(cmd *Command, args *Args) {
|
||||
if args.IsEmpty() {
|
||||
PrintUsage()
|
||||
return // not os.Exit(2); success
|
||||
printUsage()
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
if args.Size() != 1 {
|
||||
log.Fatal("too many arguments")
|
||||
utils.Check(fmt.Errorf("too many arguments"))
|
||||
}
|
||||
|
||||
for _, cmd := range All() {
|
||||
if cmd.Name() == args.First() {
|
||||
cmd.PrintUsage()
|
||||
return
|
||||
os.Exit(0)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -51,7 +52,7 @@ GitHub Commands:{{range .GitHubCommands}}{{if .Runnable}}{{if .List}}
|
|||
See 'gh help [command]' for more information about a command.
|
||||
`))
|
||||
|
||||
func PrintUsage() {
|
||||
func printUsage() {
|
||||
usageTemplate.Execute(os.Stdout, struct {
|
||||
BranchingCommands []*Command
|
||||
RemoteCommands []*Command
|
||||
|
@ -63,7 +64,7 @@ func PrintUsage() {
|
|||
})
|
||||
}
|
||||
|
||||
func Usage() {
|
||||
PrintUsage()
|
||||
func usage() {
|
||||
printUsage()
|
||||
os.Exit(2)
|
||||
}
|
||||
|
|
|
@ -84,6 +84,8 @@ func pr(cmd *Command, args *Args) {
|
|||
utils.Check(err)
|
||||
|
||||
fmt.Println(pullRequestURL)
|
||||
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
func writePullRequestChanges(repo *github.Repo, messageFile string) error {
|
||||
|
|
|
@ -1,9 +1,7 @@
|
|||
package commands
|
||||
|
||||
import (
|
||||
"github.com/jingweno/gh/git"
|
||||
"github.com/jingweno/gh/github"
|
||||
"github.com/jingweno/gh/utils"
|
||||
)
|
||||
|
||||
var cmdRemote = &Command{
|
||||
|
@ -28,9 +26,6 @@ func remote(command *Command, args *Args) {
|
|||
if args.Size() >= 2 && (args.First() == "add" || args.First() == "set-url") {
|
||||
transformRemoteArgs(args)
|
||||
}
|
||||
|
||||
err := git.SysExec("remote", args.Array()...)
|
||||
utils.Check(err)
|
||||
}
|
||||
|
||||
func transformRemoteArgs(args *Args) {
|
||||
|
|
|
@ -0,0 +1,53 @@
|
|||
package commands
|
||||
|
||||
import (
|
||||
"os"
|
||||
)
|
||||
|
||||
type Runner struct {
|
||||
Args []string
|
||||
}
|
||||
|
||||
func (r *Runner) Execute() error {
|
||||
args := NewArgs(os.Args[1:])
|
||||
if args.Size() < 1 {
|
||||
usage()
|
||||
}
|
||||
|
||||
for _, cmd := range All() {
|
||||
if cmd.Name() == args.First() && cmd.Runnable() {
|
||||
cmdArgs := args.Rest()
|
||||
if !cmd.GitExtension {
|
||||
cmd.Flag.Usage = func() {
|
||||
cmd.PrintUsage()
|
||||
}
|
||||
if err := cmd.Flag.Parse(cmdArgs); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
cmdArgs = cmd.Flag.Args()
|
||||
}
|
||||
|
||||
args = NewArgs(cmdArgs)
|
||||
cmd.Run(cmd, args)
|
||||
args.Prepend(cmd.Name())
|
||||
|
||||
cmds := args.Commands()
|
||||
length := len(cmds)
|
||||
for i, c := range cmds {
|
||||
var err error
|
||||
if i == (length - 1) {
|
||||
err = c.SysExec()
|
||||
} else {
|
||||
err = c.Exec()
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
|
@ -4,6 +4,7 @@ import (
|
|||
"fmt"
|
||||
"github.com/jingweno/gh/git"
|
||||
"github.com/jingweno/gh/utils"
|
||||
"os"
|
||||
)
|
||||
|
||||
const Version = "0.8.0"
|
||||
|
@ -23,4 +24,6 @@ func runVersion(cmd *Command, args *Args) {
|
|||
|
||||
fmt.Println(gitVersion)
|
||||
fmt.Println(ghVersion)
|
||||
|
||||
os.Exit(0)
|
||||
}
|
||||
|
|
39
main.go
39
main.go
|
@ -1,46 +1,13 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/jingweno/gh/commands"
|
||||
"github.com/jingweno/gh/git"
|
||||
"github.com/jingweno/gh/utils"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
args := commands.NewArgs(os.Args[1:])
|
||||
if args.Size() < 1 {
|
||||
commands.Usage()
|
||||
return
|
||||
}
|
||||
|
||||
for _, cmd := range commands.All() {
|
||||
if cmd.Name() == args.First() && cmd.Runnable() {
|
||||
cmdArgs := args.Rest()
|
||||
if !cmd.GitExtension {
|
||||
cmd.Flag.Usage = func() {
|
||||
cmd.PrintUsage()
|
||||
}
|
||||
if err := cmd.Flag.Parse(cmdArgs); err != nil {
|
||||
os.Exit(2)
|
||||
}
|
||||
|
||||
cmdArgs = cmd.Flag.Args()
|
||||
}
|
||||
|
||||
args = commands.NewArgs(cmdArgs)
|
||||
cmd.Run(cmd, args)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if args.Size() > 0 {
|
||||
err := git.SysExec(args.First(), args.Rest()...)
|
||||
utils.Check(err)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Fprintf(os.Stderr, "Unknown command: %s\n", args.First())
|
||||
commands.Usage()
|
||||
runner := commands.Runner{os.Args[1:]}
|
||||
err := runner.Execute()
|
||||
utils.Check(err)
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче