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

feat: jupiter support create templates from specified git branch #505

Merged
merged 6 commits into from
Oct 24, 2022
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
5 changes: 5 additions & 0 deletions cmd/jupiter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ var Commands = []cli.Command{
Usage: "choose remote template",
Value: "github.com/douyu/jupiter-layout",
},
cli.StringFlag{
Name: "branch",
Usage: "choose branch of remote template",
Value: "main",
},
cli.BoolFlag{
Name: "upgrade",
Usage: "upgrade remote template",
Expand Down
12 changes: 6 additions & 6 deletions pkg/core/cmd/cmd_new.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ func getFileInfosByGit(c *cli.Context, gitPath string) (fileInfos map[string]*fi
_, err := os.Stat(getGlobalLayoutPath(gitPath))
if os.IsNotExist(err) {
// 不存在,拉取对应的仓库
cloneGitRepo(gitPath)
cloneGitRepo(gitPath, c.String("branch"))
} else if err != nil {
// 这里的错误,是说明出现了未知的错误,应该抛出
panic(err)
Expand Down Expand Up @@ -257,7 +257,7 @@ func getFileInfosByGit(c *cli.Context, gitPath string) (fileInfos map[string]*fi
}

// 拉取 jupiter-layout 仓库
func cloneGitRepo(path string) {
func cloneGitRepo(path string, branch string) {
// 存放于git的模板地址
gitPath := "https://" + path + ".git"

Expand All @@ -266,7 +266,7 @@ func cloneGitRepo(path string) {
// clone最新仓库的master分支
// 不存在则拉取模板
var stdErr bytes.Buffer
cmd := exec.Command("git", "clone", gitPath, getGlobalLayoutPath(path), "-b", "main", "--depth=1")
cmd := exec.Command("git", "clone", gitPath, getGlobalLayoutPath(path), "-b", branch, "--depth=1")
cmd.Stderr = &stdErr
if err := cmd.Run(); err != nil {
panic(stdErr.String())
Expand Down Expand Up @@ -296,7 +296,7 @@ func checkUpgrade(c *cli.Context, path string) bool {

color.Green("check upgrade (%s) ...", path)

checkGitCorrectness(path)
checkGitCorrectness(path, c.String("branch"))

// 检查今天是否已经检查过更新
if !checkDays(path) {
Expand All @@ -319,7 +319,7 @@ func checkUpgrade(c *cli.Context, path string) bool {
}

// 检查模板的正确性
func checkGitCorrectness(path string) {
func checkGitCorrectness(path , branch string) {
cmd := exec.Command("git", "status")
cmd.Dir = getGlobalLayoutPath(path)

Expand Down Expand Up @@ -348,7 +348,7 @@ func checkGitCorrectness(path string) {
panic(err)
}

cloneGitRepo(path)
cloneGitRepo(path, branch)

createTempLock(path)
}
Expand Down
120 changes: 120 additions & 0 deletions pkg/core/cmd/cmd_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package cmd

import (
"log"
"sort"
"testing"

"github.com/stretchr/testify/assert"
"github.com/urfave/cli"
)

var Commands = []cli.Command{
{
Name: "init",
Aliases: []string{"i"},
Usage: "init jupiter dependencies",
Action: Init,
},
{
Name: "new",
Aliases: []string{"n"},
Usage: "generate code framework",
Flags: []cli.Flag{
cli.StringFlag{
Name: "remote",
Usage: "choose remote template",
Value: "github.com/douyu/jupiter-layout",
},
cli.StringFlag{
Name: "branch",
Usage: "choose branch of remote template",
Value: "main",
},
cli.BoolFlag{
Name: "upgrade",
Usage: "upgrade remote template",
},
},
Action: New,
},
{
Name: "run",
Aliases: []string{"r"},
Usage: "auto restart program when files changed",
Action: Run,
Flags: []cli.Flag{
cli.StringFlag{
Name: "c",
Value: ".jupiter.toml",
Usage: "指定启动配置文件",
},
cli.BoolFlag{
Name: "debug",
Usage: "debug mode",
},
},
},
{
Name: "update",
Aliases: []string{"upgrade"},
Usage: "Upgrade to the latest version",
Action: Update,
Flags: []cli.Flag{
cli.StringFlag{
Name: "remote",
Usage: "choose remote repo",
Value: "github.com/douyu/jupiter/cmd/jupiter",
},
},
},
{
Name: "clean",
Usage: "clear all cached",
Action: Clean,
Flags: []cli.Flag{
cli.StringFlag{
Name: "remote",
Usage: "choose remote template",
Value: "github.com/douyu/jupiter-layout",
},
},
},
{
Name: "struct2interface",
Aliases: []string{"struct2interface"},
Usage: "Auto generate interface from struct for golang",
Action: Struct2Interface,
Flags: []cli.Flag{
cli.StringFlag{
Name: "d,dir",
Usage: "please specify the code path",
Value: ".",
},
},
},
}

func run(args []string) {
app := cli.NewApp()
app.Usage = "Fast bootstrap tool for jupiter framework"
app.Commands = Commands

sort.Sort(cli.FlagsByName(app.Flags))
sort.Sort(cli.CommandsByName(app.Commands))

err := app.Run(args)
if err != nil {
log.Fatal(err)
}
}

func TestCMD(t *testing.T) {
run([]string{"jupiter", "update"})
run([]string{"jupiter", "init"})
run([]string{"jupiter", "new", "/tmp/test-go", "-remote", "github.com/douyu/jupiter-layout", "-branch", "main"})
run([]string{"jupiter", "struct2interface", "-d", "/tmp/test-go/internal/pkg"})
assert.DirExists(t, "/tmp/github.com_douyu_jupiter_layout/")
run([]string{"jupiter", "clean"})
assert.NoDirExists(t, "/tmp/github.com_douyu_jupiter_layout/")
}
9 changes: 4 additions & 5 deletions pkg/registry/etcdv3/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"github.com/douyu/jupiter/pkg/registry"
"github.com/douyu/jupiter/pkg/server"
"github.com/douyu/jupiter/pkg/xlog"
"github.com/samber/lo"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -179,17 +178,17 @@ func TestKeepalive(t *testing.T) {
}))

lease := reg.getLeaseID()
reg.client.Revoke(reg.ctx, lo.Must(reg.getOrGrantLeaseID(reg.ctx)))
reg.client.Revoke(reg.ctx, lease)

time.Sleep(1 * time.Second)
assert.NotZero(t, lo.Must(reg.getOrGrantLeaseID(reg.ctx)))
assert.True(t, lease != lo.Must(reg.getOrGrantLeaseID(reg.ctx)))
assert.NotZero(t, reg.getLeaseID())
assert.True(t, lease != reg.getLeaseID())

ttl, err := reg.client.TimeToLive(reg.ctx, lease)
assert.Nil(t, err)
assert.Equal(t, int64(-1), ttl.TTL)

ttl, err = reg.client.TimeToLive(reg.ctx, lo.Must(reg.getOrGrantLeaseID(reg.ctx)))
ttl, err = reg.client.TimeToLive(reg.ctx, reg.getLeaseID())
assert.Nil(t, err)
assert.Equal(t, int64(1), ttl.TTL)

Expand Down
23 changes: 0 additions & 23 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ package server

import (
"context"
"errors"
"fmt"
"strings"

"github.com/douyu/jupiter/pkg"
"github.com/douyu/jupiter/pkg/core/constant"
Expand Down Expand Up @@ -62,27 +60,6 @@ func (s *ServiceInfo) ServicePrefix() string {
return fmt.Sprintf("%s:%s:%s:%s/", s.Scheme, s.Name, "v1", pkg.AppMode())
}

func (s *ServiceInfo) Unmarshal(data string) error {
slices := strings.Split(data, ":")
if len(slices) != 4 {
return errors.New("invalid service info1")
}

s.Scheme = slices[0]
s.Name = slices[1]
s.Version = slices[2]

slices = strings.Split(slices[3], "/")
if len(slices) != 2 {
return errors.New("invalid service info2")
}

s.Mode = slices[0]
s.Address = slices[1]

return nil
}

// Service ...
type Service struct {
Namespace string `json:"namespace" toml:"namespace"`
Expand Down
13 changes: 13 additions & 0 deletions pkg/server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,16 @@ func TestNotImplementEqual(t *testing.T) {
address1.Equal(address2)
})
}

func TestServer(t *testing.T) {
server := ApplyOptions(
WithScheme("grpc"),
WithAddress("127.0.0.1"),
WithKind(constant.ServiceGovernor),
WithMetaData("zone", "wh"),
)

assert.Equal(t, "grpc:server.test:v1:unkown-mode/127.0.0.1", server.RegistryName())
assert.Equal(t, "grpc:server.test:v1:unkown-mode/", server.ServicePrefix())
assert.Equal(t, "grpc://127.0.0.1", server.Label())
}