From ea1b6ad4866079896f0396f30edfca4700302e3e Mon Sep 17 00:00:00 2001 From: Erik Dannenberg Date: Sun, 25 Aug 2019 14:13:40 +0200 Subject: [PATCH] Add optional rlwrap support to ask function This first iteration only adds history support to the new cmd, image completion should also be feasable now. --- cmd/new.sh | 32 ++++++++++++++++++++++---------- lib/ask.sh | 15 +++++++++++++++ lib/util.sh | 12 +++++++++--- template/gitignore-data-dir | 1 + 4 files changed, 47 insertions(+), 13 deletions(-) create mode 100755 lib/ask.sh diff --git a/cmd/new.sh b/cmd/new.sh index a74ff3a9..685de009 100644 --- a/cmd/new.sh +++ b/cmd/new.sh @@ -2,6 +2,18 @@ # Copyright (c) 2014-2019, Erik Dannenberg # All rights reserved. +readonly _KUBLER_HISTORY_NS_TYPE="${KUBLER_DATA_DIR}"/.ask_history_ns_type +readonly _KUBLER_HISTORY_NS_DIR="${KUBLER_DATA_DIR}"/.ask_history_ns_dir +readonly _KUBLER_HISTORY_IMAGE_TAG="${KUBLER_DATA_DIR}"/.ask_history_image_tag + +readonly _KUBLER_HISTORY_BUILDER_IMAGE="${KUBLER_DATA_DIR}"/.ask_history_builder_image +readonly _KUBLER_HISTORY_PARENT_IMAGE="${KUBLER_DATA_DIR}"/.ask_history_parent_image + +readonly _KUBLER_HISTORY_AUTHOR="${KUBLER_DATA_DIR}"/.ask_history_author +readonly _KUBLER_HISTORY_EMAIL="${KUBLER_DATA_DIR}"/.ask_history_email +readonly _KUBLER_HISTORY_ENGINE="${KUBLER_DATA_DIR}"/.ask_history_engine +readonly _KUBLER_HISTORY_TEST_TEMPLATE="${KUBLER_DATA_DIR}"/.ask_history_test_template + # Adds given var_name and it's replacement to global assoc. array _template_values # # Arguments: @@ -75,7 +87,7 @@ function add_namespace() { msg_info_sub "Working dir type? Choices:" msg_info_sub " single - You can't add further namespaces to the created working dir, it only holds images" msg_info_sub " multi - Creates a working dir that can hold multiple namespaces" - ask 'Type' 'single' + ask 'Type' 'single' "${_KUBLER_HISTORY_NS_TYPE}" # shellcheck disable=SC2154 ns_type="${__ask}" add_template_filter_var '_tmpl_ns_type' "${ns_type}" @@ -85,7 +97,7 @@ function add_namespace() { if [[ "${_NAMESPACE_TYPE}" == 'none' && "${ns_type}" == 'multi' ]]; then msg_info_sub msg_info_sub "Top level directory name for new namespace '${ns_name}'? The directory is created at ${_NAMESPACE_DIR}/" - ask 'Namespaces Dir' 'kubler-images' + ask 'Namespaces Dir' 'kubler-images' "${_KUBLER_HISTORY_NS_DIR}" ns_dir="${_NAMESPACE_DIR}/${__ask}" [[ -e "${ns_dir}" ]] && die "Directory ${ns_dir} already exists, aborting. If you intended to create the new namespace at this location use: \\n ${_KUBLER_BIN} --working-dir=${ns_dir} new namespace ${ns_name}" @@ -94,7 +106,7 @@ function add_namespace() { msg_info_sub msg_info "Initial image tag, a.k.a. version?" - ask 'Image Tag' "${_TODAY}" + ask 'Image Tag' "${_TODAY}" "${_KUBLER_HISTORY_IMAGE_TAG}" add_template_filter_var '_tmpl_image_tag' "${__ask}" add_template_sed_replace '^IMAGE_TAG' '#IMAGE_TAG' msg_info_sub @@ -107,15 +119,15 @@ function add_namespace() { msg_info_sub msg_info 'Who maintains the new namespace?' - ask 'Name' "${def_author}" + ask 'Name' "${def_author}" "${_KUBLER_HISTORY_AUTHOR}" add_template_filter_var '_tmpl_author' "${__ask}" - ask 'EMail' "${def_mail}" + ask 'EMail' "${def_mail}" "${_KUBLER_HISTORY_EMAIL}" add_template_filter_var '_tmpl_author_email' "${__ask}" msg_info_sub msg_info 'Default build engine?' - ask 'Engine' "${def_engine}" + ask 'Engine' "${def_engine}" "${_KUBLER_HISTORY_ENGINE}" ns_engine="${__ask}" add_template_filter_var '_tmpl_engine' "${ns_engine}" @@ -206,14 +218,14 @@ function add_image() { msg_info_sub ' to accept default value' msg_info_sub msg_info_sub 'Extend an existing Kubler managed image? Fully qualified image id (i.e. kubler/busybox) or scratch' - ask 'Parent Image' 'scratch' + ask 'Parent Image' 'scratch' "${_KUBLER_HISTORY_PARENT_IMAGE}" image_parent="${__ask}" image_builder="${DEFAULT_BUILDER}" if [[ "${image_parent}" == 'scratch' ]]; then msg_info_sub msg_info_sub "Which builder should be used? Press to use the default builder of namespace ${namespace}" - ask 'Builder Id' "${DEFAULT_BUILDER}" + ask 'Builder Id' "${DEFAULT_BUILDER}" "${_KUBLER_HISTORY_BUILDER_IMAGE}" image_builder="${__ask}" [[ "${target_id}" != *"/"* ]] && die "${target_id} should have format /" [[ "${image_builder}" != "${DEFAULT_BUILDER}" ]] && add_template_sed_replace '^#BUILDER=' 'BUILDER=' @@ -228,7 +240,7 @@ function add_image() { msg_info_sub ' bt - Add a stub for a custom build-test.sh script, a good choice if HEALTH-CHECK is not suitable' msg_info_sub ' yes - Add stubs for both test types' msg_info_sub " no - Fck it, we'll do it live!" - ask 'Tests' 'hc' + ask 'Tests' 'hc' "${_KUBLER_HISTORY_TEST_TEMPLATE}" test_type="${__ask}" [[ "${test_type}" != 'hc' && "${test_type}" != 'bt' && "${test_type}" != 'yes' && "${test_type}" != 'no' ]] \ && die "'${test_type}' is not a valid choice." @@ -286,7 +298,7 @@ function add_builder() { msg_info_sub ' to accept default value' msg_info_sub msg_info_sub 'Extend existing Kubler builder image? Fully qualified image id (i.e. kubler/bob) or stage3' - ask 'Parent Image' 'stage3' + ask 'Parent Image' 'stage3' "${_KUBLER_HISTORY_BUILDER_IMAGE}" builder_parent="${__ask}" # shellcheck disable=SC2016,SC2034 diff --git a/lib/ask.sh b/lib/ask.sh new file mode 100755 index 00000000..81f99bcb --- /dev/null +++ b/lib/ask.sh @@ -0,0 +1,15 @@ +#!/usr/bin/env bash + +# rlwrap does not work with functions or bash builtins, wrap read with a external script +function main() { + __ask= + local question default_value prefix_ask + question="$1" + default_value="$2" + prefix_ask="${3:->}" + read -r -p "${prefix_ask} ${question} (${default_value}): " __ask + [[ -z "${__ask}" ]] && __ask="${default_value}" + echo "${__ask}" +} + +main "$@" diff --git a/lib/util.sh b/lib/util.sh index 9727e066..e1045e26 100644 --- a/lib/util.sh +++ b/lib/util.sh @@ -274,18 +274,24 @@ function pwrap_handler() { unset _pwrap_handler_args _prwap_callback; } } -# Read user input displaying given question +# Read user input displaying given question, supports rlwrap if installed for history and completion # # Arguments: # 1: question # 2: default_value +# 3: history_file - optional, only used for rlwrap, default: "${KUBLER_DATA_DIR}"/.bash_history # Return value: user input or passed default_value function ask() { __ask= - local question default_value + local question default_value history_file question="$1" default_value="$2" - read -r -p "${_prefix_ask} ${question} (${default_value}): " __ask + history_file="${3:-${KUBLER_DATA_DIR}/.ask_history}" + if hash rlwrap 2>/dev/null; then + __ask="$(rlwrap -D -A -H "${history_file}" "${_LIB_DIR}"/ask.sh "${question}" "${default_value}" "${_prefix_ask}")" + else + read -r -p "${_prefix_ask} ${question} (${default_value}): " __ask + fi [[ -z "${__ask}" ]] && __ask="${default_value}" } diff --git a/template/gitignore-data-dir b/template/gitignore-data-dir index ee29af93..1df9910e 100644 --- a/template/gitignore-data-dir +++ b/template/gitignore-data-dir @@ -6,3 +6,4 @@ log/ namespaces/kubler/ packages/ tmp/ +.ask_history*