Skip to content

Instantly share code, notes, and snippets.

@QuestionDriven
Last active March 8, 2024 17:45
Show Gist options
  • Save QuestionDriven/214110f5f65e4d49ac96f16f06114463 to your computer and use it in GitHub Desktop.
Save QuestionDriven/214110f5f65e4d49ac96f16f06114463 to your computer and use it in GitHub Desktop.
Generate Karabiner-Elements' json file for import from Goku edn file
#! /usr/bin/env bash
set -e -u -o pipefail
#Usage
usage="Usage: ./generate_json.sh [options] <edn_config_file> <rule_title>"
show_help() {
cat << EOF
$usage
Generate Karabiner-Elements' json file for import from Goku edn file
-h Display this help and exit.
-n No jq: just run 'goku --dry-run-all' and output the whole karabiner.json
-O option will be ignored if this option is specified
-O Write the generated json file to the output directory (overwrite)
The filepath will be "$HOME/.config/karabiner/assets/complex_modifications/<rule_tile>.json"
-p Specify profile name (if not specified, the profile named 'Default' will be used)
EOF
}
# Defaults
no_jq=false
write_file=false
output_dir="$HOME/.config/karabiner/assets/complex_modifications/"
profile="Default"
# Parse options
OPTIND=1
while getopts "hnOp:" opt; do
case "$opt" in
"h") show_help; exit 0;;
"n") no_jq=true;;
"O") write_file=true;;
"p") profile=$OPTARG;;
esac
done
shift $(($OPTIND - 1))
# Check arguments
[[ $# -lt 2 ]] && { show_help; exit 1; }
edn_file=${1}
[[ ! -f "$edn_file" ]] && { printf "No such file: %s\n" "$edn_file"; exit 1; }
title=${2}
# Main
export GOKU_EDN_CONFIG_FILE="$edn_file"
output_file=$(printf "%s%s.json" "$output_dir" "$title")
jq_filter='{title: $title, rules: (.profiles | map(select(.name == $profile))[0] | .complex_modifications.rules)}'
if "$no_jq"; then
goku --dry-run-all
elif [ "$write_file" == false ]; then
goku --dry-run-all | jq --arg profile "$profile" --arg title "$title" "$jq_filter"
else
goku --dry-run-all | jq --arg profile "$profile" --arg title "$title" "$jq_filter" >| "$output_file"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment