Skip to content

Commit 654a3b4

Browse files
committed
feat(jzero): remove embedx
1 parent 8088605 commit 654a3b4

4 files changed

Lines changed: 98 additions & 105 deletions

File tree

cmd/jzero/.template/frame/gateway/app/cmd/server.go.tpl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
configurator "github.com/zeromicro/go-zero/core/configcenter"
77
"github.com/jzero-io/jzero/core/configcenter/subscriber"
88
"github.com/common-nighthawk/go-figure"
9-
"github.com/jzero-io/jzero/core/embedx"
109
"github.com/spf13/cobra"
1110
"github.com/zeromicro/go-zero/core/logx"
1211
"github.com/zeromicro/go-zero/core/service"
@@ -35,7 +34,7 @@ var serverCmd = &cobra.Command{
3534
logx.Must(logx.SetUp(c.Log.LogConf))
3635

3736
// write pb to local
38-
c.Gateway.Upstreams[0].ProtoSets, err = embedx.WriteToLocal(pb.Embed, embedx.WithFileMatchFunc(func(path string) bool {
37+
c.Gateway.Upstreams[0].ProtoSets, err = pb.WriteToLocal(pb.Embed, pb.WithFileMatchFunc(func(path string) bool {
3938
return filepath.Ext(path) == ".pb"
4039
}))
4140
logx.Must(err)
Lines changed: 97 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,105 @@
11
package pb
22

33
import (
4-
"embed"
4+
"embed"
5+
"fmt"
6+
"io/fs"
7+
"os"
8+
"path/filepath"
9+
10+
"github.com/pkg/errors"
511
)
612

713
var (
814
//go:embed *
915
Embed embed.FS
10-
)
16+
)
17+
18+
type Opts func(config *embedxConfig)
19+
20+
type embedxConfig struct {
21+
Dir string
22+
FileMatchFunc func(path string) bool
23+
}
24+
25+
func WithDir(dir string) Opts {
26+
return func(config *embedxConfig) {
27+
config.Dir = dir
28+
}
29+
}
30+
31+
func WithFileMatchFunc(fileFilter func(path string) bool) Opts {
32+
return func(config *embedxConfig) {
33+
config.FileMatchFunc = fileFilter
34+
}
35+
}
36+
37+
func WriteToLocal(ef embed.FS, opts ...Opts) ([]string, error) {
38+
config := &embedxConfig{}
39+
40+
for _, opt := range opts {
41+
opt(config)
42+
}
43+
44+
var fileList []string
45+
46+
err := fs.WalkDir(ef, ".", func(path string, d fs.DirEntry, err error) error {
47+
if err != nil {
48+
return err
49+
}
50+
if !d.IsDir() {
51+
data, err := ef.ReadFile(path)
52+
if err != nil {
53+
return err
54+
}
55+
if config.Dir != "" {
56+
if stat, err := os.Stat(config.Dir); err != nil {
57+
if !os.IsExist(err) {
58+
err = os.MkdirAll(config.Dir, 0o755)
59+
if err != nil {
60+
return err
61+
}
62+
}
63+
} else {
64+
if !stat.IsDir() {
65+
return errors.Errorf("%s: not a directory", config.Dir)
66+
}
67+
}
68+
}
69+
70+
var tmpFile *os.File
71+
if config.FileMatchFunc != nil {
72+
if config.FileMatchFunc(path) {
73+
if tmpFile, err = createTemp(config.Dir, path, data); err != nil {
74+
return err
75+
}
76+
}
77+
} else {
78+
if tmpFile, err = createTemp(config.Dir, path, data); err != nil {
79+
return err
80+
}
81+
}
82+
if tmpFile != nil {
83+
fileList = append(fileList, tmpFile.Name())
84+
}
85+
}
86+
return nil
87+
})
88+
if err != nil {
89+
return nil, err
90+
}
91+
return fileList, nil
92+
}
93+
94+
func createTemp(dir, path string, data []byte) (*os.File, error) {
95+
tmpFile, err := os.CreateTemp(dir, fmt.Sprintf("*%s", filepath.Ext(path)))
96+
if err != nil {
97+
return nil, err
98+
}
99+
defer tmpFile.Close()
100+
if _, err := tmpFile.Write(data); err != nil {
101+
return nil, err
102+
}
103+
104+
return tmpFile, nil
105+
}

core/embedx/embedx.go

Lines changed: 0 additions & 88 deletions
This file was deleted.

core/embedx/option.go

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)