voughtdq

voughtdq

I’m trying to create a command line utility for creating administrative users in a back office application.

defmodule Admin.CLI do

  def main(args) do
    handle_command(args)
  end

  def handle_command(["createadmin" | rest]) do
    {opts, _, _} = OptionParser.parse(rest, switches: [username: :string, password: :string],
      aliases: [u: :username, p: :password])

    username = Keyword.get(opts, :username) || raise "You must supply a username"
    password = Keyword.get(opts, :password) || raise "You must supply a password"

    case Admin.create_user(%{"username" => username, "password" => password}) do
      {:ok, _user} ->
        IO.puts("User #{username} created with password #{password}")
      {:error, _} ->
        IO.puts(:stderr, "Error creating user.")
    end
  end

  def handle_command(_) do
    IO.puts(:stderr, "Invalid command.")
  end
end

But this is the output that I receive when running the command:

$ ./admin createadmin --username test --password test
[warn] The on_load function for module Elixir.Argon2.Base returned:
{:function_clause, [{:filename, :join, [{:error, :bad_name}, 'argon2_nif'], [file: 'filename.erl', line: 446]}, {Argon2.Base, :load_nif, 0, [file: 'lib/argon2/base.ex', line: 115]}, {Argon2.Base, :init, 0, [file: 'lib/argon2/base.ex', ...]}, {:code_server, :"-handle_on_load/5-fun-0-", 1, [...]}]}

[error] Process #PID<0.243.0> raised an exception
** (FunctionClauseError) no function clause matching in :filename.join/2
    (stdlib) filename.erl:446: :filename.join({:error, :bad_name}, 'argon2_nif')
    (argon2_elixir) lib/argon2/base.ex:115: Argon2.Base.load_nif/0
    (argon2_elixir) lib/argon2/base.ex:14: Argon2.Base.init/0
    (kernel) code_server.erl:1340: anonymous fn/1 in :code_server.handle_on_load/5
** (UndefinedFunctionError) function Argon2.Base.hash_password/3 is undefined (module Argon2.Base is not available)
    (argon2_elixir) Argon2.Base.hash_password("test", <<158, 41, 41, 255, 119, 126, 67, 223, 90, 157, 199, 83, 101, 28, 67, 206>>, [])
    (argon2_elixir) lib/argon2.ex:140: Argon2.add_hash/2
    (admin) lib/admin/user.ex:30: Admin.User.maybe_add_hash/1
    (admin) lib/admin.ex:14: Admin.create_user/1
    (admin) lib/admin/cli.ex:14: Admin.CLI.handle_command/1
    (elixir) lib/kernel/cli.ex:121: anonymous fn/3 in Kernel.CLI.exec_fun/2

When running with the same input from iex -S mix, it works as expected:

iex(1)> Admin.CLI.handle_command(["createadmin", "--username", "test", "--password", "test"])
User test created with password test
[debug] QUERY OK db=25.1ms decode=0.8ms queue=0.6ms
INSERT INTO "users" ("password_hash","username") VALUES ($1,$2) RETURNING "id" ["$argon2id$v=19$m=131072,t=8,p=4$dXaxDD2S7LG2sAaxfj8u9w$iJw1bEMSwZCPS3jIFZZ4Fzg/5sGf0hSEh8izr1hlyCY", "test"]

Is my approach wrong here? Should the escript just be sending messages to a handler process that’s already running inside the application?

First 2 of 2 Posts Switch mode

NobbZ

NobbZ

You can not use NIFs in an escript.


edit

Strictly speaking, you can’t use priv_dir in an escript, which most NIFs rely on to ship the shared object which contains the actual NIF.

voughtdq

voughtdq OP

That’s unfortunate. Back to the drawing board!

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
rahultumpala
Hello, I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New

We're in Beta

About us Mission Statement