Skip to content

Instantly share code, notes, and snippets.

@tgran2028
Created February 27, 2025 09:10
Show Gist options
  • Save tgran2028/08b21ba17ac9bdd96920ee9395db3c43 to your computer and use it in GitHub Desktop.
Save tgran2028/08b21ba17ac9bdd96920ee9395db3c43 to your computer and use it in GitHub Desktop.
Export installed bin crates metadata to JSON
#!/usr/bin/env bash
#
# export installed cargo binaries as JSON
set -euo pipefail
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if ! command -v cargo &> /dev/null; then
if [[ -x $HOME/.cargo/bin/cargo ]]; then
[[ -r $HOME/.cargo/env ]] && source "$HOME/.cargo/env"
CMD=$HOME/.cargo/bin/cargo
else
echo "cargo not found"
exit 1
fi
else
CMD=cargo
fi
[[ -n $CMD ]] || exit 1
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
source /etc/os-release
[[ -n $PRETTY_NAME ]] || PRETTY_NAME=$(uname -s)
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# iterate over each line, split by space to get name and version
# create a JSON object using jq
# collect all JSON objects into an array of installed crates
# return object with metadata and array of installed crates
exec "$CMD" install --list | grep ':$' | sed 's/://g' | while read -r line; do
# name of crate
name=$(cut -d ' ' -f 1 <<< "$line")
# version of crate
version=$(cut -d ' ' -f 2 <<< "$line")
# description of crate
desc=$(exec "$CMD" search "$name" 2>&1 | grep -n "^$name =" | cut -d'#' -f2 | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
# create JSON object for create
jq -n --arg name "$name" --arg version "$version" --arg desc "$desc" \
'{
name: $name,
version: $version,
description: $desc
}'
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# collect all JSON objects into an array. Add metadata.
done | jq -s -M -c \
--arg date "$(date -u +"%Y-%m-%dT%H:%M:%SZ")" \
--arg desc "Installed cargo binaries" \
--arg user "$USER" \
--arg host "$(hostname)" \
--arg os "$PRETTY_NAME" \
--arg cargo_version "$(exec "$CMD" --version)" \
'{
date: $date,
description: $desc,
user: $user,
host: $host,
os: $os,
cargo_version: $cargo_version,
binaries: .
}' | tee "${CARGO_HOME:-$HOME/.cargo}/.cargo-bin-export.json"
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@tgran2028
Copy link
Author

Example output

{
  "date": "2025-02-27T09:11:27Z",
  "description": "Installed cargo binaries",
  "user": "tim",
  "host": "tim-XPS",
  "os": "Ubuntu 24.04.2 LTS",
  "cargo_version": "cargo 1.87.0-nightly (1d1d646c0 2025-02-21)",
  "binaries": [
    {
      "name": "aichat",
      "version": "v0.28.0",
      "description": "All-in-one LLM CLI Tool"
    },
    {
      "name": "atuin",
      "version": "v18.4.0",
      "description": "atuin - magical shell history"
    },
    {
      "name": "bat",
      "version": "v0.25.0",
      "description": "A cat(1) clone with wings."
    },
    {
      "name": "broot",
      "version": "v1.44.7",
      "description": "A new file manager"
    },
    {
      "name": "cargo-binstall",
      "version": "v1.11.1",
      "description": "Binary installation for rust projects"
    },
    {
      "name": "cargo-update",
      "version": "v16.1.0",
      "description": "A cargo subcommand for checking and applying updates to installed executables"
    },
    {
      "name": "choose",
      "version": "v1.3.6",
      "description": "A human-friendly and fast alternative to cut and (sometimes) awk"
    },
    {
      "name": "comrak",
      "version": "v0.35.0",
      "description": "A 100% CommonMark-compatible GitHub Flavored Markdown parser and formatter"
    },
    {
      "name": "counts",
      "version": "v1.0.4",
      "description": "A command line tool for ad hoc profiling."
    },
    {
      "name": "deno",
      "version": "v2.2.2",
      "description": "Provides the deno executable"
    },
    {
      "name": "dpkg-query-json",
      "version": "v0.1.12",
      "description": "A crate for parsing \"dpkg-query\" in json. Debian/Ubuntu."
    },
    {
      "name": "exa",
      "version": "v0.10.1",
      "description": "A modern replacement for ls"
    },
    {
      "name": "eza",
      "version": "v0.20.22",
      "description": "A modern replacement for ls"
    },
    {
      "name": "fd-find",
      "version": "v10.2.0",
      "description": "fd is a simple, fast and user-friendly alternative to find."
    },
    {
      "name": "git-delta",
      "version": "v0.18.2",
      "description": "A syntax-highlighting pager for git"
    },
    {
      "name": "grex",
      "version": "v1.4.5",
      "description": "grex generates regular expressions from user-provided test cases."
    },
    {
      "name": "htmlq",
      "version": "v0.4.0",
      "description": "Like jq, but for HTML."
    },
    {
      "name": "just",
      "version": "v1.39.0",
      "description": "🤖 Just a command runner"
    },
    {
      "name": "lsd",
      "version": "v1.1.5",
      "description": "An ls command with a lot of pretty colors and some other stuff."
    },
    {
      "name": "mdbook",
      "version": "v0.4.45",
      "description": "Creates a book from markdown files"
    },
    {
      "name": "mdcat",
      "version": "v2.7.1",
      "description": "cat for markdown: Show markdown documents in terminals"
    },
    {
      "name": "monolith",
      "version": "v2.8.3",
      "description": "CLI tool for saving web pages as a single HTML file"
    },
    {
      "name": "navi",
      "version": "v2.23.0",
      "description": "An interactive cheatsheet tool for the command-line"
    },
    {
      "name": "netscanner",
      "version": "v0.6.2",
      "description": "Network Scanner"
    },
    {
      "name": "nu",
      "version": "v0.102.0",
      "description": "A new type of shell"
    },
    {
      "name": "nu_plugin_highlight",
      "version": "v1.4.3+0.102.0",
      "description": "A nushell plugin for syntax highlighting"
    },
    {
      "name": "ouch",
      "version": "v0.5.1",
      "description": "A command-line utility for easily compressing and decompressing files and directories."
    },
    {
      "name": "pastel",
      "version": "v0.10.0",
      "description": "A command-line tool to generate, analyze, convert and manipulate colors"
    },
    {
      "name": "posh-fzf",
      "version": "v0.2.8",
      "description": "Fzf keybinding integration for powershell"
    },
    {
      "name": "rargs",
      "version": "v0.3.0",
      "description": "xargs + awk with pattern matching support."
    },
    {
      "name": "rls",
      "version": "v0.122.2",
      "description": "Rust Language Server - provides information about Rust programs to IDEs and other tools"
    },
    {
      "name": "rm-improved",
      "version": "v0.13.1",
      "description": "rip: a safe and ergonomic alternative to rm"
    },
    {
      "name": "sd",
      "version": "v1.0.0",
      "description": "An intuitive find & replace CLI"
    },
    {
      "name": "skim",
      "version": "v0.16.0",
      "description": "Fuzzy Finder in rust!"
    },
    {
      "name": "starship",
      "version": "v1.22.1",
      "description": "The minimal, blazing-fast, and infinitely customizable prompt for any shell! ☄🌌️"
    },
    {
      "name": "topgrade",
      "version": "v16.0.2",
      "description": "Upgrade all the things"
    },
    {
      "name": "xcp",
      "version": "v0.23.1",
      "description": "xcp is a (partial) clone of the Unix `cp` command, with more user-friendly feedback and some perfo…"
    },
    {
      "name": "zellij",
      "version": "v0.41.2",
      "description": "A terminal workspace with batteries included"
    }
  ]
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment