Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Walk parent directories to find config file #141

Merged
merged 7 commits into from
Oct 22, 2021
Merged

Walk parent directories to find config file #141

merged 7 commits into from
Oct 22, 2021

Conversation

johnmaguire
Copy link
Contributor

Code is mostly borrowed from gqlgen.

The idea here is that I want to be able to store genqlient.yaml at the top-level, but my client code lives down in graph/client/. I put the //go:generate line in graph/client/client.go.

@StevenACoffman
Copy link
Member

@johnmaguire Can you add test coverage for findCfg and findCfgInDir? The addition of pkg/errors vs stdlib probably warrants discussion from other maintainers. Possibly relevant to #118

@johnmaguire
Copy link
Contributor Author

@StevenACoffman I removed the dependency on errors and added test coverage. Let me know if this needs anything else!

Copy link
Member

@csilvers csilvers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am wondering about documentation -- where is the right place to document this behavior? Maybe not the help text, but in the README maybe? I do think you want to add an entry to the changelog for this.

If you don't mind waiting a few days, it would be nice to wait for @benjaminjkraft to weigh in on the semantics -- whether allowing the directory search is desirable. I don't see a problem with it myself, but I do know that a 'chdir' in code is a red flag for some, so it might be good to get his opinion.

Copy link
Collaborator

@benjaminjkraft benjaminjkraft left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution! This seems like a very reasonable feature to me.

I mention inline one place to document it. It would also be nice to add a mention of where genqlient looks for the config file at the top of the genqlient.yaml documentation; just to say that you can pass it a path or by default it looks for such-and-such filenames in ancestor directories.

BTW, I'm not necessarily opposed to chdir, as long as it doesn't cause surprising behavior elsewhere, but I think in theory you shouldn't need it! In which case best not.

generate/main.go Outdated
@@ -42,7 +51,7 @@ func readConfigGenerateAndWrite(configFilename string) error {
}

type cliArgs struct {
ConfigFilename string `arg:"positional" placeholder:"CONFIG" default:"genqlient.yaml" help:"path to genqlient configuration (default genqlient.yaml)"`
ConfigFilename string `arg:"positional" placeholder:"CONFIG" default:"" help:"path to genqlient configuration (default genqlient.yaml)"`
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'll need to change initConfig, below, to also handle ""! I think you can just have it default to genqlient.yaml (in the current directory).

Also, if you can briefly document the new behavior here, e.g. "(default: search ancestors of the current directory for genqlient.yaml)".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the documentation, let me know if it's OK or feel free to fix the wording as preferred.

return nil, err
}

err = os.Chdir(filepath.Dir(cfgFile))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There shouldn't be a need to chdir, I think! Did you see problems without it? As long as cfgFile passed to ReadAndValidateConfig is an absolute or current-directory-relative path, everything should be fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I didn't. I wasn't sure if the program relied on the current directory for anything besides the config file path, so I figured it was safer to add it.

It seems to be working for my limited use case, so I've pushed a commit to remove it.

@benjaminjkraft benjaminjkraft merged commit 10dc388 into Khan:main Oct 22, 2021
@johnmaguire johnmaguire deleted the default-config-file branch October 22, 2021 13:25
@johnmaguire
Copy link
Contributor Author

Thanks for merging this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants