Skip to content

Commit d410071

Browse files
committed
Merge branch 'feature/require-env-var-for-commands'
2 parents 77c5609 + 7348e5a commit d410071

File tree

9 files changed

+349
-42
lines changed

9 files changed

+349
-42
lines changed

.github/workflows/test.yml

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# @format
22

3-
on: [push, pull_request]
3+
on: [push]
44

55
jobs:
66
test:
77
strategy:
88
matrix:
99
otp: ["27.1.2"]
1010
elixir: ["1.18.1"]
11-
os: [ubuntu-latest, ubuntu-24.04-arm, windows-latest]
11+
os: [ubuntu-latest, ubuntu-24.04-arm, windows-2022]
1212
# mac is not yet supported by setup-beam
1313
# os: [ ubuntu-latest, ubuntu-24.04-arm, windows-latest, macos-latest, macos-13 ]
1414

@@ -24,15 +24,15 @@ jobs:
2424
- name: Check formatting
2525
run: mix format --check-formatted
2626
# skip on windows because different line endings (LF vs CRLF)
27-
if: matrix.os != 'windows-latest'
27+
if: matrix.os != 'windows-2022'
2828
- run: mix test
2929

3030
test-test_app:
3131
strategy:
3232
matrix:
3333
otp: ["27.1.2"]
3434
elixir: ["1.18.1"]
35-
os: [ubuntu-latest, ubuntu-24.04-arm, windows-latest]
35+
os: [ubuntu-latest, ubuntu-24.04-arm, windows-2022]
3636
# mac is not yet supported by setup-beam
3737
# os: [ ubuntu-latest, ubuntu-24.04-arm, windows-latest, macos-latest, macos-13 ]
3838

@@ -60,9 +60,7 @@ jobs:
6060
choco: age.portable
6161

6262
- name: Generate secrets
63-
run: mix secret_mana.gen.key && mix secret_mana.encrypt secrets.json
64-
env:
65-
MIX_ENV: prod
63+
run: mix secret_mana.gen.key prod && mix secret_mana.encrypt prod secrets.json
6664
working-directory: ./test_app
6765

6866
- run: mix release
@@ -72,7 +70,7 @@ jobs:
7270

7371
- name: Smoke Test app
7472
# skip this on windows, cant figure out to start and curl the server, it simply never responds
75-
if: matrix.os != 'windows-latest'
73+
if: matrix.os != 'windows-2022'
7674
run: |
7775
_build/prod/rel/test_app/bin/test_app start &
7876
curl --connect-timeout 5 --max-time 10 --retry 5 --retry-max-time 40 --retry-connrefused --fail --silent --show-error http://127.0.0.1:4000/api/healthcheck && exit 0

lib/mix/tasks/secret_mana.edit.ex

Lines changed: 89 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,96 @@
11
defmodule Mix.Tasks.SecretMana.Edit do
22
use Mix.Task
33

4+
@shortdoc "Edits SecretMana configuration for the given environment"
5+
6+
@moduledoc """
7+
Opens the SecretMana configuration for editing for a given environment.
8+
9+
## Usage
10+
11+
mix secret_mana.edit <env>
12+
13+
Where `<env>` is typically one of:
14+
15+
* dev
16+
* test
17+
* prod
18+
19+
The task will:
20+
21+
* Ensure the `:secret_mana` application is started.
22+
* Build a `SecretMana.Config` for the given environment.
23+
* Invoke `SecretMana.edit/1` with the config.
24+
25+
## Examples
26+
27+
mix secret_mana.edit dev
28+
mix secret_mana.edit prod
29+
30+
## Getting help
31+
32+
You can display this help with:
33+
34+
mix help secret_mana.edit
35+
36+
Or by using:
37+
38+
mix secret_mana.edit --help
39+
mix secret_mana.edit -h
40+
"""
41+
442
@impl Mix.Task
5-
def run(_) do
6-
Application.ensure_all_started(:secret_mana)
43+
def run(args) do
44+
case parse_args(args) do
45+
{:ok, env} ->
46+
Application.ensure_all_started(:secret_mana)
47+
48+
SecretMana.Config.new(env)
49+
|> SecretMana.edit()
50+
51+
:help ->
52+
print_manual()
53+
54+
{:error, message} ->
55+
Mix.shell().error(message)
56+
Mix.shell().info("")
57+
print_manual()
58+
Mix.raise("secret_mana.edit: invalid arguments")
59+
end
60+
end
61+
62+
defp parse_args(["-h"]), do: :help
63+
defp parse_args(["--help"]), do: :help
64+
65+
defp parse_args([env]) when is_binary(env) and env != "" do
66+
{:ok, env}
67+
end
68+
69+
defp parse_args([]) do
70+
{:error, "Missing required argument: <env>"}
71+
end
72+
73+
defp parse_args(_args) do
74+
{:error, "Too many arguments were provided."}
75+
end
76+
77+
defp print_manual do
78+
Mix.shell().info("""
79+
SecretMana Editor
80+
81+
Usage:
82+
mix secret_mana.edit <env>
83+
84+
Arguments:
85+
<env> The environment whose configuration to edit (e.g. dev, test, prod)
86+
87+
Examples:
88+
mix secret_mana.edit dev
89+
mix secret_mana.edit test
90+
mix secret_mana.edit prod
791
8-
SecretMana.Config.new()
9-
|> SecretMana.edit()
92+
For more information:
93+
mix help secret_mana.edit
94+
""")
1095
end
1196
end
Lines changed: 100 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,109 @@
11
defmodule Mix.Tasks.SecretMana.Encrypt do
22
use Mix.Task
33

4+
@shortdoc "Encrypts a file using SecretMana for the given environment"
5+
6+
@moduledoc """
7+
Encrypts a file using SecretMana for the given environment.
8+
9+
## Usage
10+
11+
mix secret_mana.encrypt <env> <file>
12+
13+
Where:
14+
15+
* `<env>` is typically one of:
16+
* dev
17+
* test
18+
* prod
19+
20+
* `<file>` is the path to the file you want to encrypt.
21+
22+
The task will:
23+
24+
* Ensure the `:secret_mana` application is started.
25+
* Build a `SecretMana.Config` for the given environment.
26+
* Invoke `SecretMana.encrypt/2` with the config and file path.
27+
28+
## Examples
29+
30+
mix secret_mana.encrypt dev config/secrets.yml
31+
mix secret_mana.encrypt prod priv/secrets.txt
32+
33+
## Getting help
34+
35+
You can show this help via:
36+
37+
mix help secret_mana.encrypt
38+
39+
Or by using:
40+
41+
mix secret_mana.encrypt --help
42+
mix secret_mana.encrypt -h
43+
"""
44+
445
@impl Mix.Task
546
def run(args) do
6-
Application.ensure_all_started(:secret_mana)
47+
case parse_args(args) do
48+
{:ok, env, file} ->
49+
Application.ensure_all_started(:secret_mana)
750

8-
args
9-
|> List.first()
10-
|> then(fn file ->
11-
if file do
12-
SecretMana.Config.new()
51+
SecretMana.Config.new(env)
1352
|> SecretMana.encrypt(file)
14-
else
15-
raise """
16-
Usage: mix secret_mana.encrypt <file>
17-
"""
18-
end
19-
end)
53+
54+
:help ->
55+
print_manual()
56+
57+
{:error, message} ->
58+
Mix.shell().error(message)
59+
Mix.shell().info("")
60+
print_manual()
61+
Mix.raise("secret_mana.encrypt: invalid arguments")
62+
end
63+
end
64+
65+
# Argument parsing
66+
67+
defp parse_args(["-h"]), do: :help
68+
defp parse_args(["--help"]), do: :help
69+
70+
defp parse_args([env, file])
71+
when is_binary(env) and env != "" and is_binary(file) and file != "" do
72+
{:ok, env, file}
73+
end
74+
75+
defp parse_args([_env]) do
76+
{:error, "Missing required argument: <file>"}
77+
end
78+
79+
defp parse_args([]) do
80+
{:error, "Missing required arguments: <env> <file>"}
81+
end
82+
83+
defp parse_args(_args) do
84+
{:error, "Too many arguments were provided."}
85+
end
86+
87+
# Manual / help text
88+
89+
defp print_manual do
90+
Mix.shell().info("""
91+
SecretMana Encrypt
92+
93+
Usage:
94+
mix secret_mana.encrypt <env> <file>
95+
96+
Arguments:
97+
<env> The environment to use (e.g. dev, test, prod)
98+
<file> The path to the file to encrypt
99+
100+
Examples:
101+
mix secret_mana.encrypt dev config/secrets.yml
102+
mix secret_mana.encrypt test priv/credentials.txt
103+
mix secret_mana.encrypt prod priv/secrets.env
104+
105+
For more information:
106+
mix help secret_mana.encrypt
107+
""")
20108
end
21109
end

lib/mix/tasks/secret_mana.ex

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
defmodule Mix.Tasks.SecretMana do
2+
use Mix.Task
3+
4+
@shortdoc "Lists available SecretMana Mix tasks"
5+
6+
@moduledoc """
7+
A small index task for the SecretMana Mix tasks.
8+
9+
## Usage
10+
11+
mix secret_mana
12+
13+
This will print a short overview of all available `secret_mana.*` tasks
14+
and how to get more detailed help.
15+
16+
For detailed information on a specific task, use:
17+
18+
mix help secret_mana.install
19+
mix help secret_mana.edit
20+
mix help secret_mana.encrypt
21+
mix help secret_mana.gen.key
22+
"""
23+
24+
@impl Mix.Task
25+
def run(_args) do
26+
Mix.shell().info("""
27+
SecretMana Mix tasks
28+
====================
29+
30+
Configuration / Setup
31+
mix secret_mana.install <env>
32+
Installs SecretMana for the given environment.
33+
34+
mix secret_mana.edit <env>
35+
Opens the SecretMana configuration for editing.
36+
37+
mix secret_mana.gen.key <env>
38+
Generates a SecretMana key for the given environment.
39+
40+
Encryption
41+
mix secret_mana.encrypt <env> <file>
42+
Encrypts a file using SecretMana for the given environment.
43+
44+
Usage hints
45+
• <env> is typically one of: dev, test, prod
46+
• Use --help or -h on any task for a short usage reminder:
47+
mix secret_mana.install --help
48+
mix secret_mana.encrypt -h
49+
""")
50+
end
51+
end

0 commit comments

Comments
 (0)