initial commit

This commit is contained in:
Thelonius Kort
2025-06-22 19:52:59 +02:00
commit 0ebacbec94
2 changed files with 59 additions and 0 deletions

57
totp-code Executable file
View File

@ -0,0 +1,57 @@
#!/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.puts()
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())