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

Support XDG_DATA_HOME #168

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 39 additions & 8 deletions lib/nerves_hub_cli.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,41 @@ defmodule NervesHubCLI do
to_string(hostname)
end

def home_dir do
override_dir =
Application.get_env(:nerves_hub_cli, :home_dir) || System.get_env("NERVES_HUB_HOME")

if override_dir == nil or override_dir == "" do
Path.expand("~/.nerves-hub")
else
override_dir
def home_dir() do
from_config = Application.get_env(:nerves_hub_cli, :home_dir)
from_env = System.get_env("NERVES_HUB_HOME")
xdg_home_nh = :filename.basedir(:user_data, "nerves-hub", %{os: :linux})
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
xdg_home_nh = :filename.basedir(:user_data, "nerves-hub", %{os: :linux})
xdg_home_nh = :filename.basedir(:user_data, "nerves-hub")

It seems like we should trust whatever basedir tells us that we should use for storing data so that this does the right thing on OSX. I "think" that renaming the variables to remove the XDG and updating the error text to refer to this as the user data directory below is what's needed to make this generic.

I.e., the same logic can still be used:

  1. Use Application.get_env(:nerves_hub_cli, :home_dir)
  2. Use $NERVES_HUB_HOME
  3. If ~/.nerves-hub and not the :user_data location, tell the user to move the directory
  4. Use the :user_data location


cond do
valid_home_dir(from_config) ->
Path.expand(from_config)

valid_home_dir(from_env) ->
Path.expand(from_env)

System.get_env("XDG_DATA_HOME") ->
# User set XDG_DATA_HOME so let it pass through
xdg_home_nh

File.dir?(Path.expand("~/.nerves-hub")) and not File.dir?(xdg_home_nh) ->
# By this point, defaults are going to be used.
# If the old default exists, but the new XDG default does not, then fail to
# give the user a chance for easier migration
Shell.error("""
NervesHubCLI has migrated to use the XDG Base Directory Specifiction and
no longer uses the default base directory of ~/.nerves-hub.

Unfortunately, this requires a one-time manual migration since you currently
have configuration stored in ~/.nerves-hub. To continue, please run ¬

$ mv ~/.nerves-hub #{xdg_home_nh}
""")

:erlang.halt(1)

true ->
# Use default $XDG_DATA_HOME/nerves-hub
xdg_home_nh
end
jjcarstens marked this conversation as resolved.
Show resolved Hide resolved
end

Expand Down Expand Up @@ -57,4 +84,8 @@ defmodule NervesHubCLI do
value -> value
end
end

defp valid_home_dir(dir) do
is_binary(dir) and dir != ""
end
end