Skip to content

Commit

Permalink
Support XDG_DATA_HOME
Browse files Browse the repository at this point in the history
Resolves #120
  • Loading branch information
jjcarstens committed Dec 22, 2020
1 parent e9bc12d commit cbe4515
Showing 1 changed file with 39 additions and 8 deletions.
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})

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
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

0 comments on commit cbe4515

Please sign in to comment.