|
| 1 | +defmodule Mix.Tasks.StackLab.Fugu.LiveProviderSmoke do |
| 2 | + @moduledoc """ |
| 3 | + Guarded live-provider smoke wrapper for fugu closeout. |
| 4 | +
|
| 5 | + The command refuses to run unless live behavior is explicitly requested and |
| 6 | + the caller asserts that secrets were loaded through the documented wrapper. |
| 7 | + Provider arguments are passed after `--`. |
| 8 | + """ |
| 9 | + |
| 10 | + use Mix.Task |
| 11 | + |
| 12 | + alias StackLab.FuguLiveProviderGuard |
| 13 | + |
| 14 | + @shortdoc "Run guarded opt-in live provider smoke for fugu" |
| 15 | + |
| 16 | + @impl Mix.Task |
| 17 | + def run(args) do |
| 18 | + Mix.Task.run("app.start") |
| 19 | + |
| 20 | + {guard_args, provider_args} = split_forwarded_args(args) |
| 21 | + |
| 22 | + {opts, _argv, invalid} = |
| 23 | + OptionParser.parse(guard_args, |
| 24 | + strict: [ |
| 25 | + allow_live: :boolean, |
| 26 | + secrets_loaded: :boolean, |
| 27 | + dry_run: :boolean, |
| 28 | + json: :boolean |
| 29 | + ] |
| 30 | + ) |
| 31 | + |
| 32 | + if invalid != [] do |
| 33 | + Mix.raise("invalid options: #{inspect(invalid)}") |
| 34 | + end |
| 35 | + |
| 36 | + guard_opts = [ |
| 37 | + allow_live?: Keyword.get(opts, :allow_live, false), |
| 38 | + secrets_loaded?: Keyword.get(opts, :secrets_loaded, false), |
| 39 | + execution_mode: execution_mode(opts), |
| 40 | + command: forwarded_command(provider_args) |
| 41 | + ] |
| 42 | + |
| 43 | + case FuguLiveProviderGuard.validate(guard_opts) do |
| 44 | + {:ok, receipt} -> |
| 45 | + maybe_run_provider_smoke(opts, provider_args) |
| 46 | + print_success(receipt, Keyword.get(opts, :json, false)) |
| 47 | + |
| 48 | + {:error, reason} -> |
| 49 | + print_failure(reason, Keyword.get(opts, :json, false)) |
| 50 | + end |
| 51 | + end |
| 52 | + |
| 53 | + defp split_forwarded_args(args) do |
| 54 | + {guard_args, rest} = Enum.split_while(args, &(&1 != "--")) |
| 55 | + |
| 56 | + case rest do |
| 57 | + ["--" | provider_args] -> {guard_args, provider_args} |
| 58 | + [] -> {guard_args, []} |
| 59 | + end |
| 60 | + end |
| 61 | + |
| 62 | + defp execution_mode(opts) do |
| 63 | + if Keyword.get(opts, :dry_run, false), do: "dry_run_guard_passed", else: "delegated_live_run" |
| 64 | + end |
| 65 | + |
| 66 | + defp forwarded_command([]), do: "mix stack_lab.provider_smoke_check" |
| 67 | + |
| 68 | + defp forwarded_command(provider_args) do |
| 69 | + Enum.join(["mix", "stack_lab.provider_smoke_check" | provider_args], " ") |
| 70 | + end |
| 71 | + |
| 72 | + defp maybe_run_provider_smoke(opts, provider_args) do |
| 73 | + if Keyword.get(opts, :dry_run, false) do |
| 74 | + :ok |
| 75 | + else |
| 76 | + Mix.Task.reenable("stack_lab.provider_smoke_check") |
| 77 | + Mix.Task.run("stack_lab.provider_smoke_check", provider_args) |
| 78 | + end |
| 79 | + end |
| 80 | + |
| 81 | + defp print_success(receipt, true), do: Mix.shell().info(Jason.encode!(receipt, pretty: true)) |
| 82 | + |
| 83 | + defp print_success(receipt, false) do |
| 84 | + Mix.shell().info("stack_lab.fugu.live_provider_smoke guard passed") |
| 85 | + Mix.shell().info("execution_mode=#{receipt["execution_mode"]}") |
| 86 | + Mix.shell().info("forwarded_command=#{receipt["forwarded_command"]}") |
| 87 | + end |
| 88 | + |
| 89 | + defp print_failure(reason, true) do |
| 90 | + %{status: "fail", error: reason} |
| 91 | + |> Jason.encode!(pretty: true) |
| 92 | + |> Mix.shell().error() |
| 93 | + |
| 94 | + exit({:shutdown, 1}) |
| 95 | + end |
| 96 | + |
| 97 | + defp print_failure(reason, false) do |
| 98 | + Mix.shell().error("stack_lab.fugu.live_provider_smoke rejected") |
| 99 | + Mix.shell().error(" #{inspect(reason)}") |
| 100 | + exit({:shutdown, 1}) |
| 101 | + end |
| 102 | +end |
0 commit comments