Skip to content

Commit

Permalink
Bump go-cli-alias to v0.0.4
Browse files Browse the repository at this point in the history
  • Loading branch information
LinuxSuRen committed Dec 8, 2020
1 parent f50a1f2 commit eeb8217
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 50 deletions.
9 changes: 9 additions & 0 deletions alias.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main

import alias "github.com/linuxsuren/go-cli-alias/pkg"

func getAliasList() []alias.Alias {
return []alias.Alias{
{Name: "cm", Command: "checkout master"},
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.15

require (
github.com/linuxsuren/cobra-extension v0.0.5
github.com/linuxsuren/go-cli-alias v0.0.3
github.com/linuxsuren/go-cli-alias v0.0.4
github.com/magiconair/properties v1.8.4
github.com/spf13/cobra v1.1.1
)
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,15 @@ github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/linuxsuren/cobra-extension v0.0.1 h1:e288zsm1iWUaAOjfiw42hiWJsrrXZnJ0vKApWPyTfkQ=
github.com/linuxsuren/cobra-extension v0.0.1/go.mod h1:UqHi31q7Dj+46nzcVWNZz5Z1JcHBJcmCyPAZRoM8LXo=
github.com/linuxsuren/cobra-extension v0.0.3/go.mod h1:UqHi31q7Dj+46nzcVWNZz5Z1JcHBJcmCyPAZRoM8LXo=
github.com/linuxsuren/cobra-extension v0.0.5 h1:3go926vpXiizRzBEwt8CddCDerRsG5hlIFr0JJvGaKc=
github.com/linuxsuren/cobra-extension v0.0.5/go.mod h1:qcEJv7BbL0UpK6MbrTESP/nKf1+z1wQdMAnE1NBl3QQ=
github.com/linuxsuren/go-cli-alias v0.0.3-0.20201208135826-ad33122f181c h1:YNpS+IdkZA9fvNa9gi7xA45FrVSNsEamrzdyezJndjM=
github.com/linuxsuren/go-cli-alias v0.0.3-0.20201208135826-ad33122f181c/go.mod h1:l2/nzdvung0zorpCxI1CT+Fuuc/W1sFEWS0jt3/b2ZM=
github.com/linuxsuren/go-cli-alias v0.0.3 h1:IbqC+4uOTCRM27WCII7Z35pXP4bwqck0J5jg2xmIqr4=
github.com/linuxsuren/go-cli-alias v0.0.3/go.mod h1:l2/nzdvung0zorpCxI1CT+Fuuc/W1sFEWS0jt3/b2ZM=
github.com/linuxsuren/go-cli-alias v0.0.4 h1:+otarDOeSZzzbTCr9CllAOQCQNpaf/HI41iDvWWUo/w=
github.com/linuxsuren/go-cli-alias v0.0.4/go.mod h1:dfwOx8H0iVpdS9gtLC80GCC4cvDvzWejxjxXn7sFWUs=
github.com/linuxsuren/go-cli-plugin v0.0.1/go.mod h1:uyO09KK8otYfDV5LVTfcWX2UbAi3kEz3PrkdfIxnDlg=
github.com/linuxsuren/http-downloader v0.0.2-0.20201207132639-19888a6beaec h1:N6xw+W4IvgOV0cmhE7iUWKHNZqMUXlT3in+1ORmqIGo=
github.com/linuxsuren/http-downloader v0.0.2-0.20201207132639-19888a6beaec/go.mod h1:zRZY9FCDBuYNDxbI2Ny5suasZsMk7J6q9ecQ3V3PIqI=
Expand Down
68 changes: 19 additions & 49 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package main

import (
"context"
"fmt"
ext "github.com/linuxsuren/cobra-extension"
extver "github.com/linuxsuren/cobra-extension/version"
alias "github.com/linuxsuren/go-cli-alias/pkg"
aliasCmd "github.com/linuxsuren/go-cli-alias/pkg/cmd"
"github.com/spf13/cobra"
"os"
Expand All @@ -13,68 +12,39 @@ import (
"syscall"
)

const (
TargetCLI = "git"
AliasCLI = "cgit"
)

func main() {
cmd := &cobra.Command{
Use: "cgit",
Use: AliasCLI,
RunE: func(cmd *cobra.Command, args []string) (err error) {
env := os.Environ()

preferGitHub(args)
useMirror(args)
preHook(args)

var gitBinary string
if gitBinary, err = exec.LookPath("git"); err == nil {
syscall.Exec(gitBinary, append([]string{"git"}, args...), env)
if gitBinary, err = exec.LookPath(TargetCLI); err == nil {
syscall.Exec(gitBinary, append([]string{TargetCLI}, args...), env)
}
return
},
}

cmd.AddCommand(extver.NewVersionCmd("linuxsuren", "cgit", "cgit", nil))
cmd.AddCommand(extver.NewVersionCmd("linuxsuren", AliasCLI, AliasCLI, nil))

var ctx context.Context
if defMgr, err := alias.GetDefaultAliasMgrWithNameAndInitialData(cmd.Name(), []alias.Alias{
{Name: "cm", Command: "checkout master"},
}); err == nil {
ctx = context.WithValue(context.Background(), alias.AliasKey, defMgr)
aliasCmd.AddAliasCmd(cmd, getAliasList())

cmd.AddCommand(aliasCmd.NewRootCommand(ctx))
} else {
cmd.Println(fmt.Errorf("cannot get default alias manager, error: %v", err))
}
cmd.AddCommand(ext.NewCompletionCmd(cmd))

cmd.SilenceErrors = true
a := cmd.Execute()
if a != nil && strings.Contains(a.Error(), "unknown command") {
args := os.Args[1:]
var ctx context.Context
var defMgr *alias.DefaultAliasManager
var err error
if defMgr, err = alias.GetDefaultAliasMgrWithNameAndInitialData(cmd.Name(), []alias.Alias{
{Name: "cm", Command: "checkout master"},
}); err == nil {
ctx = context.WithValue(context.Background(), alias.AliasKey, defMgr)
var gitBinary string
var targetCmd []string
env := os.Environ()

if gitBinary, err = exec.LookPath("git"); err != nil {
panic("cannot find git")
}

if ok, redirect := aliasCmd.RedirectToAlias(ctx, args); ok {
args = redirect
}

preferGitHub(args)
useMirror(args)
aliasCmd.Execute(cmd, TargetCLI, getAliasList(), preHook)
}

targetCmd = append([]string{"git"}, args...)
_ = syscall.Exec(gitBinary, targetCmd, env) // ignore the errors due to we've no power to deal with it
} else {
err = fmt.Errorf("cannot get default alias manager, error: %v", err)
}
}
func preHook(args []string) {
preferGitHub(args)
useMirror(args)
}

func preferGitHub(args []string) {
Expand All @@ -83,7 +53,7 @@ func preferGitHub(args []string) {
}

address := args[1]
if !strings.HasPrefix(address, "http") {
if !strings.HasPrefix(address, "http") || !strings.HasPrefix(address, "git@") {
args[1] = fmt.Sprintf("https://github.com.cnpmjs.org/%s", address)
}
}
Expand Down

0 comments on commit eeb8217

Please sign in to comment.