feat: Add --remote (#1387)

Co-authored-by: anqiansong <anqiansong@bytedance.com>
This commit is contained in:
anqiansong
2021-12-29 18:16:42 +08:00
committed by GitHub
parent 23deaf50e6
commit b8ea16a88e
12 changed files with 265 additions and 31 deletions

View File

@@ -17,6 +17,7 @@ import (
const (
NL = "\n"
goctlDir = ".goctl"
gitDir = ".git"
)
var goctlHome string
@@ -82,6 +83,16 @@ func GetGoctlHome() (string, error) {
return filepath.Join(home, goctlDir), nil
}
// GetGitHome returns the git home of goctl.
func GetGitHome() (string, error) {
goctlH, err := GetGoctlHome()
if err != nil {
return "", err
}
return filepath.Join(goctlH, gitDir), nil
}
// GetTemplateDir returns the category path value in GoctlHome where could get it by GetGoctlHome
func GetTemplateDir(category string) (string, error) {
goctlHome, err := GetGoctlHome()

View File

@@ -0,0 +1,23 @@
package util
import (
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
)
func TestGetGitHome(t *testing.T) {
homeDir, err := os.UserHomeDir()
if err != nil {
return
}
actual, err := GetGitHome()
if err != nil {
return
}
expected := filepath.Join(homeDir, goctlDir, gitDir)
assert.Equal(t, expected, actual)
}

36
tools/goctl/util/git.go Normal file
View File

@@ -0,0 +1,36 @@
package util
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"github.com/tal-tech/go-zero/tools/goctl/util/env"
)
func CloneIntoGitHome(url string) (dir string, err error) {
gitHome, err := GetGitHome()
if err != nil {
return "", err
}
os.RemoveAll(gitHome)
ext := filepath.Ext(url)
repo := strings.TrimSuffix(filepath.Base(url), ext)
dir = filepath.Join(gitHome, repo)
path, err := env.LookPath("git")
if err != nil {
return "", err
}
if !env.CanExec() {
return "", fmt.Errorf("os %q can not call 'exec' command", runtime.GOOS)
}
cmd := exec.Command(path, "clone", url, dir)
cmd.Env = os.Environ()
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
return
}