-
Notifications
You must be signed in to change notification settings - Fork 62
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
feat: add --output-file flag (#145) #150
base: main
Are you sure you want to change the base?
Conversation
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: ipsum-0320 The full list of commands accepted by this bot can be found here.
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
Welcome @ipsum-0320! |
Hi @ipsum-0320. Thanks for your PR. I'm waiting for a kubernetes-sigs member to verify that this patch is reasonable to test. If it is, they should reply with Once the patch is verified, the new status will be reflected by the I understand the commands that are listed here. Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. |
610ff69
to
1a676d5
Compare
/ok-to-test |
1a676d5
to
33c2a26
Compare
@LiorLieberman @mlavacca Could you please review my code? The other two reviewers didn't seem to have time. I have a lot of time to contribute to the project, please feel free to give me your insights on my code 😊 |
sorry for not getting to this one yet @ipsum-0320, I will get to it within the next two days |
@levikobi ok, thanks! |
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 like this gets the job done
Thanks @ipsum-0320
/lgtm
/cc @LiorLieberman @mlavacca
cmd/print.go
Outdated
@@ -235,6 +281,9 @@ func newPrintCommand() *cobra.Command { | |||
cmd.Flags().StringVarP(&pr.outputFormat, "output", "o", "yaml", | |||
fmt.Sprintf(`Output format. One of: (%s)`, strings.Join(allowedFormats, ", "))) | |||
|
|||
cmd.Flags().StringVarP(&pr.outputFile, "output-file", "", "", | |||
"Path to the conversion result file, the tool will output the conversion result to the file. Value pattern should be *.yaml of *.json (default \"./ingress2gateway.yaml or ./ingress2gateway.json\")") |
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.
"Path to the conversion result file, the tool will output the conversion result to the file. Value pattern should be *.yaml of *.json (default \"./ingress2gateway.yaml or ./ingress2gateway.json\")") | |
"Path to the conversion result file, the tool will output the conversion result to the file. Value pattern should be *.yaml of *.json (default \"./ingress2gateway.yaml or ./ingress2gateway.json\")") |
Thanks @ipsum-0320. /hold |
hi, @LiorLieberman . As mentioned in issue #145, if we output any other log messages to the console, such messages will also be dumped into the output file, thus contaminating the .yaml/.json file. |
That I understand. I am saying why we default to a file? Why we dont keep the default to the console ? |
Oh, @LiorLieberman I think if we force it to output to a file, we can completely avoid contaminating the conversion results with log messages. Of course, outputting to the console is more convenient... I'll make it optional. |
New changes are detected. LGTM label has been removed. |
501c969
to
5fc17e8
Compare
@levikobi @LiorLieberman hi guys, PTAL 😊 |
.gitignore
Outdated
|
||
# IDE file | ||
.idea/ | ||
.DS_Store |
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'd prefer not to have IDE-based fields in the .gitignore. You can add these entries in your global .gitignore
to avoid committing these folders in all your local projects.
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.
okok, got it.
cmd/print.go
Outdated
if !strings.HasSuffix(pr.outputFile, ".json") && !strings.HasSuffix(pr.outputFile, ".yaml") { | ||
return fmt.Errorf("output file must have be *.json or *.yaml") | ||
} | ||
if strings.HasSuffix(pr.outputFile, ".json") && pr.outputFormat != "json" { | ||
return fmt.Errorf("output format is yaml, but output file is *.json") | ||
} | ||
if strings.HasSuffix(pr.outputFile, ".yaml") && pr.outputFormat != "yaml" { | ||
return fmt.Errorf("output format is json, but output file is *.yaml") | ||
} |
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 don't think we need such an enforcement/validation. I'd give users the possibility to choose the filename they prefer, even without the correct extension.
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 just want to prevent confusion, maybe I can change it to output a warning instead of an error.
cmd/print.go
Outdated
err := file.Close() | ||
if err != nil { | ||
fmt.Println("Error closing file:", err) | ||
return |
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.
return |
This return
is not needed 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.
Yes, of course, got it.
cmd/print.go
Outdated
if resourceCount == 0 { | ||
err := os.Remove(pr.outputFile) | ||
if err != nil { | ||
fmt.Printf("Error removing file %s: %v\n", pr.outputFile, err) | ||
} | ||
} |
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.
Creating a file upfront and deleting it if no resources are displayed is a bit dirty in my opinion. I'd slightly refactor the outputResult
function to extract the count of objects into a dedicated function. Before calling outputResult
we can check if there are resources. If not, we do not need to create any file and we can just move on.
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.
ok, got it.
5fc17e8
to
d42332c
Compare
d42332c
to
580b0fe
Compare
@mlavacca PTAL 😊 |
PR needs rebase. Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. |
@@ -25,4 +25,4 @@ go.work | |||
/tmp | |||
|
|||
# GolangCI-Lint cache | |||
/cache | |||
/cache |
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.
/cache | |
/cache | |
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 is something weird with this table, it looks like it has been entirely reformatted.
if pr.outputFile == "" { | ||
return | ||
} |
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 need to validate that outputFile is not "" when the flag is enabled upfront. If the user provides such a flag without a value, we just exit with an error
if !strings.HasSuffix(pr.outputFile, ".json") && !strings.HasSuffix(pr.outputFile, ".yaml") { | ||
fmt.Println("Warning: output file should be *.json or *.yaml") | ||
} else if strings.HasSuffix(pr.outputFile, ".json") && pr.outputFormat != "json" { | ||
fmt.Println("Warning: output format is yaml, but output file is *.json") | ||
} else if strings.HasSuffix(pr.outputFile, ".yaml") && pr.outputFormat != "yaml" { | ||
fmt.Printf("Warning: output format is json, but output file is *.yaml") | ||
} | ||
return | ||
} |
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 do not think we need this logic at all, we should use the file provided by the user regardless the extension in my opinion, without logging any specific message
@@ -235,6 +286,9 @@ func newPrintCommand() *cobra.Command { | |||
cmd.Flags().StringVarP(&pr.outputFormat, "output", "o", "yaml", | |||
fmt.Sprintf(`Output format. One of: (%s)`, strings.Join(allowedFormats, ", "))) | |||
|
|||
cmd.Flags().StringVarP(&pr.outputFile, "output-file", "", "", | |||
"Path to the conversion result file, the tool will output the conversion result to the file. Value pattern should be *.yaml of *.json. If not set, the tool will output to stdout") |
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 not enforce any specific validation on the file extension. Users should be able to use whatever file they prefer
msg = fmt.Sprintf("%s in %s namespace", msg, pr.namespaceFilter) | ||
} | ||
fmt.Println(msg) | ||
func (pr *PrintRunner) output2File(gatewayResources []i2gw.GatewayResources) { |
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.
func (pr *PrintRunner) output2File(gatewayResources []i2gw.GatewayResources) { | |
func (pr *PrintRunner) outputToFile(gatewayResources []i2gw.GatewayResources) { |
} | ||
} else { | ||
if pr.checkResourceCount(gatewayResources) { | ||
pr.output2File(gatewayResources) |
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.
pr.output2File(gatewayResources) | |
pr.outputToFile(gatewayResources) |
👋 @ipsum-0320, how are things going? If you need any help in addressing the comments and move this one forward, please let us know |
The Kubernetes project currently lacks enough contributors to adequately respond to all PRs. This bot triages PRs according to the following rules:
You can:
Please send feedback to sig-contributor-experience at kubernetes/community. /lifecycle stale |
The Kubernetes project currently lacks enough active contributors to adequately respond to all PRs. This bot triages PRs according to the following rules:
You can:
Please send feedback to sig-contributor-experience at kubernetes/community. /lifecycle rotten |
What type of PR is this?
/kind feature
What this PR does / why we need it:
To separate the output and logs, we have introduced a new
--output-file
flag to allow users to dump the converted resources into a specific file, while the console only outputs log messages/warnings. The--output-file
has the following requirements:--output-file
is "./ingress2gateway.yaml" or "./ingress2gateway.json", depending on whether theoutput
flag is set to "yaml" or "json".Which issue(s) this PR fixes:
Fixes #145
Does this PR introduce a user-facing change?: