Compare commits
20 Commits
main
...
fix/apollo
Author | SHA1 | Date |
---|---|---|
baozhecheng | c41d158696 | 2 years ago |
baozhecheng | ce8ed7e4c3 | 2 years ago |
baozhecheng | 46eea6f3a1 | 2 years ago |
baozhecheng | 58e9d41300 | 2 years ago |
baozhecheng | f0f494682b | 2 years ago |
baozhecheng | 2affd568b1 | 2 years ago |
baozhecheng | a25bf5d00f | 2 years ago |
baozhecheng | 0ac2cea598 | 2 years ago |
baozhecheng | 1e0a3c04be | 2 years ago |
baozhecheng | 4e99331879 | 2 years ago |
baozhecheng | fec026af5f | 2 years ago |
baozhecheng | b3994334ed | 2 years ago |
baozhecheng | 8fd97b6258 | 2 years ago |
baozhecheng | 7342800caa | 2 years ago |
baozhecheng | dad8275bd5 | 2 years ago |
baozhecheng | 2eea21cc23 | 2 years ago |
baozhecheng | 2e38a736da | 2 years ago |
baozhecheng | 0017f5aa53 | 2 years ago |
baozhecheng | 102092009e | 2 years ago |
baozhecheng | c423dc3494 | 2 years ago |
@ -1,12 +0,0 @@ |
||||
daysUntilStale: 30 |
||||
daysUntilClose: 3 |
||||
exemptLabels: |
||||
- pinned |
||||
- security |
||||
- bug |
||||
staleLabel: wontfix |
||||
markComment: > |
||||
This issue has been automatically marked as stale because it has not had |
||||
recent activity. It will be closed if no further activity occurs. Thank you |
||||
for your contributions. |
||||
closeComment: true |
@ -1,58 +0,0 @@ |
||||
package base |
||||
|
||||
import ( |
||||
"errors" |
||||
"net/url" |
||||
"regexp" |
||||
"strings" |
||||
) |
||||
|
||||
var ( |
||||
scpSyntaxRe = regexp.MustCompile(`^(\w+)@([\w.-]+):(.*)$`) |
||||
scheme = []string{"git", "https", "http", "git+ssh", "ssh", "file", "ftp", "ftps"} |
||||
) |
||||
|
||||
// ParseVCSUrl ref https://github.com/golang/go/blob/master/src/cmd/go/internal/vcs/vcs.go
|
||||
// see https://go-review.googlesource.com/c/go/+/12226/
|
||||
// git url define https://git-scm.com/docs/git-clone#_git_urls
|
||||
func ParseVCSUrl(repo string) (*url.URL, error) { |
||||
var ( |
||||
repoURL *url.URL |
||||
err error |
||||
) |
||||
|
||||
if m := scpSyntaxRe.FindStringSubmatch(repo); m != nil { |
||||
// Match SCP-like syntax and convert it to a URL.
|
||||
// Eg, "git@github.com:user/repo" becomes
|
||||
// "ssh://git@github.com/user/repo".
|
||||
repoURL = &url.URL{ |
||||
Scheme: "ssh", |
||||
User: url.User(m[1]), |
||||
Host: m[2], |
||||
Path: m[3], |
||||
} |
||||
} else { |
||||
if !strings.Contains(repo, "//") { |
||||
repo = "//" + repo |
||||
} |
||||
if strings.HasPrefix(repo, "//git@") { |
||||
repo = "ssh:" + repo |
||||
} else if strings.HasPrefix(repo, "//") { |
||||
repo = "https:" + repo |
||||
} |
||||
repoURL, err = url.Parse(repo) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
} |
||||
|
||||
// Iterate over insecure schemes too, because this function simply
|
||||
// reports the state of the repo. If we can't see insecure schemes then
|
||||
// we can't report the actual repo URL.
|
||||
for _, s := range scheme { |
||||
if repoURL.Scheme == s { |
||||
return repoURL, nil |
||||
} |
||||
} |
||||
return nil, errors.New("unable to parse repo url") |
||||
} |
@ -1,55 +0,0 @@ |
||||
package base |
||||
|
||||
import ( |
||||
"net" |
||||
"strings" |
||||
"testing" |
||||
) |
||||
|
||||
func TestParseVCSUrl(t *testing.T) { |
||||
repos := []string{ |
||||
// ssh://[user@]host.xz[:port]/path/to/repo.git/
|
||||
"ssh://git@github.com:7875/go-kratos/kratos.git", |
||||
// git://host.xz[:port]/path/to/repo.git/
|
||||
"git://github.com:7875/go-kratos/kratos.git", |
||||
// http[s]://host.xz[:port]/path/to/repo.git/
|
||||
"https://github.com:7875/go-kratos/kratos.git", |
||||
// ftp[s]://host.xz[:port]/path/to/repo.git/
|
||||
"ftps://github.com:7875/go-kratos/kratos.git", |
||||
//[user@]host.xz:path/to/repo.git/
|
||||
"git@github.com:go-kratos/kratos.git", |
||||
// ssh://[user@]host.xz[:port]/~[user]/path/to/repo.git/
|
||||
"ssh://git@github.com:7875/go-kratos/kratos.git", |
||||
// git://host.xz[:port]/~[user]/path/to/repo.git/
|
||||
"git://github.com:7875/go-kratos/kratos.git", |
||||
//[user@]host.xz:/~[user]/path/to/repo.git/
|
||||
"git@github.com:go-kratos/kratos.git", |
||||
///path/to/repo.git/
|
||||
"~/go-kratos/kratos.git", |
||||
// file:///path/to/repo.git/
|
||||
"file://~/go-kratos/kratos.git", |
||||
} |
||||
for _, repo := range repos { |
||||
url, err := ParseVCSUrl(repo) |
||||
if err != nil { |
||||
t.Fatal(repo, err) |
||||
} |
||||
urlPath := strings.TrimLeft(url.Path, "/") |
||||
if urlPath != "go-kratos/kratos.git" { |
||||
t.Fatal(repo, "parse url failed", urlPath) |
||||
} |
||||
} |
||||
} |
||||
|
||||
func TestParseSsh(t *testing.T) { |
||||
repo := "ssh://git@github.com:7875/go-kratos/kratos.git" |
||||
url, err := ParseVCSUrl(repo) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
host, _, err := net.SplitHostPort(url.Host) |
||||
if err != nil { |
||||
host = url.Host |
||||
} |
||||
t.Log(host, url.Path) |
||||
} |
@ -1,25 +0,0 @@ |
||||
package change |
||||
|
||||
import "testing" |
||||
|
||||
func TestParseGithubURL(t *testing.T) { |
||||
urls := []struct { |
||||
url string |
||||
owner string |
||||
repo string |
||||
}{ |
||||
{"https://github.com/go-kratos/kratos.git", "go-kratos", "kratos"}, |
||||
{"https://github.com/go-kratos/kratos", "go-kratos", "kratos"}, |
||||
{"git@github.com:go-kratos/kratos.git", "go-kratos", "kratos"}, |
||||
{"https://github.com/go-kratos/go-kratos.dev.git", "go-kratos", "go-kratos.dev"}, |
||||
} |
||||
for _, url := range urls { |
||||
owner, repo := ParseGithubURL(url.url) |
||||
if owner != url.owner { |
||||
t.Fatalf("owner want: %s, got: %s", owner, url.owner) |
||||
} |
||||
if repo != url.repo { |
||||
t.Fatalf("repo want: %s, got: %s", repo, url.repo) |
||||
} |
||||
} |
||||
} |
@ -1,29 +0,0 @@ |
||||
//go:build linux
|
||||
// +build linux
|
||||
|
||||
package project |
||||
|
||||
import ( |
||||
"testing" |
||||
) |
||||
|
||||
func Test_processProjectParams(t *testing.T) { |
||||
type args struct { |
||||
projectName string |
||||
fallbackPlaceDir string |
||||
} |
||||
tests := []struct { |
||||
name string |
||||
args args |
||||
want string |
||||
}{ |
||||
{"absLinux", args{projectName: "/home/kratos/awesome/go/demo", fallbackPlaceDir: ""}, "/home/kratos/awesome/go"}, |
||||
} |
||||
for _, tt := range tests { |
||||
t.Run(tt.name, func(t *testing.T) { |
||||
if _, got := processProjectParams(tt.args.projectName, tt.args.fallbackPlaceDir); got != tt.want { |
||||
t.Errorf("processProjectParams() = %v, want %v", got, tt.want) |
||||
} |
||||
}) |
||||
} |
||||
} |
@ -1,144 +0,0 @@ |
||||
package project |
||||
|
||||
import ( |
||||
"fmt" |
||||
"go/parser" |
||||
"go/token" |
||||
"os" |
||||
"path/filepath" |
||||
"testing" |
||||
|
||||
"github.com/go-kratos/kratos/cmd/kratos/v2/internal/base" |
||||
) |
||||
|
||||
// TestCmdNew tests the `kratos new` command.
|
||||
func TestCmdNew(t *testing.T) { |
||||
cwd := changeCurrentDir(t) |
||||
projectName := "helloworld" |
||||
|
||||
// create a new project
|
||||
CmdNew.SetArgs([]string{projectName}) |
||||
if err := CmdNew.Execute(); err != nil { |
||||
t.Fatalf("executing command: %v", err) |
||||
} |
||||
|
||||
// check that the expected files were created
|
||||
for _, file := range []string{ |
||||
"go.mod", |
||||
"go.sum", |
||||
"README.md", |
||||
"cmd/helloworld/main.go", |
||||
} { |
||||
if _, err := os.Stat(filepath.Join(cwd, projectName, file)); err != nil { |
||||
t.Errorf("expected file %s to exist", file) |
||||
} |
||||
} |
||||
|
||||
// check that the go.mod file contains the expected module name
|
||||
assertGoMod(t, filepath.Join(cwd, projectName, "go.mod"), projectName) |
||||
|
||||
assertImportsInclude(t, filepath.Join(cwd, projectName, "cmd", projectName, "wire.go"), fmt.Sprintf(`"%s/internal/biz"`, projectName)) |
||||
} |
||||
|
||||
// TestCmdNewNoMod tests the `kratos new` command with the --nomod flag.
|
||||
func TestCmdNewNoMod(t *testing.T) { |
||||
cwd := changeCurrentDir(t) |
||||
|
||||
// create a new project
|
||||
CmdNew.SetArgs([]string{"project"}) |
||||
if err := CmdNew.Execute(); err != nil { |
||||
t.Fatalf("executing command: %v", err) |
||||
} |
||||
|
||||
// add new app with --nomod flag
|
||||
CmdNew.SetArgs([]string{"--nomod", "project/app/user"}) |
||||
if err := CmdNew.Execute(); err != nil { |
||||
t.Fatalf("executing command: %v", err) |
||||
} |
||||
|
||||
// check that the expected files were created
|
||||
for _, file := range []string{ |
||||
"go.mod", |
||||
"go.sum", |
||||
"README.md", |
||||
"cmd/project/main.go", |
||||
"app/user/cmd/user/main.go", |
||||
} { |
||||
if _, err := os.Stat(filepath.Join(cwd, "project", file)); err != nil { |
||||
t.Errorf("expected file %s to exist", file) |
||||
} |
||||
} |
||||
|
||||
assertImportsInclude(t, filepath.Join(cwd, "project/app/user/cmd/user/wire.go"), `"project/app/user/internal/biz"`) |
||||
} |
||||
|
||||
// assertImportsInclude checks that the file at path contains the expected import.
|
||||
func assertImportsInclude(t *testing.T, path, expected string) { |
||||
t.Helper() |
||||
|
||||
got, err := imports(path) |
||||
if err != nil { |
||||
t.Fatalf("getting imports: %v", err) |
||||
} |
||||
|
||||
for _, imp := range got { |
||||
if imp == expected { |
||||
return |
||||
} |
||||
} |
||||
|
||||
t.Errorf("expected imports to include %s, got %v", expected, got) |
||||
} |
||||
|
||||
// imports returns the imports in the file at path.
|
||||
func imports(path string) ([]string, error) { |
||||
fset := token.NewFileSet() |
||||
f, err := parser.ParseFile(fset, path, nil, parser.ImportsOnly) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
|
||||
imports := make([]string, 0, len(f.Imports)) |
||||
for _, s := range f.Imports { |
||||
imports = append(imports, s.Path.Value) |
||||
} |
||||
|
||||
return imports, nil |
||||
} |
||||
|
||||
// assertGoMod checks that the go.mod file contains the expected module name.
|
||||
func assertGoMod(t *testing.T, path, expected string) { |
||||
t.Helper() |
||||
|
||||
got, err := base.ModulePath(path) |
||||
if err != nil { |
||||
t.Fatalf("getting module path: %v", err) |
||||
} |
||||
|
||||
if got != expected { |
||||
t.Errorf("expected module name %s, got %s", expected, got) |
||||
} |
||||
} |
||||
|
||||
// change the working directory to the tempdir
|
||||
func changeCurrentDir(t *testing.T) string { |
||||
t.Helper() |
||||
|
||||
tmp := t.TempDir() |
||||
|
||||
oldCWD, err := os.Getwd() |
||||
if err != nil { |
||||
t.Fatalf("getting working directory: %v", err) |
||||
} |
||||
|
||||
if err := os.Chdir(tmp); err != nil { |
||||
t.Fatalf("changing working directory: %v", err) |
||||
} |
||||
t.Cleanup(func() { |
||||
if err := os.Chdir(oldCWD); err != nil { |
||||
t.Fatalf("restoring working directory: %v", err) |
||||
} |
||||
}) |
||||
|
||||
return tmp |
||||
} |
@ -1,30 +0,0 @@ |
||||
//go:build windows
|
||||
// +build windows
|
||||
|
||||
package project |
||||
|
||||
import ( |
||||
"testing" |
||||
) |
||||
|
||||
func Test_processProjectParams(t *testing.T) { |
||||
type args struct { |
||||
projectName string |
||||
fallbackPlaceDir string |
||||
} |
||||
tests := []struct { |
||||
name string |
||||
args args |
||||
want string |
||||
}{ |
||||
{"absWindows", args{projectName: "c:\\kratos\\awesome\\go\\demo", fallbackPlaceDir: ""}, "c:\\kratos\\awesome\\go"}, |
||||
//{"relativeWindows", args{projectName: "/home/kratos/awesome/go/demo", fallbackPlaceDir: ""}, "/home/kratos/awesome/go"},
|
||||
} |
||||
for _, tt := range tests { |
||||
t.Run(tt.name, func(t *testing.T) { |
||||
if _, got := processProjectParams(tt.args.projectName, tt.args.fallbackPlaceDir); got != tt.want { |
||||
t.Errorf("getProjectPlaceDir() = %v, want %v", got, tt.want) |
||||
} |
||||
}) |
||||
} |
||||
} |
@ -1,4 +1,4 @@ |
||||
package main |
||||
|
||||
// release is the current kratos tool version.
|
||||
const release = "v2.6.3" |
||||
const release = "v2.4.1" |
||||
|
@ -1,17 +0,0 @@ |
||||
{{ range .Errors }} |
||||
|
||||
{{ if .HasComment }}{{ .Comment }}{{ end -}} |
||||
func Is{{.CamelValue}}(err error) bool { |
||||
if err == nil { |
||||
return false |
||||
} |
||||
e := errors.FromError(err) |
||||
return e.Reason == {{ .Name }}_{{ .Value }}.String() && e.Code == {{ .HTTPCode }} |
||||
} |
||||
|
||||
{{ if .HasComment }}{{ .Comment }}{{ end -}} |
||||
func Error{{ .CamelValue }}(format string, args ...interface{}) *errors.Error { |
||||
return errors.New({{ .HTTPCode }}, {{ .Name }}_{{ .Value }}.String(), fmt.Sprintf(format, args...)) |
||||
} |
||||
|
||||
{{- end }} |
@ -1,4 +1,4 @@ |
||||
package main |
||||
|
||||
// release is the current protoc-gen-go-errors version.
|
||||
const release = "v2.6.3" |
||||
const release = "v2.4.1" |
||||
|
@ -1,93 +0,0 @@ |
||||
{{$svrType := .ServiceType}} |
||||
{{$svrName := .ServiceName}} |
||||
|
||||
{{- range .MethodSets}} |
||||
const Operation{{$svrType}}{{.OriginalName}} = "/{{$svrName}}/{{.OriginalName}}" |
||||
{{- end}} |
||||
|
||||
type {{.ServiceType}}HTTPServer interface { |
||||
{{- range .MethodSets}} |
||||
{{- if ne .Comment ""}} |
||||
{{.Comment}} |
||||
{{- end}} |
||||
{{.Name}}(context.Context, *{{.Request}}) (*{{.Reply}}, error) |
||||
{{- end}} |
||||
} |
||||
|
||||
func Register{{.ServiceType}}HTTPServer(s *http.Server, srv {{.ServiceType}}HTTPServer) { |
||||
r := s.Route("/") |
||||
{{- range .Methods}} |
||||
r.{{.Method}}("{{.Path}}", _{{$svrType}}_{{.Name}}{{.Num}}_HTTP_Handler(srv)) |
||||
{{- end}} |
||||
} |
||||
|
||||
{{range .Methods}} |
||||
func _{{$svrType}}_{{.Name}}{{.Num}}_HTTP_Handler(srv {{$svrType}}HTTPServer) func(ctx http.Context) error { |
||||
return func(ctx http.Context) error { |
||||
var in {{.Request}} |
||||
{{- if .HasBody}} |
||||
if err := ctx.Bind(&in{{.Body}}); err != nil { |
||||
return err |
||||
} |
||||
|
||||
{{- if not (eq .Body "")}} |
||||
if err := ctx.BindQuery(&in); err != nil { |
||||
return err |
||||
} |
||||
{{- end}} |
||||
{{- else}} |
||||
if err := ctx.BindQuery(&in{{.Body}}); err != nil { |
||||
return err |
||||
} |
||||
{{- end}} |
||||
{{- if .HasVars}} |
||||
if err := ctx.BindVars(&in); err != nil { |
||||
return err |
||||
} |
||||
{{- end}} |
||||
http.SetOperation(ctx,Operation{{$svrType}}{{.OriginalName}}) |
||||
h := ctx.Middleware(func(ctx context.Context, req interface{}) (interface{}, error) { |
||||
return srv.{{.Name}}(ctx, req.(*{{.Request}})) |
||||
}) |
||||
out, err := h(ctx, &in) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
reply := out.(*{{.Reply}}) |
||||
return ctx.Result(200, reply{{.ResponseBody}}) |
||||
} |
||||
} |
||||
{{end}} |
||||
|
||||
type {{.ServiceType}}HTTPClient interface { |
||||
{{- range .MethodSets}} |
||||
{{.Name}}(ctx context.Context, req *{{.Request}}, opts ...http.CallOption) (rsp *{{.Reply}}, err error) |
||||
{{- end}} |
||||
} |
||||
|
||||
type {{.ServiceType}}HTTPClientImpl struct{ |
||||
cc *http.Client |
||||
} |
||||
|
||||
func New{{.ServiceType}}HTTPClient (client *http.Client) {{.ServiceType}}HTTPClient { |
||||
return &{{.ServiceType}}HTTPClientImpl{client} |
||||
} |
||||
|
||||
{{range .MethodSets}} |
||||
func (c *{{$svrType}}HTTPClientImpl) {{.Name}}(ctx context.Context, in *{{.Request}}, opts ...http.CallOption) (*{{.Reply}}, error) { |
||||
var out {{.Reply}} |
||||
pattern := "{{.Path}}" |
||||
path := binding.EncodeURL(pattern, in, {{not .HasBody}}) |
||||
opts = append(opts, http.Operation(Operation{{$svrType}}{{.OriginalName}})) |
||||
opts = append(opts, http.PathTemplate(pattern)) |
||||
{{if .HasBody -}} |
||||
err := c.cc.Invoke(ctx, "{{.Method}}", path, in{{.Body}}, &out{{.ResponseBody}}, opts...) |
||||
{{else -}} |
||||
err := c.cc.Invoke(ctx, "{{.Method}}", path, nil, &out{{.ResponseBody}}, opts...) |
||||
{{end -}} |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
return &out, err |
||||
} |
||||
{{end}} |
@ -1,4 +1,4 @@ |
||||
package main |
||||
|
||||
// release is the current protoc-gen-go-http version.
|
||||
const release = "v2.6.3" |
||||
const release = "v2.4.1" |
||||
|
@ -1,90 +0,0 @@ |
||||
package apollo |
||||
|
||||
import ( |
||||
"testing" |
||||
) |
||||
|
||||
func Test_genKey(t *testing.T) { |
||||
type args struct { |
||||
ns string |
||||
sub string |
||||
} |
||||
tests := []struct { |
||||
name string |
||||
args args |
||||
want string |
||||
}{ |
||||
{ |
||||
name: "blank namespace", |
||||
args: args{ |
||||
ns: "", |
||||
sub: "x.y", |
||||
}, |
||||
want: "x.y", |
||||
}, |
||||
{ |
||||
name: "properties namespace", |
||||
args: args{ |
||||
ns: "application", |
||||
sub: "x.y", |
||||
}, |
||||
want: "application.x.y", |
||||
}, |
||||
{ |
||||
name: "namespace with format", |
||||
args: args{ |
||||
ns: "app.yaml", |
||||
sub: "x.y", |
||||
}, |
||||
want: "app.x.y", |
||||
}, |
||||
} |
||||
for _, tt := range tests { |
||||
t.Run(tt.name, func(t *testing.T) { |
||||
if got := genKey(tt.args.ns, tt.args.sub); got != tt.want { |
||||
t.Errorf("genKey() = %v, want %v", got, tt.want) |
||||
} |
||||
}) |
||||
} |
||||
} |
||||
|
||||
func Test_format(t *testing.T) { |
||||
tests := []struct { |
||||
name string |
||||
namespace string |
||||
want string |
||||
}{ |
||||
{ |
||||
name: "properties namespace", |
||||
namespace: "application", |
||||
want: "json", |
||||
}, |
||||
{ |
||||
name: "properties namespace #1", |
||||
namespace: "app.setting", |
||||
want: "json", |
||||
}, |
||||
{ |
||||
name: "namespace with format[yaml]", |
||||
namespace: "app.yaml", |
||||
want: "yaml", |
||||
}, |
||||
{ |
||||
name: "namespace with format[yml]", |
||||
namespace: "app.yml", |
||||
want: "yml", |
||||
}, |
||||
{ |
||||
name: "namespace with format[json]", |
||||
namespace: "app.json", |
||||
want: "json", |
||||
}, |
||||
} |
||||
for _, tt := range tests { |
||||
t.Run(tt.name, func(t *testing.T) { |
||||
if got := format(tt.namespace); got != tt.want { |
||||
t.Errorf("format() = %v, want %v", got, tt.want) |
||||
} |
||||
}) |
||||
} |
||||
} |
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue