this post was submitted on 27 Feb 2024
19 points (100.0% liked)

Rust

5999 readers
23 users here now

Welcome to the Rust community! This is a place to discuss about the Rust programming language.

Wormhole

!performance@programming.dev

Credits

  • The icon is a modified version of the official rust logo (changing the colors to a gradient and black background)

founded 1 year ago
MODERATORS
 

Hello fellow rustaceans! Recently, there was a thread about how we can grow this community (how can I link to posts across servers?), where I already talked briefly about this topic, saying that I did not know if it is worthy of a full post here, as most things seem to be pretty professional looking links to talks and blogs. I've gotten some encouragement to post it, so here we go:

When to use a library instead of a CLI

I'm working on a little project called Autcrate in my free time, which aims to streamline the release and publishing process (what exactly it does isn't really important for this discussion). Autocrate uses git to get the path of the current repo, tags and pushes releases, generates a change log from commits and so on.

Up until a week ago, I was just using the git2 library crate, which offers the functionalities of libgit2 for rust. While good, using this crate is much more complicated than for example just executing git push from my program using std::process::command. I am only using the porcelain functionalities of git (as of now), so all functionality could be achieved by calling the CLI interface.

Question

When is it acceptable to use CLI Commands instead of using libraries provided for that same software?

Is it generally better to use API/ABI from libraries, or is it maybe even better to use the CLI interface, reducing the list of dependencies?

Pro and Con of using Commands instead of libraries

Pro Con
Reduces the dependencies of a crate Adds a dependency that cannot be tracked by cargo
Much easier to program for developers The CLI interface is not versioned and might break in the future
Documentation of the CLI interface is often better than of libraries Bad usage of command cannot be detected at compile time
Cli program might not be available depending on architecture or platform

(this is of course not an exhaustive list. I will edit it if something comes up in the thread.)

Edit

Alright then. Thank you for your answers. While using the git CLI would probably be fine, since it's very stable and available on most systems (especially those for CI/CD), it might change and is at best hacky. I'll be doing the "right" thing and use libgit2 instead of just calling CLI commands.

you are viewing a single comment's thread
view the rest of the comments
[โ€“] snaggen@programming.dev 5 points 8 months ago (1 children)

In my mind I think that doing an execution of an external program is fine in scritps, but for more robust programs you should use libraries. There are of course exceptions, I once used libsvn instead of just calling the svn binary (as you understand, this was a long time ago, when svn was till the way to version software). libsvn turned out to be the most horrible library, so for that it would have been better to call the binary instead. But, in general, avoid runtime dependencies and errors if possible, also libraries normally allows for much better error handling. So, I always use a library if available and not obviously horrible.

[โ€“] PlexSheep@feddit.de 1 points 8 months ago

Thank you for your answer. The git crate is still not that easy to understand, but the documentation is there and the interface is alright once you figured out what needs to be done, so I've decided to use it as a library.