Skip to content
This repository has been archived by the owner on Mar 4, 2022. It is now read-only.

Commit

Permalink
Add walk-timeout command line flag (#545)
Browse files Browse the repository at this point in the history
* Adds walk timeout to command line flag to commands which walk the directory structure.
  • Loading branch information
Steve Ayers authored Mar 31, 2020
1 parent b337d63 commit 45aa5ef
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/.idea
/.tmp
/.vscode
/prototool
/bazel-*
/brew
/etc/docker/testing/gen
Expand Down
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ all: lint cover bazeltest bazelbuild
install:
go install ./cmd/prototool

.PHONy: bins
bins:
go build ./cmd/prototool

.PHONY: license
license: __eval_srcs $(UPDATE_LICENSE)
update-license $(SRCS)
Expand Down
5 changes: 5 additions & 0 deletions internal/cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ type flags struct {
tmp bool
uncomment bool
generateIgnores bool
walkTimeout string
}

func (f *flags) bindAddress(flagSet *pflag.FlagSet) {
Expand Down Expand Up @@ -260,3 +261,7 @@ func (f *flags) bindKey(flagSet *pflag.FlagSet) {
func (f *flags) bindServerName(flagSet *pflag.FlagSet) {
flagSet.StringVar(&f.serverName, "server-name", "", "Override expected server \"Common Name\" when validating TLS certificate. Should usually be set if using a HTTP proxy or an IP for the --address. If set, --tls is required.")
}

func (f *flags) bindWalkTimeout(flagSet *pflag.FlagSet) {
flagSet.StringVar(&f.walkTimeout, "walk-timeout", "3s", "The maximum time to allow for walking the directory structure looking for proto files.")
}
25 changes: 25 additions & 0 deletions internal/cmd/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"io"
"os"
"strings"
"time"

wordwrap "github.com/mitchellh/go-wordwrap"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -55,6 +56,7 @@ var (
flags.bindProtocURL(flagSet)
flags.bindProtocBinPath(flagSet)
flags.bindProtocWKTPath(flagSet)
flags.bindWalkTimeout(flagSet)
},
}

Expand All @@ -75,6 +77,7 @@ var (
flags.bindProtocURL(flagSet)
flags.bindProtocBinPath(flagSet)
flags.bindProtocWKTPath(flagSet)
flags.bindWalkTimeout(flagSet)
},
}

Expand All @@ -94,6 +97,7 @@ var (
flags.bindProtocBinPath(flagSet)
flags.bindProtocWKTPath(flagSet)
flags.bindOutputPathBreakDescriptorSet(flagSet)
flags.bindWalkTimeout(flagSet)
},
}

Expand Down Expand Up @@ -121,6 +125,7 @@ Artifacts are downloaded to the following directories based on flags and environ
BindFlags: func(flagSet *pflag.FlagSet, flags *flags) {
flags.bindCachePath(flagSet)
flags.bindConfigData(flagSet)
flags.bindWalkTimeout(flagSet)
},
}

Expand Down Expand Up @@ -157,6 +162,7 @@ Artifacts are downloaded to the following directories based on flags and environ
flags.bindProtocURL(flagSet)
flags.bindProtocBinPath(flagSet)
flags.bindProtocWKTPath(flagSet)
flags.bindWalkTimeout(flagSet)
},
}

Expand Down Expand Up @@ -250,6 +256,7 @@ If Vim integration is set up, files will be generated when you open a new Protob
flags.bindProtocWKTPath(flagSet)
flags.bindOutputPath(flagSet)
flags.bindTmp(flagSet)
flags.bindWalkTimeout(flagSet)
},
}

Expand All @@ -262,6 +269,7 @@ If Vim integration is set up, files will be generated when you open a new Protob
},
BindFlags: func(flagSet *pflag.FlagSet, flags *flags) {
flags.bindConfigData(flagSet)
flags.bindWalkTimeout(flagSet)
},
}

Expand All @@ -284,6 +292,7 @@ If Vim integration is set up, files will be generated when you open a new Protob
flags.bindProtocURL(flagSet)
flags.bindProtocBinPath(flagSet)
flags.bindProtocWKTPath(flagSet)
flags.bindWalkTimeout(flagSet)
},
}

Expand All @@ -303,6 +312,7 @@ If Vim integration is set up, files will be generated when you open a new Protob
flags.bindProtocURL(flagSet)
flags.bindProtocBinPath(flagSet)
flags.bindProtocWKTPath(flagSet)
flags.bindWalkTimeout(flagSet)
},
}

Expand Down Expand Up @@ -397,6 +407,7 @@ $ prototool grpc example \
flags.bindCert(flagSet)
flags.bindKey(flagSet)
flags.bindServerName(flagSet)
flags.bindWalkTimeout(flagSet)
},
}

Expand All @@ -414,6 +425,7 @@ $ prototool grpc example \
flags.bindProtocURL(flagSet)
flags.bindProtocBinPath(flagSet)
flags.bindProtocWKTPath(flagSet)
flags.bindWalkTimeout(flagSet)
},
}

Expand All @@ -432,6 +444,7 @@ $ prototool grpc example \
flags.bindProtocURL(flagSet)
flags.bindProtocBinPath(flagSet)
flags.bindProtocWKTPath(flagSet)
flags.bindWalkTimeout(flagSet)
},
}

Expand All @@ -450,6 +463,7 @@ $ prototool grpc example \
flags.bindProtocURL(flagSet)
flags.bindProtocBinPath(flagSet)
flags.bindProtocWKTPath(flagSet)
flags.bindWalkTimeout(flagSet)
},
}

Expand Down Expand Up @@ -488,6 +502,7 @@ $ prototool grpc example \
flags.bindProtocBinPath(flagSet)
flags.bindProtocWKTPath(flagSet)
flags.bindGenerateIgnores(flagSet)
flags.bindWalkTimeout(flagSet)
},
}

Expand Down Expand Up @@ -634,6 +649,16 @@ func getRunner(develMode bool, stdin io.Reader, stdout io.Writer, stderr io.Writ
exec.RunnerWithProtocURL(flags.protocURL),
)
}
if flags.walkTimeout != "" {
parsedWalkTimeout, err := time.ParseDuration(flags.walkTimeout)
if err != nil {
return nil, err
}
runnerOptions = append(
runnerOptions,
exec.RunnerWithWalkTimeout(parsedWalkTimeout),
)
}
if develMode {
runnerOptions = append(
runnerOptions,
Expand Down
8 changes: 8 additions & 0 deletions internal/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ package exec

import (
"io"
"time"

"go.uber.org/zap"
)
Expand Down Expand Up @@ -134,6 +135,13 @@ func RunnerWithProtocURL(protocURL string) RunnerOption {
}
}

// RunnerWithWalkTimeout returns a RunnerOption that sets a given walk timeout
func RunnerWithWalkTimeout(walkTimeout time.Duration) RunnerOption {
return func(runner *runner) {
runner.walkTimeout = walkTimeout
}
}

// NewRunner returns a new Runner.
//
// workDirPath should generally be the current directory.
Expand Down
2 changes: 2 additions & 0 deletions internal/exec/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ type runner struct {
protocURL string
errorFormat string
json bool
walkTimeout time.Duration
}

func newRunner(workDirPath string, input io.Reader, output io.Writer, options ...RunnerOption) *runner {
Expand All @@ -92,6 +93,7 @@ func newRunner(workDirPath string, input io.Reader, output io.Writer, options ..
}
protoSetProviderOptions := []file.ProtoSetProviderOption{
file.ProtoSetProviderWithLogger(runner.logger),
file.ProtoSetProviderWithWalkTimeout(runner.walkTimeout),
}
if runner.configData != "" {
protoSetProviderOptions = append(
Expand Down
3 changes: 0 additions & 3 deletions internal/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ import (
"go.uber.org/zap"
)

// DefaultWalkTimeout is the default walk timeout.
const DefaultWalkTimeout time.Duration = 3 * time.Second

var rootDirPath = filepath.Dir(string(filepath.Separator))

// ProtoSet represents a set of .proto files and an associated config.
Expand Down
6 changes: 4 additions & 2 deletions internal/file/proto_set_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ type protoSetProvider struct {

func newProtoSetProvider(options ...ProtoSetProviderOption) *protoSetProvider {
protoSetProvider := &protoSetProvider{
logger: zap.NewNop(),
walkTimeout: DefaultWalkTimeout,
logger: zap.NewNop(),
}
for _, option := range options {
option(protoSetProvider)
Expand Down Expand Up @@ -225,6 +224,9 @@ func (c *protoSetProvider) walkAndGetAllProtoFiles(absWorkDirPath string, absDir
return nil, err
}
}

c.logger.Debug("walking the directory structure", zap.Duration("walkTimeout", c.walkTimeout))

walkErrC := make(chan error)
go func() {
walkErrC <- filepath.Walk(
Expand Down

0 comments on commit 45aa5ef

Please sign in to comment.