belaustegui

belaustegui

I am thinking about building a tool to use from the command line.
The tool would provide a set of commands that interact with a remote API (from a bug tracking service, imagine something like Jira). The objective of the tool is to provide an alternative way to perform simple modifications directly from the terminal, instead of having to open the page in the web browser, etc.

Ideally I want the tool to be distributed as a single binary that can be put in the $PATH and executed.

I’ve seen that Go and Rust (apart from the always beloved C) are adequate for this requirements. But the thing is: I’m in love with Elixir and I would love to write this tool in it. Would Elixir allow me to generate a ready-to-run binary file?

Showing Posts 1 to 10

sotojuan

sotojuan

You’d still need the VM installed in the machine and to start up before the CLI tool can be run so it’s not an “independent” binary like Go, C, or Rust.

brainbag

brainbag

I have written several CLI tools in Elixir now using escript, including the specific one you mentioned (remote API) and I love it enough that I’ve been using it instead of Ruby. I’m not certain about distribution though since I haven’t had any reason to share it.

Set up the project for CLI:

# add to mix.exs in the project function
[app: :myproject, 
...
escript: [main_module: YourProjectName.CLI],
...]

Create the entry point:

defmodule YourProjectName.CLI do
  def main(args) do 
    # - use OptionParser to parse args
    # - use IO.ANSI for color
    # - use System.halt(code) to exit with an error code
    # - use ExUnit.CaptureIO to capture terminal output for testing
    IO.puts "Howdy Earth!"
  end
end

Build and run

# build it
mix escript.build

# install it into your path
mix escript.install
benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

If you use a release though the VM is indeed bundled in.

Onor.io

Onor.io

There’s no way to get a single exe file with Elixir (or indeed Erlang) but you don’t need the entire development environment installed either. Even with Escript (which is as close as I think you’ll come to a standalone binary) you’ll need some files from the Erlang runtime.

To me, it’s 6 of one and half-a-dozen of the other because even with a “standalone” binary you’ll often still need shared objects/dll’s to be installed for things to work. One could, I suppose, build a single statically linked binary but that’s not a panacea either because the exe will be rather large.

oc

sotojuan

sotojuan

Good point, but shipping a whole VM for a small CLI app doesn’t sit right with me.

OvermindDL1

OvermindDL1

I would just use Rust if it was a little project that I could spend some time learning the proper Rust libraries for (still learning the language when I get time… randomly…), otherwise I would just use C++, which has almost too many libraries to handle about anything and I already would know which I would use. ^.^

Really though, it depends on the purpose, like if Python will be on the destinations then a simple Python script would be best, or if Windows then use powershell, etc… etc…

bbense

bbense

So you don’t use any Ruby, Perl or Python?

As far as I know, only the only choice for a completely stand alone binary these days is Go.
( I’ve no idea what Rust’s runtime, need to check that out).

Even boring old C is dependent on shared libraries.

Everything I’ve written in Elixir is a cli more or less. Where Elixir really shines for this kind of stuff is when you have a 24 core server and enough work to keep those cores busy. That’s not every cli app, but if you have that scale of problem, it’s so much easier to deal with in Elixir.

While the default erlang runtime is 200meg, there is a lot that can be trimmed; escripts only require the erlang runtime. While maybe not comparable to a minimal Ruby or Python installation, it’s certainly the same as any Java app. Elixir cli’s at least start much faster than most java apps I’ve used, they do have a minimal runup time that makes them less than ideal for some uses.

sotojuan

sotojuan

For a CLI tool? Probably not, but it depends on the context. Ruby, Perl, Python (latter two especially) are almost universal in Unix boxes. It’s a good bet they’re installed. If you’re making a simple CLI tool for many different kinds of users and platforms, I do think shipping a whole VM or expecting a runtime isn’t the best way to go, but I’m not that strict about it (I use Node CLI tools!). If it’s more complex and Elixir, Ruby, or Python can be expected to generally be available, go ahead. youtube-dl, a famous CLI tool uses Python and it hasn’t hurt its adoption.

belaustegui

belaustegui OP

Thank you all for your replies! This is why I love this community :purple_heart:

As @bbense says, I could use Ruby, Perl or Python. In fact the tool I am going to build is a replacement for an existing one that is currently written in Ruby. I am choosing to replace it because if I want to use the tool everywhere on my system, I have to add the gem in the global Ruby installation. This should not be a problem if the tool did not require other gems which get also installed globally. I consider this a little dirty and it is what I would like to avoid.

The idea of @brainbag and @Onor.io resembles more closely what I have in mind. Building an escript would allow me to distribute my package as a single binary with its dependencies bundle (it would still require the Erlang VM, but so happens with JARs).

I am going to opt for Go or Rust (I like more the Rust approach because it is more close to functional programming) because I want to keep the dependencies absolutely minimum, the ideal would be to distribute a binary that could be run as is. I am a little bit sad about not using Elixir, but @bbsense has a point when he says that “Where Elixir really shines for this kind of stuff is when you have a 24 core server and enough work to keep those cores busy”.

So, Go or Rust it is. Thank you all!

lrosa007

lrosa007

I’ve been using a cli project as an excuse to learn Rust. I considered a lot of the same things at first. Since my project has zero network interfacing, Elixir seemed like overkill. I’ve used Go before and although it was kinda fun, I didn’t get hooked in any where near the same way Rust or Elixir got me. FWIW, there is a lot of fast software written in Go.

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
jaybe78
Hello, I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter). The diffic...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & 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
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New
webofbits
Aludel - LLM Evaluation Workbench Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews