Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: new project by adding branch name #125

Merged
merged 1 commit into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## v1.8.2
- feat: support PostgreSQL
- feat: support config multiple databases
- chore(cli): generate project by adding branch name

## v1.8.1
- fix: GitHub workflow badge URL
Expand Down
82 changes: 61 additions & 21 deletions cmd/eagle/internal/base/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,29 @@ package base

import (
"context"
"fmt"
"net"
"os"
"os/exec"
"path"
"strings"
)

var unExpandVarPath = []string{"~", ".", ".."}

// Repo is git repository manager.
type Repo struct {
url string
home string
url string
branch string
home string
}

// NewRepo new a repository manager.
func NewRepo(url string) *Repo {
var start int
start = strings.Index(url, "//")
if start == -1 {
start = strings.Index(url, ":") + 1
} else {
start += 2
}
end := strings.LastIndex(url, "/")
func NewRepo(url string, branch string) *Repo {
return &Repo{
url: url,
home: eagleHomeWithDir("repo/" + url[start:end]),
url: url,
branch: branch,
home: eagleHomeWithDir("repo/" + repoDir(url)),
}
}

Expand All @@ -36,15 +34,30 @@ func (r *Repo) Path() string {
if end == -1 {
end = len(r.url)
}
return path.Join(r.home, r.url[start+1:end])
var branch string
if r.branch == "" {
branch = "@main"
} else {
branch = "@" + r.branch
}
return path.Join(r.home, r.url[start+1:end]+branch)
}

// Pull fetch the repository from remote url.
func (r *Repo) Pull(ctx context.Context) error {
cmd := exec.Command("git", "pull")
cmd := exec.CommandContext(ctx, "git", "symbolic-ref", "HEAD")
cmd.Dir = r.Path()
cmd.Stderr = os.Stderr
err := cmd.Run()
_, err := cmd.CombinedOutput()
if err != nil {
return err
}
cmd = exec.CommandContext(ctx, "git", "pull")
cmd.Dir = r.Path()
out, err := cmd.CombinedOutput()
fmt.Println(string(out))
if err != nil {
return err
}
return err
}

Expand All @@ -53,10 +66,19 @@ func (r *Repo) Clone(ctx context.Context) error {
if _, err := os.Stat(r.Path()); !os.IsNotExist(err) {
return r.Pull(ctx)
}
cmd := exec.Command("git", "clone", r.url, r.Path())
cmd.Stderr = os.Stderr
err := cmd.Run()
return err
var cmd *exec.Cmd
if r.branch == "" {
cmd = exec.CommandContext(ctx, "git", "clone", r.url, r.Path())
} else {
cmd = exec.CommandContext(ctx, "git", "clone", "-b", r.branch, r.url, r.Path())
}

out, err := cmd.CombinedOutput()
fmt.Println(string(out))
if err != nil {
return err
}
return nil
}

func (r *Repo) CopyTo(ctx context.Context, to string, modPath string, ignores []string) error {
Expand All @@ -69,3 +91,21 @@ func (r *Repo) CopyTo(ctx context.Context, to string, modPath string, ignores []
}
return copyDir(r.Path(), to, []string{mod, modPath}, ignores)
}

func repoDir(url string) string {
vcsURL, err := ParseVCSUrl(url)
if err != nil {
return url
}
// check host contains port
host, _, err := net.SplitHostPort(vcsURL.Host)
if err != nil {
host = vcsURL.Host
}
for _, p := range unExpandVarPath {
host = strings.TrimLeft(host, p)
}
dir := path.Base(path.Dir(vcsURL.Path))
url = fmt.Sprintf("%s/%s", host, dir)
return url
}
2 changes: 1 addition & 1 deletion cmd/eagle/internal/base/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

func TestRepo(t *testing.T) {
r := NewRepo("https://github.com/go-eagle/eagle-layout.git")
r := NewRepo("https://github.com/go-eagle/eagle-layout.git", "main")
if err := r.Clone(context.Background()); err != nil {
t.Fatal(err)
}
Expand Down
10 changes: 6 additions & 4 deletions cmd/eagle/internal/project/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ type Project struct {
Name string
}

// New new a project from remote repo.
func (p *Project) New(ctx context.Context, dir string, layout string) error {
// New create a project from remote repo.
func (p *Project) New(ctx context.Context, dir string, layout string, branch string) error {
to := path.Join(dir, p.Name)
if _, err := os.Stat(to); !os.IsNotExist(err) {
fmt.Printf("🚫 %s already exists\n", p.Name)
Expand All @@ -39,11 +39,13 @@ func (p *Project) New(ctx context.Context, dir string, layout string) error {
}
}

fmt.Printf("🚀 Creating service %s, layout repo is %s, please wait a moment.\n\n", p.Name, layout)
repo := base.NewRepo(layout)
fmt.Printf("🚀 Creating service %s, layout repo is %s, branch is %s, please wait a moment.\n\n", p.Name, layout, branch)
repo := base.NewRepo(layout, branch)
if err := repo.CopyTo(ctx, to, p.Name, []string{".git", ".github"}); err != nil {
return err
}

// rename cmd/server to cmd/{p.Name}
//e := os.Rename(
// path.Join(to, "cmd", "server"),
// path.Join(to, "cmd", p.Name),
Expand Down
8 changes: 5 additions & 3 deletions cmd/eagle/internal/project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,18 @@ var CmdNew = &cobra.Command{

var (
repoURL string
branch string
defaultTimeout string
)

func init() {
if repoURL = os.Getenv("eagle_LAYOUT_REPO"); repoURL == "" {
if repoURL = os.Getenv("EAGLE_LAYOUT_REPO"); repoURL == "" {
repoURL = "https://github.com/go-eagle/eagle-layout.git"
}

defaultTimeout = "60s"
CmdNew.Flags().StringVarP(&repoURL, "-repo-url", "r", repoURL, "layout repo")
CmdNew.Flags().StringVarP(&repoURL, "repo-url", "r", repoURL, "layout repo")
CmdNew.Flags().StringVarP(&branch, "branch", "b", branch, "repo branch name")
CmdNew.Flags().StringVarP(&defaultTimeout, "timeout", "t", defaultTimeout, "request timeout time")
}

Expand Down Expand Up @@ -65,7 +67,7 @@ func run(cmd *cobra.Command, args []string) {
p := &Project{Name: name}
done := make(chan error, 1)
go func() {
done <- p.New(ctx, wd, repoURL)
done <- p.New(ctx, wd, repoURL, branch)
}()

select {
Expand Down
2 changes: 1 addition & 1 deletion cmd/eagle/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (

var (
// Version is the version of the compiled software.
Version = "v0.16.0"
Version = "v0.17.0"

rootCmd = &cobra.Command{
Use: "eagle",
Expand Down
Loading