Skip to content

Commit

Permalink
Merge pull request #89 from k1LoW/organize-options
Browse files Browse the repository at this point in the history
Organize options
  • Loading branch information
k1LoW authored May 4, 2024
2 parents edd8ba4 + e396044 commit 779de59
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 63 deletions.
12 changes: 6 additions & 6 deletions grpcstub.go
Original file line number Diff line number Diff line change
Expand Up @@ -949,14 +949,14 @@ func (s *Server) resolveProtos(ctx context.Context, c *config) error {
return err
}
var bufresolvOpts []bufresolv.Option
if c.bufDir != "" {
bufresolvOpts = append(bufresolvOpts, bufresolv.BufDir(c.bufDir))
for _, dir := range c.bufDirs {
bufresolvOpts = append(bufresolvOpts, bufresolv.BufDir(dir))
}
if c.bufConfig != "" {
bufresolvOpts = append(bufresolvOpts, bufresolv.BufConfig(c.bufConfig))
for _, config := range c.bufConfigs {
bufresolvOpts = append(bufresolvOpts, bufresolv.BufConfig(config))
}
if c.bufLock != "" {
bufresolvOpts = append(bufresolvOpts, bufresolv.BufLock(c.bufLock))
for _, lock := range c.bufLocks {
bufresolvOpts = append(bufresolvOpts, bufresolv.BufLock(lock))
}
bufresolvOpts = append(bufresolvOpts, bufresolv.BufModule(c.bufModules...))
br, err := bufresolv.New(bufresolvOpts...)
Expand Down
105 changes: 48 additions & 57 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,54 +15,19 @@ type config struct {
cacert, cert, key []byte
healthCheck bool
disableReflection bool
bufDir string
bufLock string
bufConfig string
bufDirs []string
bufLocks []string
bufConfigs []string
bufModules []string
}

type Option func(*config) error

// Proto append proto
func Proto(proto string) Option {
return func(c *config) error {
protos := []string{}
if f, err := os.Stat(proto); err == nil {
if !f.IsDir() {
c.protos = unique(append(c.protos, proto))
return nil
}
proto = filepath.Join(proto, "*")
}
base, pattern := doublestar.SplitPattern(filepath.ToSlash(proto))
abs, err := filepath.Abs(base)
if err != nil {
return err
}
fsys := os.DirFS(abs)
if err := doublestar.GlobWalk(fsys, pattern, func(p string, d fs.DirEntry) error {
if d.IsDir() {
return nil
}
protos = unique(append(protos, filepath.Join(base, p)))
return nil
}); err != nil {
return err
}
if len(protos) == 0 {
c.protos = unique(append(c.protos, proto))
} else {
c.protos = unique(append(c.protos, protos...))
}
return nil
}
}

// Protos append protos
func Protos(protos []string) Option {
// Proto append protos
func Proto(protos ...string) Option {
return func(c *config) error {
for _, p := range protos {
opt := Proto(p)
opt := proto(p)
if err := opt(c); err != nil {
return err
}
Expand All @@ -71,16 +36,8 @@ func Protos(protos []string) Option {
}
}

// ImportPath set import path
func ImportPath(path string) Option {
return func(c *config) error {
c.importPaths = unique(append(c.importPaths, path))
return nil
}
}

// ImportPaths set import paths
func ImportPaths(paths []string) Option {
// ImportPath set import paths
func ImportPath(paths ...string) Option {
return func(c *config) error {
c.importPaths = unique(append(c.importPaths, paths...))
return nil
Expand Down Expand Up @@ -115,25 +72,25 @@ func DisableReflection() Option {
}

// BufDir use buf directory.
func BufDir(dir string) Option {
func BufDir(dirs ...string) Option {
return func(c *config) error {
c.bufDir = dir
c.bufDirs = unique(append(c.bufDirs, dirs...))
return nil
}
}

// BufLock use buf.lock for BSR.
func BufLock(lock string) Option {
func BufLock(locks ...string) Option {
return func(c *config) error {
c.bufLock = lock
c.bufLocks = unique(append(c.bufLocks, locks...))
return nil
}
}

// BufConfig use buf.yaml for BSR.
func BufConfig(p string) Option {
func BufConfig(configs ...string) Option {
return func(c *config) error {
c.bufConfig = p
c.bufConfigs = unique(append(c.bufConfigs, configs...))
return nil
}
}
Expand All @@ -159,6 +116,40 @@ func BufModules(modules []string) Option {
}
}

func proto(proto string) Option {
return func(c *config) error {
protos := []string{}
if f, err := os.Stat(proto); err == nil {
if !f.IsDir() {
c.protos = unique(append(c.protos, proto))
return nil
}
proto = filepath.Join(proto, "*")
}
base, pattern := doublestar.SplitPattern(filepath.ToSlash(proto))
abs, err := filepath.Abs(base)
if err != nil {
return err
}
fsys := os.DirFS(abs)
if err := doublestar.GlobWalk(fsys, pattern, func(p string, d fs.DirEntry) error {
if d.IsDir() {
return nil
}
protos = unique(append(protos, filepath.Join(base, p)))
return nil
}); err != nil {
return err
}
if len(protos) == 0 {
c.protos = unique(append(c.protos, proto))
} else {
c.protos = unique(append(c.protos, protos...))
}
return nil
}
}

func unique(in []string) []string {
u := []string{}
m := map[string]struct{}{}
Expand Down

0 comments on commit 779de59

Please sign in to comment.