-
Notifications
You must be signed in to change notification settings - Fork 345
Conversation
…son.Unmarshal if the file is empty
Codecov Report
@@ Coverage Diff @@
## dev #224 +/- ##
==========================================
+ Coverage 69.11% 69.65% +0.53%
==========================================
Files 73 73
Lines 3785 3865 +80
==========================================
+ Hits 2616 2692 +76
- Misses 844 846 +2
- Partials 325 327 +2
Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good overall! Let's see if we can remove the configDirPath
field and simplify this a little.
CHANGELOG.md
Outdated
@@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. | |||
## [Unreleased] | |||
- Accept `prototool.json` files for configuation in addition to | |||
`prototool.yaml` files. | |||
- Add `--config-data` flag. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's add more context here, such as how this can be used and/or is useful.
@@ -63,6 +64,10 @@ func (f *flags) bindCallTimeout(flagSet *pflag.FlagSet) { | |||
flagSet.StringVar(&f.callTimeout, "call-timeout", "60s", "The maximum time to for all calls to be completed.") | |||
} | |||
|
|||
func (f *flags) bindConfigData(flagSet *pflag.FlagSet) { | |||
flagSet.StringVar(&f.configData, "config-data", "", "The configuration data to use instead of reading prototool.yaml or prototool.json files.\nThis will act as if there is a configuration file with the given data in the current directory, and no other configuration files recursively.\nThis is an advanced feature and is not recommended to be generally used.") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should shorten this description. The first sentence will likely suffice:
The configuration data to use instead of reading prototool.yaml or prototool.json files.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmmm, I disagree, let's cover this today offline?
@@ -45,6 +45,7 @@ var ( | |||
return runner.All(args, flags.disableFormat, flags.disableLint, flags.fix) | |||
}, | |||
BindFlags: func(flagSet *pflag.FlagSet, flags *flags) { | |||
flags.bindConfigData(flagSet) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not just make this a global flag, similar to debug
? I realize that some commands might not need a configuration, such as version
or config init
, but the same could be said about debug
, so adding this to the global set seems fine to me. What are your thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm indifferent here, since version
/files
/config
won't rely on it. It would be nice as a global flag for discovery purposes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't global, by the same argument, --protoc-url
should be global and we chose that not to be global.
internal/file/proto_set_provider.go
Outdated
@@ -33,6 +33,8 @@ import ( | |||
|
|||
type protoSetProvider struct { | |||
logger *zap.Logger | |||
configDirPath string |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should be able to drop this field (see below).
internal/file/proto_set_provider.go
Outdated
configDirPath := absDirPath | ||
if configFilePath != "" { | ||
configDirPath = filepath.Dir(configFilePath) | ||
configDirPath := c.configDirPath |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's remove protoSetProvider.configDirPath
and just use the workDirPath
instead. They should always be the same.
configDirPath := absDirPath
if c.configData {
// The users has specified configuration via the command line. Set the configuration
// directory to the current working directory.
configDirPath = workDirPath
} else {
configFilePath, err := c.configProvider.GetFilePathForDir(absDirPath)
if err != nil {
return nil, err
}
// We need everything for generation, not just the files in the given directory.
// Traverse back to the config file if it is shallower.
// The display path will be unaffected as this is based on workDirPath.
if configFilePath != "" {
configDirPath = filepath.Dir(configFilePath)
}
}
...
This will let us drop the validateOptions
helper, too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💯
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This logic is slightly wrong, you never use absDirPath
as a value for configDirPath
directly and the else
is therefore not needed, but corrected it and updated.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also note that this now requires propagating the work dir path to one more place, but we were already propagating it.
internal/file/proto_set_provider.go
Outdated
@@ -195,6 +218,17 @@ func (c *protoSetProvider) walkAndGetAllProtoFiles(absWorkDirPath string, absDir | |||
timedOut bool | |||
) | |||
allExcludes := make(map[string]struct{}) | |||
// if we have a configDirPath and configData, we compute the exclude prefixes once | |||
// from this dirPath and data, and do not do it again in the below walk function | |||
if c.configDirPath != "" { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can update this to c.configData != ""
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
internal/file/proto_set_provider.go
Outdated
for _, exclude := range excludes { | ||
allExcludes[exclude] = struct{}{} | ||
// Do not add if we have configDirPath and configData. | ||
if c.configDirPath == "" { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can update this to c.configData == ""
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
internal/file/proto_set_provider.go
Outdated
return fmt.Errorf("both configDirPath and configData in protoSetProvider must be set or unset") | ||
} | ||
return nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This shouldn't be necessary (commented above).
internal/settings/config_provider.go
Outdated
} | ||
dirPath = filepath.Clean(dirPath) | ||
return externalConfigToConfig(externalConfig, dirPath) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Neat 👍
@@ -83,6 +92,18 @@ func (c *configProvider) GetExcludePrefixesForDir(dirPath string) ([]string, err | |||
return getExcludePrefixesForDir(dirPath) | |||
} | |||
|
|||
func (c *configProvider) GetExcludePrefixesForData(dirPath string, externalConfigData string) ([]string, error) { | |||
if !filepath.IsAbs(dirPath) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we have this validation in GetForData
, too?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Before this change, the protoSetProvider
required no information about configuration and I think we should keep it that way. We're leaking configuration information into protoSetProvider
when instead this should be contained within the configProvider
-- a protoSetProvider
shouldn't care how configuration got there, just that it exists.
A protoSetProvider
already holds a configProvider
, which means that the protoSetProvider
s current implementation can be left virtually unchanged. Instead of creating a ProtoSetProviderWithConfigData
option, we should replace it with an equivalent ConfigProviderOption
. This would enable us to leave the ConfigProvider
interface unchanged too so we'd remove a lot of the duplication that's been introduced. I think we can get a lot of simplification by going this alternate route.
Some of the comments @amckinney's left are still relevant with this suggested change. Specifically, not needing the configDirPath
.
@@ -45,6 +45,7 @@ var ( | |||
return runner.All(args, flags.disableFormat, flags.disableLint, flags.fix) | |||
}, | |||
BindFlags: func(flagSet *pflag.FlagSet, flags *flags) { | |||
flags.bindConfigData(flagSet) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm indifferent here, since version
/files
/config
won't rely on it. It would be nice as a global flag for discovery purposes.
internal/file/proto_set_provider.go
Outdated
configDirPath := absDirPath | ||
if configFilePath != "" { | ||
configDirPath = filepath.Dir(configFilePath) | ||
configDirPath := c.configDirPath |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💯
@AlexAPB perhaps we should sync offline - I don't get what you're concretely suggesting. The same amount of information is leaked from what I see, and changing the logic is a significant refactor. Maybe I'm missing something, but we should sync offline and/or I would want a concrete suggestion on how to proceed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Talked offline. My reservations with this change would be rectified by a much larger refactor that we don't really have the bandwidth for right now. This is fine for now, since are our 1.0 commitment is to the public commands and flags.
For a clean up change (sometime in the future) we should ideally create the settings provider once and thread that information along throughout the invocation. It would make changes like this less leaky.
This adds the
--config-data
flag.Having played with this, I actually do wish we added a
--config-dir
or--config-dir-path
flag as well, If you play with it locally you might see what I mean, but I understand the reasoning not to add it.A nice representation of having a JSON file and then
cat
'ing it on the command line would be to cd intointernal/cmd/testdata/format/proto3
and then doing:Note if you have bash completion set up on your computer and installed with
brew install prototool
, you need to dobrew uninstall prototool
forprototool
to be picked up on your$PATH
frommake install
, as bash completion will still make it so that the Homebrewprototool
binary is used, regardless of the order of the directories in your$PATH
.