From 9824616c633ce14c35cb27691fa7042e3c9ea427 Mon Sep 17 00:00:00 2001 From: Chris Kruining Date: Thu, 11 Dec 2025 23:20:36 +0100 Subject: [PATCH] feat: implement user management in just --- .jq/table.jq | 6 ++-- .just/users.just | 89 +++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 83 insertions(+), 12 deletions(-) diff --git a/.jq/table.jq b/.jq/table.jq index 83b98f2..5c58aef 100644 --- a/.jq/table.jq +++ b/.jq/table.jq @@ -26,7 +26,7 @@ def to_cells(sizes): to_cells(sizes; null); def to_line(left; joiner; right): [left, .[1], (.[1:] | map([joiner, .]) ), right] | flatten | join(""); -def to_table(data; header_callback; cell_callback): +def create(data; header_callback; cell_callback): (data[0] | to_entries | map(.key)) as $keys | ([$keys]) as $header | (data | map(to_entries | map(.value))) as $rows @@ -55,5 +55,5 @@ def to_table(data; header_callback; cell_callback): | join("\n") ); -def to_table(data; header_callback): to_table(data; header_callback; null); -def to_table(data): to_table(data; _::style(_::BOLD); null); +def create(data; header_callback): create(data; header_callback; null); +def create(data): create(data; _::style(_::BOLD); null); diff --git a/.just/users.just b/.just/users.just index cecd74b..486ac67 100644 --- a/.just/users.just +++ b/.just/users.just @@ -6,17 +6,16 @@ _default: [script] list: - cd .. && just vars get ulmo zitadel/users \ - | jq fromjson \ - | jq -r -C ' - include ".jq/table"; - include ".jq/format"; + cd .. && just vars get ulmo zitadel/users | jq -r -C ' + import ".jq/table" as table; + import ".jq/format" as f; - to_entries + fromjson + | to_entries | sort_by(.key) | map( - (.key|to_title) + ":\n" - + to_table( + (.key|f::to_title) + ":\n" + + table::create( .value | to_entries | sort_by(.key) @@ -24,4 +23,76 @@ list: ) ) | join("\n\n┄┄┄\n\n") - ' + '; + +[script] +add: + exec 5>&1 + + pad () { [ "$#" -gt 1 ] && [ -n "$2" ] && printf "%$2.${2#-}s" "$1"; } + + input() { + local label=$1 + local value=$2 + + local res=$(gum input --header "$label" --value "$value") + echo -e "\e[2m$(pad "$label" -11)\e[0m$res" >&5 + echo $res + } + + data=`cd .. && just vars get ulmo zitadel/users | jq 'fromjson'` + + # Gather inputs + org=` + jq -r 'to_entries | map(.key)[]' <<< "$data" \ + | gum choose --header 'Which organisation to save to?' --select-if-one + ` + username=`input 'user name' 'new-user'` + email=`input 'email' 'new.user@example.com'` + first_name=`input 'first name' 'John'` + last_name=`input 'last name' 'Doe'` + + user_exists=`jq --arg 'org' "$org" --arg 'username' "$username" '.[$org][$username]? | . != null' <<< "$data"` + + if [ "$user_exists" == "true" ]; then + gum confirm 'User already exists, overwrite it?' --padding="1 1" || exit 0 + fi + + next=` + jq \ + --arg 'org' "$org" \ + --arg 'username' "$username" \ + --arg 'email' "$email" \ + --arg 'first_name' "$first_name" \ + --arg 'last_name' "$last_name" \ + --compact-output \ + '.[$org] += { $username: { email: $email, firstName: $first_name, lastName: $last_name } }' \ + <<< $data + ` + + gum spin --title "saving..." -- echo "$(cd .. && just vars set ulmo 'zitadel/users' "$next")" + +[script] +remove: + data=`cd .. && just vars get ulmo zitadel/users | jq fromjson` + + # Gather inputs + org=` + jq -r 'to_entries | map(.key)[]' <<< "$data" \ + | gum choose --header 'Which organisation?' --select-if-one + ` + user=` + jq -r --arg org "$org" '.[$org] | to_entries | map(.key)[]' <<< "$data" \ + | gum choose --header 'Which user?' --select-if-one + ` + + next=` + jq \ + --arg 'org' "$org" \ + --arg 'user' "$user" \ + --compact-output \ + 'del(.[$org][$user])' \ + <<< $data + ` + + gum spin --title "saving..." -- echo "$(cd .. && just vars set ulmo 'zitadel/users' "$next")"