#!/usr/bin/env -S ERL_FLAGS=+B elixir Mix.install([{:nimble_totp, ">= 1.0.0"},{:yaml_elixir, "~> 2.9"}]) if System.get_env("DEPS_ONLY") == "true" do System.halt(0) Process.sleep(:infinity) end defmodule TOTP do @moduledoc """ ## Usage $ totp-code get-code identifier $ totp-code list-identifiers $ totp-code set-secret identifier secret # men at work """ def main([]), do: cmd("help", []) def main(argv) do [command | args] = argv cmd(command, args) end defp cmd("get-code", [identifier]) do get_secret(identifier) |> Base.decode32!() |> NimbleTOTP.verification_code() |> IO.write() end defp cmd("list-identifiers", _) do read_config() |> Map.get("secrets") |> Map.keys() |> Enum.join("\n") |> IO.puts() end defp cmd("help", _), do: IO.puts(@moduledoc) defp read_config() do System.user_home() |> Path.join(".totp-code.conf") |> YamlElixir.read_from_file!() end defp get_secret(identifier) do read_config() |> Map.get("secrets") |> Map.get(identifier) end end TOTP.main(System.argv())