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

Prompt for country on first run and save to config file #30

Merged
merged 3 commits into from
Jul 7, 2022
Merged

Conversation

rossf7
Copy link
Contributor

@rossf7 rossf7 commented Jul 5, 2022

Towards #16

On first run we now check if the config file ~/.config/grid-intensity/config.yaml exists.

If not the user is prompted to enter a country code. We still show the country from the user's locale as a hint.

go run .
Provider ember-climate.org needs an ISO country code as a region parameter.
ESP detected from your locale.
FRA
{
	"country_code_iso_2": "FR",
	"country_code_iso_3": "FRA",
	"country_or_region": "France",
	"year": 2021,
	"latest_year": 2021,
	"emissions_intensity_gco2_per_kwh": 67.781
}

Running a 2nd time the saved setting is used.

go run .
{
	"country_code_iso_2": "FR",
	"country_code_iso_3": "FRA",
	"country_or_region": "France",
	"year": 2021,
	"latest_year": 2021,
	"emissions_intensity_gco2_per_kwh": 67.781
}

Specifying a country code will save the new value.

go run . -c DE
{
	"country_code_iso_2": "DE",
	"country_code_iso_3": "DEU",
	"country_or_region": "Germany",
	"year": 2021,
	"latest_year": 2021,
	"emissions_intensity_gco2_per_kwh": 363.982
}

}

err = getGridIntensityForCountry(country)
err := runWithError()
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 extracted the logic to this function which simplifies the error handling. So we only need a single log.Fatal call.

@@ -50,30 +51,92 @@ func Execute() {

func init() {
rootCmd.Flags().StringP(countryCode, "c", "", "Country code in ISO 3 character format")
viper.BindPFlag(countryCode, rootCmd.Flags().Lookup(countryCode))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is how we add viper for managing the config by binding it to the cobra CLI flag.

https://github.com/spf13/viper#working-with-flags

if countryCode == "" {
// No country code provided so try to detect it from the user's locale.
tag, err := locale.Detect()
result, err := ember.GetGridIntensityForCountry(countryCode)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry the diff is a bit hard to follow here. All the logic for getting the country code is moved to the getCountryCode func.

@rossf7 rossf7 marked this pull request as ready for review July 5, 2022 12:02
@rossf7 rossf7 requested a review from mrchrisadams July 5, 2022 12:02
Copy link
Member

@mrchrisadams mrchrisadams left a comment

Choose a reason for hiding this comment

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

I'm happy for this to be merged in @rossf7.

There's only one query I have here when we prompt users for a country code.

However, I'm ok with us splitting that out as a separate issue, and for us to it to talk about the first time experience across all the providers we currently use.

viper.Set(countryCode, country)
}

err = getGridIntensityForCountry(country)
Copy link
Member

Choose a reason for hiding this comment

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

Do you think it's worth making explicit that the country code is being used to make the request/lookup against our set of country / carbon intensity codes?

I know the returned JSON blob mentions that the country and country code, but for the first time, when I looked over it, I felt like having a short one line outlining what was about to happen before showing the returned value would make it easier to understand what was happening.

This probably isn't needed on subsequent uses, but I think it provides a more gentle way into this domain.

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, I agree I think we should explain more what is going on.

I've been thinking we could add an --output flag that defaults to text but also supports JSON.
The text output could walk the user through choosing a provider and region. As well as linking to our developer site for more docs. WDYT?

Copy link
Member

Choose a reason for hiding this comment

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

yes. would you mind creating an issue for this? I think this would be a good one to include Fershad for this, so he can create some tutorials and add some reference to it in the developers.tgwf site

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@mrchrisadams I created #32 for this. I agree it would be good to work with Fershad on this and to have some tutorials.

@rossf7
Copy link
Contributor Author

rossf7 commented Jul 7, 2022

Thanks @mrchrisadams I'm going to merge this. I'll create a new issue for the first time experience.

@rossf7 rossf7 merged commit b7cc6da into main Jul 7, 2022
@rossf7 rossf7 deleted the config-file branch July 7, 2022 10:51
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.

2 participants