Skip to content

Instantly share code, notes, and snippets.

@tomodachi94
Last active March 28, 2024 04:13
Show Gist options
  • Save tomodachi94/aaae79f7cb4e7b2087727fbbfe05eb12 to your computer and use it in GitHub Desktop.
Save tomodachi94/aaae79f7cb4e7b2087727fbbfe05eb12 to your computer and use it in GitHub Desktop.
craftos-select: register names for CraftOS-PC computers and then launch them

craftos-select

Register names for CraftOS-PC computers and then launch them with fzf.

Dependencies

  • CraftOS-PC (with the CLI accessible)
  • awk (GNU awk was used in development, but any compliant version should suffice)
  • fzf (for picking the computer from the registry)
  • jq (for manipulating the registry)

Usage

Add this to your .profile/.bashrc/.zshrc

# Path to the JSON registry file, feel free to adjust as desired
export CRAFTOS_SELECT_REGISTRY_FILE="$HOME/.local/share/craftos-select/registry.json"

source /path/to/craftos-select
source /ptah/to/craftos-register

Then, relaunch your shell and run this:

craftos-register <id of computer> "<name of computer, spaces allowed>"
# Run this as many times as you want, or edit the registry file manually
craftos-select
# Pick the computer you want
# CraftOS-PC opens
# Script exits
#!/usr/bin/env zsh
# Function to add a new computer entry to the registry
craftos-register() {
readonly id=${1:?"The id must be specified."}
readonly name=${2:?"The name must be specified."}
if [ ! -f "$CRAFTOS_SELECT_REGISTRY_FILE" ]; then
# Create the registry file with an initial empty array
echo '{"computers": []}' > "$CRAFTOS_SELECT_REGISTRY_FILE"
fi
jq --arg name "$name" --argjson id "$id" '.computers += [{"name": $name, "id": $id}]' "$CRAFTOS_SELECT_REGISTRY_FILE" > "$CRAFTOS_SELECT_REGISTRY_FILE.tmp" && mv "$CRAFTOS_SELECT_REGISTRY_FILE.tmp" "$CRAFTOS_SELECT_REGISTRY_FILE"
}
#!/usr/bin/env zsh
# Function to pick a computer from the registry and launch it
craftos-select() {
# Check if the registry file exists
if [ ! -f "$CRAFTOS_SELECT_REGISTRY_FILE" ]; then
echo "Error: Registry file not found at $CRAFTOS_SELECT_REGISTRY_FILE"
return 1
fi
local computers=$(jq -r '.computers[] | "\(.name) \(.id)"' "$CRAFTOS_SELECT_REGISTRY_FILE")
local selected=$(echo "$computers" | fzf --prompt="Select a computer: ")
id=$(echo "$selected" | awk 'END {print $NF}')
craftos --id "$id"
}
#!/usr/bin/env zsh
# Do nothing if requirements are not installed
(( ${+commands[craftos]} )) || echo "WARNING: craftos not found"
(( ${+commands[jq]} )) || echo "WARNING: jq not found"
(( ${+commands[fzf]} )) || echo "WARNING: fzf not found"
# Configuration
export CRAFTOS_SELECT_REGISTRY_FILE=${CRAFTOS_SELECT_REGISTRY_FILE:-"${XDG_DATA_DIR:-$HOME/.local/share}/craftos-select/registry.json"}
mkdir -p "$(dirname $CRAFTOS_SELECT_REGISTRY_FILE)"
# Load opswd function
autoload -Uz craftos-select
autoload -Uz craftos-register
{
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
outputs = { self, nixpkgs }:
let
forAllSystems = function:
nixpkgs.lib.genAttrs [
"x86_64-linux"
"aarch64-linux"
"x86_64-darwin"
"aarch64-darwin"
]
(system: function nixpkgs.legacyPackages.${system});
in
{
packages = forAllSystems (pkgs: {
default = let
mkExecSubstitution = with pkgs; file: ''
substituteInPlace '${file}' \
--replace 'awk ' '${lib.getExe gawk} '
substituteInPlace '${file}' \
--replace 'craftos ' '${lib.getExe craftos-pc} '
substituteInPlace '${file}' \
--replace 'jq ' '${lib.getExe jq} '
substituteInPlace '${file}' \
--replace 'fzf ' '${lib.getExe fzf} '
'';
in pkgs.stdenv.mkDerivation {
name = "zsh-plugin-craftos-select";
src = ./.;
installPhase = ''
runHook preInstall
mkdir -p $out
install $src/craftos-select.plugin.zsh $out/craftos-select.plugin.zsh
install $src/craftos-select $out/craftos-select
install $src/craftos-register $out/craftos-register
runHook postInstall
'';
postInstall = ''
cd $out
${mkExecSubstitution "craftos-select.plugin.zsh"}
${mkExecSubstitution "craftos-select"}
${mkExecSubstitution "craftos-register"}
'';
};
});
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment