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

Feature/xdg base dir 2 #68

Merged
merged 13 commits into from
Jul 28, 2021
Merged

Conversation

teddylear
Copy link
Contributor

@teddylear teddylear commented Jun 20, 2021

Setting the 'PACKER_CONFIG_DIRandPACKER_CONFIG_DIR` to use the default XDG spec for unix systems. Not sure of the best way of testing these changes as they are operating system specific for the paths. Because of this I removed tests for cache directory as they changed depending on the operating system. If you have any advice for how to add tests for these please let me know!

Addresses: hashicorp/packer#9007

@teddylear teddylear force-pushed the feature/xdg-base-dir-2 branch 2 times, most recently from e699b7b to 4ae7de2 Compare June 24, 2021 22:32
@teddylear teddylear closed this Jun 24, 2021
@teddylear teddylear reopened this Jun 24, 2021
@teddylear teddylear marked this pull request as ready for review June 24, 2021 22:59
@teddylear teddylear requested a review from a team as a code owner June 24, 2021 22:59
@teddylear teddylear closed this Jun 24, 2021
@teddylear teddylear reopened this Jun 24, 2021
packer/cache_config_unix.go Outdated Show resolved Hide resolved
@SwampDragons
Copy link
Contributor

Hi, thanks for opening this. I haven't had a chance to test yet, but this appears like it would be backwards-breaking in that new versions of Packer will look in a new place and error if the XDG_CACHE_HOME env var exists, but the config file isn't there.

I think to preserve backwards compatability it would be better to first check for the config in the XDG_CACHE_HOME and then fall back to checking the old path if nothing is discovered in XDG_CACHE_HOME.

@teddylear
Copy link
Contributor Author

@SwampDragons I updated PR to check for XDG_CONFIG_HOME env car and for packer configuration there before defaulting back to old location. I'm assuming for the cache it does not matter the location as it's a cache. Please let me know if there's anything I should change and if there is a way to automate the testing of this.

@ogarcia
Copy link

ogarcia commented Jun 29, 2021

@teddylear bad update, XDG_CONFIG_HOME does not have to be configured or exist as environment variable. The behavior must be:

  1. If XDG_CONFIG_HOME exists as environment variable use it
  2. If not use default: $HOME/.config

I think that you must check if old config dir exists and in this case use it, if not, use XDG.

@teddylear
Copy link
Contributor Author

@SwampDragons I just want to confirm the behavior that we want here. Do we want the behavior listed on the issue that @ogarcia is suggesting with:

  1. If XDG_CONFIG_HOME exists as environment variable use it
  2. If not use default: $HOME/.config

Or the behavior I believe your last comment suggests which is:

  1. Check if XDG_CONFIG_HOME and if there is configuration there then use it
  2. else use the default location we have already today.

I'm assuming for the cache we do not mind where this is placed as it's a cache. Please let me know how to proceed.

@ogarcia
Copy link

ogarcia commented Jun 30, 2021

@teddylear you forgot one step:

  1. Check if default location that we have today exists, if yes, use it (backwards compatibility).
  2. Else If XDG_CONFIG_HOME exists as environment variable use it
  3. Else use default: $HOME/.config

With this implementation, the new users have XDG, and old ones can delete default location or move current config to XDG to have it.

@SwampDragons
Copy link
Contributor

I think either mine as you restated it, or ogarcia's work. I'd originally skipped the "check home, then xdg" because I figure if users have a packer config in the xdg-set location it's intentional and should be used, but I can see the benefit of keeping pure backwards compat.

@ogarcia
Copy link

ogarcia commented Jul 1, 2021

@SwampDragons the main reason of perform check before is that all Linux/MacOS/Derivatives users are using XDG because is a standard (even if the XDG_CONFIG_HOME environment variable is not explicity declared).

In the other hand, using your approach for a user who was not configuration, if the app need to create something in its config directory will use default dir instead of XDG_CONFIG_HOME because XDG_CONFIG_HOME is empty for these user.

@teddylear
Copy link
Contributor Author

I have updated with @ogarcia suggestion. Please let me know if there is anything to update or change.

@ogarcia
Copy link

ogarcia commented Jul 3, 2021

@teddylear mutch better! But you must fix XDG_CACHE_HOME because now it have the logic:

  1. If XDG_CACHE_HOME exists it will be used
  2. Else, use a default packer_cache directory

And must be:

  1. If XDG_CACHE_HOME exists as environment variable use it
  2. If not use default: $HOME/.cache

Or use same logic that XDG_CONFIG_HOME if you want backwards compatibility, but is a cache and I think that is not needed in this case. 😉

@teddylear
Copy link
Contributor Author

@ogarcia I believe I have updated with the desired logic.

defaultConfigFileDir = filepath.Join(xdgCacheHome, "packer")
} else {
homeDir := os.Getenv("HOME")
defaultConfigFileDir = filepath.Join(homeDir, ".cache")
Copy link

Choose a reason for hiding this comment

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

You miss add packer here.

defaultConfigFileDir = filepath.Join(homeDir, ".cache", "packer")

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated for cache and config.

log.Printf("Old default config directory found: %s", dir)
} else if xdgConfigHome := os.Getenv("XDG_CONFIG_HOME"); xdgConfigHome != "" {
log.Printf("Detected xdg config directory from env var: %s", xdgConfigHome)
dir = xdgConfigHome
Copy link

Choose a reason for hiding this comment

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

Here is miss the packer too

dir = filepath.Join(xdgConfigHome, "packer")

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated

@ogarcia
Copy link

ogarcia commented Jul 9, 2021

@teddylear Great work! IMHO it's ready to merge 😉

@teddylear
Copy link
Contributor Author

@SwampDragons This is ready to review, please let me know if you have any ideas around automating tests for this.

@SwampDragons
Copy link
Contributor

sorry, I'm slammed. I'll try to take a look soon.

//
// CachePath can error in case it cannot find the cwd.
// When the directory is not absolute, CachePath will try to make a
// a cache depending on the operating system.
//
Copy link
Contributor

Choose a reason for hiding this comment

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

👋🏼
Could you please update the examples down here in this list depending on the OS ? That would make it easier for me. 🙂

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated.

Copy link
Contributor

Choose a reason for hiding this comment

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

Super awesome, thanks.

packer/cache.go Outdated Show resolved Hide resolved
"testing"
)

func TestCachePath(t *testing.T) {
Copy link
Contributor

@azr azr Jul 21, 2021

Choose a reason for hiding this comment

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

Why removing the tests ? I think it would be better to update them.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As mentioned above, I'm having issues with creating tests with each path being different depending on the OS. If you have any examples of how to do this I would be glad to update as I tried to update the tests in place but hit a wall making them work for without needing to know the OS.

Copy link
Contributor

@azr azr Jul 27, 2021

Choose a reason for hiding this comment

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

Because the behaviors differ between OS, you will have to create tests that test things under unix and under windows. One way to fix this would be to check using runtime.GOOS == "windows", may be the table tests could be different depending on the os type ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated to make different test files depending on OS similar to the OS specific cache and config files.

Copy link
Contributor

Choose a reason for hiding this comment

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

Super good ! Thanks.

pathing/config_file_unix.go Outdated Show resolved Hide resolved
pathing/config_file_windows.go Outdated Show resolved Hide resolved
packer/cache_config_unix.go Outdated Show resolved Hide resolved
teddylear and others added 3 commits July 22, 2021 13:16
Co-authored-by: Adrien Delorme <azr@users.noreply.github.com>
Co-authored-by: Adrien Delorme <azr@users.noreply.github.com>
comments. Adding comment for how the cache configuration works.
@teddylear
Copy link
Contributor Author

@azr Addressed previous comments. Please let me know if there is anything else to updates.

Comment on lines +25 to +36
// reset env
packerCacheDir := os.Getenv("PACKER_CACHE_DIR")
os.Setenv("PACKER_CACHE_DIR", "")
defer func() {
os.Setenv("PACKER_CACHE_DIR", packerCacheDir)
}()

xdgCacheHomeDir := os.Getenv("XDG_CACHE_HOME")
os.Setenv("XDG_CACHE_HOME", "")
defer func() {
os.Setenv("XDG_CACHE_HOME", xdgCacheHomeDir)
}()
Copy link
Contributor

Choose a reason for hiding this comment

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

Super nice !

Comment on lines +20 to +24
// For Unix:
// PACKER_CACHE_DIR="", XDG_CONFIG_HOME="", Default_config CacheDir() => "$HOME/cache/packer"
// PACKER_CACHE_DIR="", XDG_CONFIG_HOME="", Default_config CacheDir("foo") => "$HOME/cache/packer/foo"
// PACKER_CACHE_DIR="bar", XDG_CONFIG_HOME="", Default_config CacheDir("foo") => "./bar/foo
// PACKER_CACHE_DIR="/home/there", XDG_CONFIG_HOME="", Default_config CacheDir("foo", "bar") => "/home/there/foo/bar
Copy link
Contributor

Choose a reason for hiding this comment

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

I like these new examples !
What will Default_config stand for here ?
Do you think we should add something for XDG_CONFIG_HOME too ?

Suggested change
// For Unix:
// PACKER_CACHE_DIR="", XDG_CONFIG_HOME="", Default_config CacheDir() => "$HOME/cache/packer"
// PACKER_CACHE_DIR="", XDG_CONFIG_HOME="", Default_config CacheDir("foo") => "$HOME/cache/packer/foo"
// PACKER_CACHE_DIR="bar", XDG_CONFIG_HOME="", Default_config CacheDir("foo") => "./bar/foo
// PACKER_CACHE_DIR="/home/there", XDG_CONFIG_HOME="", Default_config CacheDir("foo", "bar") => "/home/there/foo/bar
// For Unix:
// PACKER_CACHE_DIR="", XDG_CONFIG_HOME="", Default_config CacheDir() => "$HOME/cache/packer"
// PACKER_CACHE_DIR="", XDG_CONFIG_HOME="", Default_config CacheDir("foo") => "$HOME/cache/packer/foo"
// PACKER_CACHE_DIR="bar", XDG_CONFIG_HOME="", Default_config CacheDir("foo") => "./bar/foo
// PACKER_CACHE_DIR="/home/there", XDG_CONFIG_HOME="", Default_config CacheDir("foo", "bar") => "/home/there/foo/bar
// PACKER_CACHE_DIR="", XDG_CONFIG_HOME="/home/there", Default_config CacheDir("foo", "bar") => "/home/there/foo/bar

@azr
Copy link
Contributor

azr commented Jul 28, 2021

Super awesome, thanks for bearing with us @teddylear, I just want to say that this LGTM in terms of code and I just had a question/suggestion on docs.

@SwampDragons SwampDragons merged commit 4c9e709 into hashicorp:main Jul 28, 2021
@SwampDragons
Copy link
Contributor

Thanks for this!

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.

5 participants