Skip to content
This repository has been archived by the owner on Jun 6, 2024. It is now read-only.

feat: issue#647 export log to file #938

Merged
merged 9 commits into from
May 28, 2023

Conversation

alonzyl
Copy link
Contributor

@alonzyl alonzyl commented May 15, 2023

I would appreciate your feedback on whether there are any areas that I could enhance this cr. Any suggestions for improvement would be greatly appreciated.

while writing the code i came across a few questions about the flow, I would be happy to change anything if you think it would be better.

  • on validation should i try to write in the directory to check if there are righting rights?
  • should all the test fail if the directory is not valid?
  • maybe not limit the file saving only to json, allow all supported output types.

@hadar-co hadar-co linked an issue May 15, 2023 that may be closed by this pull request
@hadar-co
Copy link
Contributor

Hey @alonzyl :)

on validation should i try to write in the directory to check if there are righting rights?
should all the test fail if the directory is not valid?

Regarding these 2 questions, you should check if the directory exists and is writable. See here for some approaches to achieve this. If the dir does not exist or is not writable the export should be skipped without any other side-effects.

maybe not limit the file saving only to json, allow all supported output types.
I think JSON is enough, it is flexible enough to cover most use-cases, if we see a need for more output types we can add them in the future:)

Thanks!

@alonzyl
Copy link
Contributor Author

alonzyl commented May 22, 2023

Hi, I updated the code with the comments above. Would like to hear you opinion :)

@adifayer
Copy link
Contributor

@alonzyl Thank you for submitting this PR! 🚀 We will review it in the upcoming days :)

cmd/test/main.go Outdated
err = evaluation.PrintResults(evaluationData)

if testCommandData.SaveResults != "" {
if evaluation.IsWritableDirectory(testCommandData.SaveResults) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This function looks a little redundant to me, I mean why not just try to writeFile and if an error is returned then just return the error?

The error returned from this should look like this, which is understandable enough, WDYT?
@alonzyl
image

cmd/test/main.go Outdated

err = ioutil.WriteFile(testCommandData.SaveResults, []byte(resultsText), 0666)
if err != nil {
fmt.Println(err)
Copy link
Contributor

Choose a reason for hiding this comment

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

This print seems a bit redundant as well, since the error returned will be printed out

"golang.org/x/sys/unix"
)

func IsWritableDirectory(filePath string) bool {
Copy link
Contributor

Choose a reason for hiding this comment

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

If you indeed don't use this function, This whole file can be deleted as well, less code makes it simpler

func test_testCommand_save_results_flag(t *testing.T, ctx *TestCommandContext) {
flags := TestCommandFlags{SaveResults: "./"}
err := flags.Validate()
assert.NoError(t, err)
Copy link
Contributor

Choose a reason for hiding this comment

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

Optional - You could add a test case for when there is a wrong path and an error is thrown

@shalev007 shalev007 changed the title Issue#647 export log to file feat: Issue#647 export log to file May 24, 2023
@shalev007 shalev007 changed the title feat: Issue#647 export log to file feat: issue#647 export log to file May 24, 2023
@shalev007
Copy link
Contributor

Hey @alonzyl thanks for the contribution! 🙏
looks simple and good however I think there's some extra code there, take a look at the comments

@alonzyl
Copy link
Contributor Author

alonzyl commented May 24, 2023

Hi, I totally agree with your remarks and have made the necessary changes accordingly. If there are more things that I can improve, I would be happy to make further changes :)

@shalev007 shalev007 merged commit abcea62 into datreeio:main May 28, 2023
@shalev007
Copy link
Contributor

@alonzyl Thanks you so much for the changes, it looks great.
merged 🥳

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

Successfully merging this pull request may close these issues.

Write policy check results to local log file
4 participants