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

feat: Introduce log and refactor logic of cmd #18

Merged
merged 14 commits into from
Mar 8, 2024

Conversation

pimielowski
Copy link
Contributor

@pimielowski pimielowski commented Feb 28, 2024

Ultimately we agreed to not splitting the logic for the mktp and mksdk to two different files, rather to refactor the base code and introduce the log instead of io.writer.

Description

Motivation and Context

How Has This Been Tested?

Screenshots (if appropriate)

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist

  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes if appropriate.
  • All new and existing tests passed.

Copy link
Collaborator

@shinmog shinmog left a comment

Choose a reason for hiding this comment

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

Generally speaking, as long as there is a command that does both "create the sdk and then create the terraform provider" then I am neutral on splitting things into separate files. I think it makes sense that if there will be separate commands than the command that does both is not called "mktp," so whatever name you want to use is fine. Please put the command in the "cmd" directory with the other commands.

The reason that I don't use "log" to output log messages right now is because of unit testing. Catching and testing the content of log messages that are output using "log" is harder than checking what's written to os.Stderr / os.Stdout. In a testing scenario, you can import "os" and then replace what's saved in os.Stderr / os.Stdout with string buffers, which will allow you to validate log messages written out are as expected.

Copy link
Contributor

@sebastianczech sebastianczech left a comment

Choose a reason for hiding this comment

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

I merged #17 into main branch, so please resolve conflicts.

Regarding splitting command at this stage of the project - I'm not convinced. Maybe later, but for now we have much more other stuff to implement.

Regarding log messages - I prefer to use log package, because the logger writes to standard error and prints the date and time of each logged message. It has also a nice feature for new lines - if the message being printed does not end in a newline, the logger will add one.

When we take final decision about splitting and logs, I will approve that PR.

pimielowski and others added 3 commits March 4, 2024 13:56
…ogic

# Conflicts:
#	cmd/mktp/main.go
#	go.mod
#	pkg/mktp/cmd.go
#	pkg/naming/names.go
pkg/commands/codegen/codegen_test.go Outdated Show resolved Hide resolved
pkg/commands/codegen/codegen_test.go Outdated Show resolved Hide resolved
pkg/commands/codegen/codegen_test.go Outdated Show resolved Hide resolved
pkg/generate/generator_test.go Show resolved Hide resolved
pkg/generate/generator_test.go Show resolved Hide resolved
@pimielowski
Copy link
Contributor Author

Generally speaking, as long as there is a command that does both "create the sdk and then create the terraform provider" then I am neutral on splitting things into separate files. I think it makes sense that if there will be separate commands than the command that does both is not called "mktp," so whatever name you want to use is fine. Please put the command in the "cmd" directory with the other commands.

The reason that I don't use "log" to output log messages right now is because of unit testing. Catching and testing the content of log messages that are output using "log" is harder than checking what's written to os.Stderr / os.Stdout. In a testing scenario, you can import "os" and then replace what's saved in os.Stderr / os.Stdout with string buffers, which will allow you to validate log messages written out are as expected.

I redo some part of the code, and create one file in cmd/main.go to handle the invocation of the script, also I change a little bit the logic for executing the codegen for sdk and terraform, but ultimately I stick with one code file.

About the test, right now we assume if error exist it is bad :) joke aside, right now we don't have the test related to different error messages. There are couple nice libraries also introducing more capability for testing from go-kit library, like level etc, which will be nice to have, but still this is another dependencies.

@pimielowski
Copy link
Contributor Author

I merged #17 into main branch, so please resolve conflicts.

Regarding splitting command at this stage of the project - I'm not convinced. Maybe later, but for now we have much more other stuff to implement.

Regarding log messages - I prefer to use log package, because the logger writes to standard error and prints the date and time of each logged message. It has also a nice feature for new lines - if the message being printed does not end in a newline, the logger will add one.

When we take final decision about splitting and logs, I will approve that PR.

Thanks, I merged the changes :)
I'm agreed, after some testing it doesn't have any purposes right now, so I stick to the one file.
Totally agreed about log!

cmd/main.go Outdated
Copy link
Collaborator

Choose a reason for hiding this comment

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

I have a strong preference for the ./cmd directory to only have other directories, not go code. You mentioned that you reviewed a few other big projects in our last meeting tho, did some of them do this, and you're modeling this after what you found in your investigations?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, ie. Prometheus are constructed that way -> https://github.com/prometheus/prometheus/blob/main/cmd/prometheus/main.go

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I change it a little bit, I added directory there

import (
"context"
"fmt"
"log"
Copy link
Collaborator

Choose a reason for hiding this comment

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

I have a preference to not use log, but instead have the commands take a stdin / stderr / stdout, and then use fmt.Fprintf() to output messages there, as this setup provides easier access to the log messages output for unit tests / integration tests. If you look at the scm generator, although I don't have any unit tests there, this is modeled in that generator.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But testing is based on the function working correctly and not on function logs. We are expecting that function behave and gives us wanted output :)

Copy link
Contributor

@sebastianczech sebastianczech Mar 8, 2024

Choose a reason for hiding this comment

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

In my opinion we can go wit log approach and in tests focus on function outputs, not logs generated by function.

If in future that decision will not work for us, we can always change it :)

@pimielowski pimielowski requested a review from shinmog March 8, 2024 10:03
Copy link
Contributor

@sebastianczech sebastianczech left a comment

Choose a reason for hiding this comment

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

👍

@pimielowski pimielowski changed the title Split creation of terraform provider and provider sdk + introduce log… feat: Introduce log and refactor logic of cmd Mar 8, 2024
@pimielowski pimielowski merged commit ec26a4a into main Mar 8, 2024
1 check passed
@pimielowski pimielowski deleted the feature-add-codegen-logic branch March 8, 2024 13:17
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.

3 participants