Skip to content

Commit

Permalink
Merge pull request #30 from lazygophers/luoxin
Browse files Browse the repository at this point in the history
支持配置文件同步
  • Loading branch information
Luoxin authored Jun 17, 2024
2 parents f8c53c2 + 3fa03ba commit 4ac66a6
Show file tree
Hide file tree
Showing 164 changed files with 1,380 additions and 1,445 deletions.
14 changes: 2 additions & 12 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ run:
timeout: 30m
skip-dirs-use-default: false
skip-files:
- "*.gen.go"
- ".*.gen.go"
- ".*.pb.go"
modules-download-mode: readonly
allow-parallel-runners: true
Expand Down Expand Up @@ -483,15 +483,6 @@ linters-settings:
forbidigo:
forbid:
- '"github.com/lazygophers/log"'
tagliatelle:
case:
rules:
json: snake
yaml: snake
xml: snake
bson: snake
avro: snake
toml: snake
nilnil:
checked-types:
- ptr
Expand All @@ -503,6 +494,7 @@ linters-settings:
disabled-checks:
# 可能需要删除的代码
- commentedOutCode
- octalLiteral
- elseif
- rangeValCopy
- paramTypeCombine
Expand Down Expand Up @@ -631,8 +623,6 @@ linters:
- varnamelen
- wastedassign
- testpackage
- paralleltest
- gofmt
- nilerr
- tagliatelle
- nakedret
14 changes: 7 additions & 7 deletions cli/i18n/tran.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,10 @@ var tranCmd = &cobra.Command{
return errors.New("no localizer for " + filepath.Ext(srcFile))
}

c.Translater, ok = i18n.GetTranslater(i18n.TransacteType(state.Config.I18n.Translater))
c.Translator, ok = i18n.GetTranslator(i18n.TransacteType(state.Config.I18n.Translator))
if !ok {
pterm.Warning.Printfln("no translater for %s", state.Config.I18n.Translater)
return errors.New("no translater be found")
pterm.Warning.Printfln("no translator for %s", state.Config.I18n.Translator)
return errors.New("no translator be found")
}

err = i18n.Translate(c)
Expand Down Expand Up @@ -267,8 +267,8 @@ func mergeTranCmdFlags(cmd *cobra.Command) {
if cmd.Flag("generate-const").Changed {
state.Config.I18n.GenerateConst = utils.GetBool("generate-const", cmd)
}
if cmd.Flag("translater").Changed {
state.Config.I18n.Translater = utils.GetString("translater", cmd)
if cmd.Flag("translator").Changed {
state.Config.I18n.Translator = utils.GetString("translator", cmd)
}

if cmd.Flag("all-languages").Changed {
Expand Down Expand Up @@ -296,9 +296,9 @@ func initTran() {
tranCmd.Flags().StringSliceP("languages", "l", state.Config.I18n.Languages, state.Localize(state.I18nTagCliI18nTranFlagsLanguages))

tranCmd.Flags().Bool("generate-const", state.Config.I18n.GenerateConst, state.Localize(state.I18nTagCliI18nTranFlagsGenerateConst))
tranCmd.Flags().String("translater", state.Config.I18n.Translater, state.Localize(state.I18nTagCliI18nTranFlagsTranslaterUsage, map[string]any{
tranCmd.Flags().String("translator", state.Config.I18n.Translator, state.Localize(state.I18nTagCliI18nTranFlagsTranslatorUsage, map[string]any{
"Type": map[i18n.TransacteType]string{
i18n.TransacteTypeGoogleFree: state.Localize(state.I18nTagCliI18nTranFlagsTranslaterGoogleFree),
i18n.TransacteTypeGoogleFree: state.Localize(state.I18nTagCliI18nTranFlagsTranslatorGoogleFree),
},
}))

Expand Down
48 changes: 30 additions & 18 deletions cli/sync.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cli

import (
"bytes"
"encoding/base64"
"fmt"
"github.com/go-resty/resty/v2"
Expand All @@ -10,7 +11,6 @@ import (
"github.com/lazygophers/lrpc/middleware/xerror"
"github.com/lazygophers/utils/app"
"github.com/lazygophers/utils/cryptox"
"github.com/lazygophers/utils/fake"
"github.com/lazygophers/utils/json"
"github.com/lazygophers/utils/osx"
"github.com/lazygophers/utils/runtime"
Expand All @@ -19,7 +19,9 @@ import (
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
"io"
"io/fs"
"net/http"
"net/url"
"os"
"path/filepath"
"reflect"
Expand Down Expand Up @@ -184,6 +186,13 @@ var syncCmd = &cobra.Command{
// 存储到当前目录下的文件(按照文件名 hash)
fileName := filepath.Join(runtime.UserConfigDir(), app.Organization, "codegen", cryptox.Md5(c.Sync.Remote), "codegen.cfg.yaml")

file, err := os.OpenFile(fileName, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0666)
if err != nil {
log.Errorf("err:%v", err)
return err
}
defer file.Close()

encoder := yaml.NewEncoder(file)
encoder.SetIndent(4)

Expand Down Expand Up @@ -214,12 +223,13 @@ var syncCmd = &cobra.Command{
}

func syncFromRemote(c *state.Cfg) error {

client := resty.New().
SetTimeout(time.Second * 10).
SetRetryWaitTime(time.Second).
SetRetryCount(3).
SetHeaders(map[string]string{
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.162 Safari/537.36",
"User-Agent": fmt.Sprintf("lazygophers/%s (%s)", app.Version, app.Organization),
}).
AddRetryCondition(func(response *resty.Response, err error) bool {
if err != nil {
Expand All @@ -241,12 +251,17 @@ func syncFromRemote(c *state.Cfg) error {
}
}).
SetHeaders(c.Sync.Headers).
OnBeforeRequest(func(client *resty.Client, request *resty.Request) error {
request.SetHeader("User-Agent", fake.RandomUserAgent())
return nil
}).
SetLogger(log.Clone().SetOutput(io.Discard)).
SetBaseURL(filepath.Dir(c.Sync.Remote))
SetLogger(log.Clone().SetOutput(io.Discard))

{
u, err := url.Parse(c.Sync.Remote)
if err != nil {
log.Errorf("err:%v", err)
return err
}
u.Path = filepath.Dir(u.Path)
client.SetBaseURL(u.String())
}

if os.Getenv("HTTPS_PROXY") != "" {
client.SetProxy(os.Getenv("HTTPS_PROXY"))
Expand All @@ -261,8 +276,9 @@ func syncFromRemote(c *state.Cfg) error {
c.Sync.CacheTemplatePath = filepath.Join(runtime.UserConfigDir(), app.Organization, "codegen", cacheKey, "template")
}

if !osx.IsDir(filepath.Dir(c.Sync.CacheTemplatePath)) {
err := os.MkdirAll(filepath.Dir(c.Sync.CacheTemplatePath), os.ModePerm)
if !osx.IsDir(c.Sync.CacheTemplatePath) {
pterm.Info.Printfln("try to create template directory at %s", c.Sync.CacheTemplatePath)
err := os.MkdirAll(c.Sync.CacheTemplatePath, fs.ModePerm)
if err != nil {
log.Errorf("err:%v", err)
return err
Expand All @@ -273,7 +289,6 @@ func syncFromRemote(c *state.Cfg) error {
// 获取新的配置
{
resp, err := client.R().
SetDoNotParseResponse(true).
Get(c.Sync.Remote)
if err != nil {
log.Errorf("err:%v", err)
Expand Down Expand Up @@ -308,14 +323,13 @@ func syncFromRemote(c *state.Cfg) error {
}

if unmarshaler, ok := state.SupportedExt[ext]; ok {
err = unmarshaler(resp.RawBody(), &nc)
resp.RawBody().Close()
err = unmarshaler(bytes.NewBuffer(resp.Body()), &nc)
if err != nil {
log.Error(string(resp.Body()))
log.Errorf("err:%v", err)
return err
}
} else {
resp.RawBody().Close()
log.Errorf("unsupported config file format:%v", ext)
return fmt.Errorf("unsupported config file format:%v", ext)
}
Expand Down Expand Up @@ -400,15 +414,13 @@ func syncFromRemote(c *state.Cfg) error {
pterm.Info.Printfln("try get template from %s", dst.String())
resp, err := client.R().Get(dst.String())
if err != nil {
log.Errorf("err:%v", err)
return err
}
if err != nil {
log.Errorf("get template from %s", resp.Request.URL)
log.Errorf("err:%v", err)
return err
}

if resp.StatusCode() != http.StatusOK {
log.Errorf("get template from %s", resp.Request.URL)
log.Errorf("unexpected status code: %v", resp.StatusCode())
return xerror.NewError(int32(resp.StatusCode()))
}
Expand Down
4 changes: 4 additions & 0 deletions codegen/generate_add_rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,10 @@ func GenerateAddRpc(pb *PbPackage, msg *PbMessage, opt *AddRpcOption) (err error
"Model": opt.Model,
"Role": role,
})
if err != nil {
log.Errorf("err:%v", err)
return err
}

//rpcName = b.String()
rpcName = strings.ReplaceAll(b.String(), " ", "")
Expand Down
30 changes: 22 additions & 8 deletions codegen/generate_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,18 @@ func GenerateImpl(pb *PbPackage) (err error) {
return err
}

if len(goPackage) == 0 {
goPackage = make(map[string]*GoPackage)
}

if goPackage["impl"] == nil {
goPackage["impl"] = &GoPackage{}
}

if len(goPackage["impl"].FuncMap) == 0 {
goPackage["impl"].FuncMap = make(map[string]*GoFuncNode)
}

matchFunc := func(rpc *PbRPC) bool {
for _, goFuncNode := range goPackage["impl"].FuncMap {
goFunc := goFuncNode.goFunc
Expand Down Expand Up @@ -324,18 +336,20 @@ func GenerateImpl(pb *PbPackage) (err error) {
}

type RpcRouteOption struct {
RpcName string
Path string
Method string
Role string
RpcName string
Path string
Method string
Role string
SkipGenRoute bool
}

func pbRpc2Route(rpc *PbRPC) *RpcRouteOption {
opt := &RpcRouteOption{
RpcName: rpc.Name,
Path: rpc.genOption.Path,
Method: rpc.genOption.Method,
Role: rpc.genOption.Role,
RpcName: rpc.Name,
Path: rpc.genOption.Path,
Method: rpc.genOption.Method,
Role: rpc.genOption.Role,
SkipGenRoute: rpc.genOption.SkipGenRoute,
}

return opt
Expand Down
2 changes: 1 addition & 1 deletion codegen/generate_pb_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func GenPbFile(pb *PbPackage) error {

// NOTE: 添加注释
{
goPbFilePath := filepath.Join(pb.ProjectRoot(), pb.PackageName+".pb.go")
goPbFilePath := filepath.Join(pb.ProjectRoot(), pb.PackageName()+".pb.go")

stat, err := os.Stat(goPbFilePath)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion codegen/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func GetPath(t PathType, pb *PbPackage) string {
return pb.ProjectRoot()

case PathTypePbGo:
return filepath.Join(pb.ProjectRoot(), pb.PackageName+".pb.go")
return filepath.Join(pb.ProjectRoot(), pb.PackageName()+".pb.go")

case PathTypeGoMod:
return filepath.Join(pb.ProjectRoot(), "go.mod")
Expand Down
Loading

0 comments on commit 4ac66a6

Please sign in to comment.