1.3 KiB · text History f960521
defmodule Mix.Tasks.Vigil.Token do
  @shortdoc "Create an API token for a user"
  @moduledoc """
  Create a personal API token for an existing user and print it. The raw
  token is shown this once; only its hash is kept.

      mix vigil.token sam@example.com "sentry-cli" project:releases,org:read

  Scopes are comma-separated, from: org:read org:write org:admin
  org:integrations member:read member:write member:admin team:read
  team:write team:admin project:read project:write project:admin
  project:releases event:read event:write event:admin. In each
  organization the token is still narrowed to the user's role there.

  In a release, the same thing is `bin/token` (`Vigil.Release.token/0`).
  """

  use Mix.Task

  @requirements ["app.start"]

  @impl Mix.Task
  def run(args) do
    case OptionParser.parse(args, strict: []) do
      {_, [email, label, scopes], []} ->
        case Vigil.Release.create_token(email, label, scopes) do
          {:ok, token} ->
            Mix.shell().info(token.token)
            Mix.shell().info(Vigil.Release.describe_token(token, email))

          {:error, reason} ->
            Mix.shell().error(Vigil.Release.token_error(reason, email))
            exit({:shutdown, 1})
        end

      _ ->
        Mix.shell().error(~s(usage: mix vigil.token EMAIL "LABEL" SCOPE[,SCOPE...]))
        exit({:shutdown, 1})
    end
  end
end