From c41d2af86bb58ab7423244d541922d2eddf39bfa Mon Sep 17 00:00:00 2001 From: pvincent Date: Thu, 29 Feb 2024 22:39:26 +0400 Subject: [PATCH] autodetect_email from .ssh/authorized_keys --- lib/install.sh | 38 ++++++++++++++++++++-------------- templates/etc/defaults.yaml.j2 | 2 +- 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/lib/install.sh b/lib/install.sh index e00d7f0..cb778ca 100755 --- a/lib/install.sh +++ b/lib/install.sh @@ -226,7 +226,6 @@ function check_credential { check_yaml_defined_value /etc/miaou/defaults.yaml 'credential.username' && check_yaml_defined_value /etc/miaou/defaults.yaml 'credential.shadow' && check_yaml_defined_value /etc/miaou/defaults.yaml 'credential.email' - } function check_target() { @@ -260,7 +259,7 @@ function miaou_configfiles() { if [[ ! -f /etc/miaou/defaults.yaml ]]; then echo -n "building /etc/miaou/defaults.yaml for the first time..." shadow_passwd=$(sudo grep "$CURRENT_USER" /etc/shadow | cut -d ':' -f2) - env current_user="$CURRENT_USER" shadow_passwd="$shadow_passwd" tera -e --env-key env --env-only -t "$MIAOU_BASEDIR/templates/etc/defaults.yaml.j2" -o /etc/miaou/defaults.yaml >/dev/null + env current_user="$CURRENT_USER" shadow_passwd="$shadow_passwd" valid_email="$valid_email" tera -e --env-key env --env-only -t "$MIAOU_BASEDIR/templates/etc/defaults.yaml.j2" -o /etc/miaou/defaults.yaml >/dev/null yq ".target=\"$TARGET\"" /etc/miaou/defaults.yaml -i PREFIX="" echoinfo OK fi @@ -339,8 +338,7 @@ EOF } function extra_dev_desktop { - # detect if DEV - # detect if DESKTOP + # FIXME: detect if DEV && DESKTOP_USAGE to install spicy-client and other desktop tools : } @@ -363,15 +361,13 @@ EOF fi } -function ask_for_credential { - local PREFIX="ask:credential" - if ! check_credential 2>/dev/null; then - echo "further details required, please replace any by a proper value ...press any key to open editor" - read -rn1 - editor /etc/miaou/defaults.yaml - fi - check_credential - echo "successfully checked!" +function ask_for_email { + local PREFIX="install:ask_for_email" + valid_email=$(auto_detect_email) + while ! is_email_valid "$valid_email"; do + echo -n "mandatory email: " + read -rei "$valid_email" valid_email + done } function preload_bookworm_image { @@ -393,7 +389,15 @@ function is_email_valid { } function auto_detect_email { - : + found_email="" + if [[ -f $HOME/.ssh/authorized_keys ]]; then + while IFS= read -r line; do + found_email="$line" + is_email_valid "$found_email" && break + done < <(cut -d ' ' -f3 <"$HOME/.ssh/authorized_keys") + fi + + builtin echo "$found_email" } ### MAIN @@ -423,8 +427,11 @@ else TARGET=${1:-} CURRENT_USER=$(id -un) - sudo_required + check_normal_user check_target + ask_for_email + + sudo_required install_miaou_bash install_mandatory_commands prepare_toolbox @@ -435,7 +442,6 @@ else miaou_resolver miaou_evalfrombashrc miaou_configfiles - ask_for_credential opt_link extra_dev_desktop diff --git a/templates/etc/defaults.yaml.j2 b/templates/etc/defaults.yaml.j2 index 8bf1ee9..dfac725 100644 --- a/templates/etc/defaults.yaml.j2 +++ b/templates/etc/defaults.yaml.j2 @@ -5,4 +5,4 @@ containers: credential: username: {{env.current_user}} shadow: {{env.shadow_passwd}} - email: TO BE DEFINED # example user@domain.tld + email: {{env.valid_email}}