-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support to clone from a mirror and fork it (#43)
- Loading branch information
1 parent
2268167
commit 5eba315
Showing
10 changed files
with
202 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/AlecAivazis/survey/v2" | ||
"github.com/linuxsuren/cgit/pkg" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
"os/exec" | ||
"path" | ||
"strings" | ||
) | ||
|
||
type cloneOption struct { | ||
ws bool | ||
} | ||
|
||
// NewCloneCommand returns the clone command | ||
func NewCloneCommand() (cmd *cobra.Command) { | ||
opt := &cloneOption{} | ||
|
||
cmd = &cobra.Command{ | ||
Use: "clone", | ||
Short: "A smart way to clone repositories from GitHub", | ||
RunE: opt.runE, | ||
} | ||
|
||
flags := cmd.Flags() | ||
flags.BoolVarP(&opt.ws, "ws", "", false, "Clone the code into ~/ws/github/org/repo if it is true") | ||
return | ||
} | ||
|
||
func (o *cloneOption) runE(_ *cobra.Command, args []string) (err error) { | ||
output := func(arg string) string { | ||
if orgAndRepo := strings.Split(arg, "/"); len(orgAndRepo) == 2 { | ||
return path.Join(viper.GetString("ws"), arg) | ||
} | ||
return "" | ||
} | ||
if !o.ws { | ||
output = nil | ||
} | ||
args = pkg.ParseShortCode(args, output) | ||
|
||
var targetDir string | ||
gitAddress := args[0] | ||
if len(args) >= 2 { | ||
targetDir = args[1] | ||
} | ||
|
||
var gitBinary string | ||
if gitBinary, err = exec.LookPath("git"); err == nil { | ||
gitArgs := []string{"clone"} | ||
gitArgs = append(gitArgs, args...) | ||
pkg.UseMirror(gitArgs) | ||
if err = pkg.ExecCommandInDir(gitBinary, "", gitArgs...); err == nil { | ||
err = pkg.ExecCommandInDir(gitBinary, targetDir, "remote", "set-url", "origin", gitAddress) | ||
} | ||
} | ||
if err != nil { | ||
return | ||
} | ||
|
||
var ghBinary string | ||
if ghBinary, err = exec.LookPath("gh"); err == nil { | ||
prompt := &survey.Confirm{ | ||
Message: "do you want to fork it?", | ||
} | ||
var ok bool | ||
if err = survey.AskOne(prompt, &ok); err == nil && ok { | ||
err = pkg.ExecCommandInDir(ghBinary, targetDir, "repo", "fork", "--remote") | ||
} | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package pkg | ||
|
||
import "strings" | ||
|
||
// UseMirror modify the args with a GitHub mirror | ||
func UseMirror(args []string) { | ||
// only for git clone | ||
if len(args) < 2 || args[0] != "clone" { | ||
return | ||
} | ||
for i, arg := range args { | ||
if strings.Contains(arg, "github.com") && !strings.Contains(arg, "github.com.cnpmjs.org") { | ||
args[i] = strings.ReplaceAll(arg, "github.com", "github.com.cnpmjs.org") | ||
break | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package pkg | ||
|
||
import ( | ||
"github.com/magiconair/properties/assert" | ||
"testing" | ||
) | ||
|
||
func TestUseMirror(t *testing.T) { | ||
type args struct { | ||
args []string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
verify func(*testing.T, []string) | ||
}{{ | ||
name: "normal case", | ||
args: args{ | ||
[]string{"clone", "https://github.com/a/b"}, | ||
}, | ||
verify: func(t *testing.T, args []string) { | ||
assert.Equal(t, args[0], "clone") | ||
assert.Equal(t, args[1], "https://github.com.cnpmjs.org/a/b") | ||
}, | ||
}} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
UseMirror(tt.args.args) | ||
tt.verify(t, tt.args.args) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package pkg | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
// ParseShortCode parses a short org/repo to a GitHub URL | ||
func ParseShortCode(args []string, output func(string) string) []string { | ||
if len(args) <= 0 { | ||
return args | ||
} | ||
if output == nil { | ||
output = defaultOutput | ||
} | ||
|
||
result := []string{fmt.Sprintf("https://github.com/%s", args[0])} | ||
if len(args) > 1 { | ||
result = append(result, args[1:]...) | ||
} else { | ||
if target := output(args[0]); target != "" { | ||
result = append(result, target) | ||
} | ||
} | ||
return result | ||
} | ||
|
||
func defaultOutput(arg string) string { | ||
if orgAndRepo := strings.Split(arg, "/"); len(orgAndRepo) == 2 { | ||
return orgAndRepo[1] | ||
} | ||
return "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package pkg | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestParseShortCode(t *testing.T) { | ||
type args struct { | ||
args []string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want []string | ||
}{{ | ||
name: "org/repo format", | ||
args: args{[]string{"org/repo"}}, | ||
want: []string{"https://github.com/org/repo", "repo"}, | ||
}, { | ||
name: "'org/repo output' format", | ||
args: args{[]string{"org/repo", "dir"}}, | ||
want: []string{"https://github.com/org/repo", "dir"}, | ||
}, { | ||
name: "unexpected format", | ||
args: args{[]string{}}, | ||
want: []string{}, | ||
}} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := ParseShortCode(tt.args.args, nil); !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("ParseShortCode() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |