-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
cli/config: improve handling of errors #5077
Conversation
06e9312
to
604c531
Compare
Ah 🙈 probably testing won't work if it's running as
|
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.
LGTM (once CI is green)
604c531
to
4aab3ef
Compare
if runtime.GOOS != "windows" { | ||
skip.If(t, os.Getuid() == 0, "cannot test permission denied when running as root") | ||
} |
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 tried if I could make the tests run as non-root, but ran into some issues, so for now, skipping this tests when running as root.
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.
overal LGTM, with some godoc nits/questions
@@ -84,8 +84,14 @@ func LoadFromReader(configData io.Reader) (*configfile.ConfigFile, error) { | |||
return &configFile, err | |||
} | |||
|
|||
// Load reads the configuration files in the given directory, and sets up | |||
// the auth config information and returns values. | |||
// Load reads the configuration file ([ConfigFileName]) from the given directory. |
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.
what is ([ConfigFileName]) here?
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.
They're "doc links"; if you use brackets and put a reference to a type, then GoDoc renders them as links.
You can test those using pkgsite
(go install golang.org/x/pkgsite/cmd/pkgsite@latest
), and then browse the docs (it's a bit fiddly on the CLI because of go modules, so I just ran it in a container);
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.
Ah, good to know! I didn't think that having just the type name would be enough for the linking to occur
cli/config/config.go
Outdated
// Load reads the configuration files in the given directory, and sets up | ||
// the auth config information and returns values. | ||
// Load reads the configuration file ([ConfigFileName]) from the given directory. | ||
// If no directory is given, it uses the default [Dir]. A [configfile.ConfigFile] |
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.
nit: [*configfile.ConfigFile]
here since it's a pointer?
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.
// than it doesn't exist then stop | ||
return configFile, nil | ||
// Any other error happening when failing to read the file must be returned. | ||
return configFile, errors.Wrap(err, "loading config file") |
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.
nit: maybe errors.Wrapf(err, "loading config file: %s: ", filename)
like below so the errors have a consistent format?
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 left that out, because os.Open
returns a os.PathError
/ fs.PathError
; https://pkg.go.dev/os#PathError and those already include the path, which would be open /no/such/file: no such file or directory
, so manually adding the filename would now have the filename in the error twice.
cli/config/config.go
Outdated
} | ||
return configFile, err | ||
} | ||
|
||
// LoadDefaultConfigFile attempts to load the default config file and returns | ||
// an initialized ConfigFile struct if none is found. | ||
// an initialized ConfigFile struct if none is found or when failing to load |
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 not sure about godoc best practices when returning pointers to structs, should we specify that here as well or only in the function signature?
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.
Yeah, I left it out for now; I think we should consider deprecating this function, or at least provide an alternative that does return the error.
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 tried to slightly rephrase the GoDoc to make it more clear that it returns a reference, and that it initialises a default struct if it fails to load / find a config-file.
4aab3ef
to
c102e29
Compare
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #5077 +/- ##
==========================================
+ Coverage 61.32% 61.34% +0.02%
==========================================
Files 298 298
Lines 20706 20706
==========================================
+ Hits 12698 12703 +5
+ Misses 7106 7103 -3
+ Partials 902 900 -2 |
Signed-off-by: Sebastiaan van Stijn <[email protected]>
This may need further discussion, but we currently handle dangling symlinks gracefully, so let's add a test for this, and verify that we don't replace symlinks with a file. Signed-off-by: Sebastiaan van Stijn <[email protected]>
…nted Loading the config should print no warnings on a successful load. Signed-off-by: Sebastiaan van Stijn <[email protected]>
When attempting to load a config-file that exists, but is not accessible for the current user, we should not discard the error. This patch makes sure that the error is returned by Load(), but does not yet change LoadDefaultConfigFile, as this requires a change in signature. Signed-off-by: Sebastiaan van Stijn <[email protected]>
c102e29
to
dc75e9e
Compare
@krissetto I made some changes; PTAL if it LGTY |
looking good @thaJeztah 😎 |
Let me bring this one in; I'll try to spend some time on fixing the actual handling related to #5075, but taking it small steps at a time. |
Improving some parts of how we handle failures when loading the CLI configuration file. This relates to #5075, but does not yet fix that issue (only handling some additional errors);
Changes in this PR should not likely cause a change in behavior (so could be included in a patch release); follow-up changes are still needed to handle various scenarios where we should hard-fail, but there's many different related code-paths, so this still needs work.
cli/config: Load(): improve GoDoc
cli/config: add test for dangling symlink for config-file
This may need further discussion, but we currently handle dangling
symlinks gracefully, so let's add a test for this, and verify that
we don't replace symlinks with a file.
cli/config: TestLoadDefaultConfigFile: check that no warnings are printed
Loading the config should print no warnings on a successful load.
cli/config: do not discard permission errors when loading config-file
When attempting to load a config-file that exists, but is not accessible for
the current user, we should not discard the error.
This patch makes sure that the error is returned by Load(), but does not yet
change LoadDefaultConfigFile, as this requires a change in signature.
- Description for the changelog
- A picture of a cute animal (not mandatory but encouraged)