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

feat: SetupDaemonConfig no longer needs a file #214

Merged
merged 1 commit into from
Feb 19, 2024

Conversation

miparnisari
Copy link
Contributor

@miparnisari miparnisari commented Jan 22, 2024

Currently, SetupDaemonConfig needs a concrete instance of logrus and a file. This PR removes the need of a file. It's bad practice to accept a filename as parameter: https://100go.co/?h=file#using-a-filename-as-a-function-input-46. If consumers want, they can pass a string.

Accepting a filename as a function input to read from a file should, in most cases, be considered a code smell (except in specific functions such as os.Open). Indeed, it makes unit tests more complex because we may have to create multiple files. It also reduces the reusability of a function (although not all functions are meant to be reused). Using the io.Reader interface abstracts the data source. Regardless of whether the input is a file, a string, an HTTP request, or a gRPC request, the implementation can be reused and easily tested.

@miparnisari miparnisari requested a review from Baliedge as a code owner January 22, 2024 03:25
func SetupDaemonConfig(logger *logrus.Logger, configFile string) (DaemonConfig, error) {
// SetupDaemonConfig returns a DaemonConfig object that is the result of merging the lines
// in the provided configFile and the environment variables. See `example.conf` for all available config options and their descriptions.
func SetupDaemonConfig(logger *logrus.Logger, configFile io.Reader) (DaemonConfig, error) {
Copy link
Contributor Author

@miparnisari miparnisari Jan 22, 2024

Choose a reason for hiding this comment

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

This function really really shouldn't accept a concrete instance of a logrus.Logger. This should be an interface and, if consumers want, they can pass their own implementation (or a NoOpLogger)

Something else I noticed: it is really really easy for consumers of this project to simply do gubernator.SpawnDaemon(ctx, DaemonConfig{GRPCListenAddress: "localhost:8111"}) - this will set all other configs to their zero values! 😱

Copy link
Contributor

Choose a reason for hiding this comment

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

That sounds like a great idea! Thank you!

Copy link
Contributor

Choose a reason for hiding this comment

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

Agreed on the Logrus concrete type thing. Would be a nice addition to Gubernator v3 to ditch Logrus once and for all.

Copy link
Contributor

@thrawn01 thrawn01 left a comment

Choose a reason for hiding this comment

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

This looks great! Lets get this merged!

func SetupDaemonConfig(logger *logrus.Logger, configFile string) (DaemonConfig, error) {
// SetupDaemonConfig returns a DaemonConfig object that is the result of merging the lines
// in the provided configFile and the environment variables. See `example.conf` for all available config options and their descriptions.
func SetupDaemonConfig(logger *logrus.Logger, configFile io.Reader) (DaemonConfig, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

That sounds like a great idea! Thank you!

@Baliedge Baliedge merged commit 452c5b5 into mailgun:master Feb 19, 2024
4 checks passed
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.

3 participants