Skip to content

Commit

Permalink
Merge pull request #3875 from nulib/errant-dash-prefix
Browse files Browse the repository at this point in the history
Don't start index pipeline name with "-" in release
  • Loading branch information
mbklein authored Mar 11, 2024
2 parents 1ec5cab + 26423aa commit 171499d
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 4 deletions.
15 changes: 11 additions & 4 deletions app/lib/env.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@ if !function_exported?(:Env, :prefix, 0) do
alias Hush.Provider.{AwsSecretsManager, SystemEnvironment}

def prefix do
with env <- if(function_exported?(Mix, :env, 0), do: Mix.env(), else: nil) do
[System.get_env("DEV_PREFIX"), env] |> Enum.reject(&is_nil/1) |> Enum.join("-")
end
env =
cond do
System.get_env("RELEASE_NAME") -> nil
function_exported?(Mix, :env, 0) -> Mix.env()
true -> nil
end

[System.get_env("DEV_PREFIX"), env] |> Enum.reject(&is_nil/1) |> Enum.join("-")
end

def prefix(val), do: [prefix(), to_string(val)] |> Enum.reject(&is_nil/1) |> Enum.join("-")
def prefix(val), do: [prefix(), to_string(val)] |> reject_empty() |> Enum.join("-")
def atom_prefix(val), do: prefix(val) |> String.to_atom()

def aws_secret(name, opts \\ []),
Expand All @@ -23,5 +28,7 @@ if !function_exported?(:Env, :prefix, 0) do
defp hush_secret(provider, name, opts), do: {:hush, provider, name, opts}

defp secrets_path, do: System.get_env("SECRETS_PATH", "config")

defp reject_empty(list), do: Enum.reject(list, &(is_nil(&1) or &1 == ""))
end
end
67 changes: 67 additions & 0 deletions app/test/env_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
defmodule EnvTest do
use ExUnit.Case

import Env

setup %{environment: environment} do
set_env = fn
{key, nil} -> System.delete_env(key)
{key, value} -> System.put_env(key, value)
end

saved = Enum.map(environment, fn {key, _} -> {key, System.get_env(key)} end)
Enum.each(environment, set_env)
on_exit(fn -> Enum.each(saved, set_env) end)
end

describe "hush" do
@describetag environment: [{"SECRETS_PATH", "foo/config"}]

test "aws_secret/2" do
assert aws_secret("meadow", test: :value) ==
{:hush, Hush.Provider.AwsSecretsManager, "foo/config/meadow", [test: :value]}
end

test "meadow_secret/2" do
assert meadow_secret(test: :value) ==
{:hush, Hush.Provider.AwsSecretsManager, "foo/config/meadow", [test: :value]}
end

test "environment_secret/2" do
assert environment_secret("DEV_PREFIX", test: :value) ==
{:hush, Hush.Provider.SystemEnvironment, "DEV_PREFIX", [test: :value]}
end
end

describe "dev environment" do
@describetag environment: [{"DEV_PREFIX", "env"}]

test "prefix/0" do
assert prefix() == "env-test"
end

test "prefix/1" do
assert prefix("database") == "env-test-database"
end

test "atom_prefix/1" do
assert atom_prefix("database") == :"env-test-database"
end
end

describe "release environment" do
@describetag environment: [{"DEV_PREFIX", nil}, {"RELEASE_NAME", "meadow"}]

test "prefix/0" do
assert prefix() == ""
end

test "prefix/1" do
assert prefix("database") == "database"
end

test "atom_prefix/1" do
assert atom_prefix("database") == :database
end
end
end

0 comments on commit 171499d

Please sign in to comment.