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

Refactor to use os.UserHomeDir() #2740

Merged
merged 2 commits into from Jan 22, 2021
Merged

Refactor to use os.UserHomeDir() #2740

merged 2 commits into from Jan 22, 2021

Conversation

gleich
Copy link
Contributor

@gleich gleich commented Jan 6, 2021

Part of #2470

Copy link
Contributor

@mislav mislav left a comment

Choose a reason for hiding this comment

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

Thanks, this is a good start!

For the sake of backwards compatibility, however, we should keep the dependency on go-homedir as part of the migration path. In the original issue, I stressed this out:

It's important to note that whenever we add new logic about config file storage, we need to ensure a migration path for users who already have configuration files in the old location.

So, whenever we try to read from a config file, we should check both the new location and the old location, and automatically move the file from old to new location if found. This migratory logic shall stay around until the next major release in the future, when we will remove it.

See #2429 (comment) for more context.

@gleich
Copy link
Contributor Author

gleich commented Jan 7, 2021

Ok so I just made the following changes:

  1. Get the home dir using go-homedir and using os.
  2. If they are not the same then move the old path to the new path. It also makes sure that the config in that old path exists and is a folder.
  3. If they are the same just return the new path.

Copy link
Contributor

@mislav mislav 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 good! If you have time, I've requested some changes that will make the logic safer & clearer.

@@ -30,6 +31,34 @@ func ParseDefaultConfig() (Config, error) {
return ParseConfig(ConfigFile())
}

// Get the absolute path for a folder or file. Support for the old homedir method
// Tied to https://github.com/cli/cli/pull/2740
func GetAbsolutePath(elem ...string) (string, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Could this be named HomeDirPath?

Also, could the name somehow reflect that auto-renaming will happen if the old home location was found? Basically, the idea is that no new callers should ever use this function. It's only to be used in places where we currently relied on mitchellh/go-homedir.

fullOldPath := filepath.Join(append([]string{oldHomeDir}, elem...)...)
fullPath := filepath.Join(append([]string{homeDir}, elem...)...)

if fullOldPath != fullPath {
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we avoid renaming if elem was empty? This is to have a guard against ever renaming the literal user's home directory, which of course we never want to move/rename.

@gleich
Copy link
Contributor Author

gleich commented Jan 15, 2021

Ok so I changed the following:

  1. Added a godoc comment to the function that tells new contributors not to use this function and to instead use os.UserHomeDir().
  2. Checked to make sure elem has a length greater than one before renaming the dir.
  3. Renamed the function to DeprecatedHomeDirPath so it is a little more clear.

Copy link
Contributor

@mislav mislav left a comment

Choose a reason for hiding this comment

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

Thank you! This looks great.

I've pushed an optimization that avoids ever running go-homedir functionality until the fallback path is activated.

@mislav mislav merged commit 7fecaaa into cli:trunk Jan 22, 2021
@gleich gleich deleted the homedir branch January 22, 2021 19:30
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.

None yet

2 participants