From 0d70a6eadd5f0b46d2b77cc8009493e994231a3d Mon Sep 17 00:00:00 2001 From: Dmytro Serdiuk Date: Sun, 3 Nov 2019 00:33:12 +0200 Subject: [PATCH] Simplify the configuration process The configuration will start from `acquire-git` command and guide a user through the process interactively. If standards are configured, they will be printed to CLI instead of reconfiguring. During the installation, `acquire-git` will be executed instead of providing the instructions. This allows updating settings for the new versions automatically. Now, the `read` addon supports partial mocking of the answers correctly. This works because a replay variable is always set with either provided value or empty value. #177 --- .workflows/ci-pipeline.bash | 4 +++ docs/commands.md | 3 ++ docs/configuration.md | 6 ++-- docs/getting-started.md | 7 +++-- install.bash | 12 +------- libexec/git-elegant-acquire-git | 41 +++++++++++++++++++++++++- libexec/git-elegant-acquire-repository | 2 +- libexec/plugins/configuration | 31 +++++++++++++++++-- tests/addons-read.bash | 4 ++- tests/git-elegant-acquire-git.bats | 23 +++++++++++++++ workflows | 9 +++++- 11 files changed, 118 insertions(+), 24 deletions(-) diff --git a/.workflows/ci-pipeline.bash b/.workflows/ci-pipeline.bash index 5cfc8e1..ea8737c 100755 --- a/.workflows/ci-pipeline.bash +++ b/.workflows/ci-pipeline.bash @@ -15,6 +15,10 @@ pipeline() { .workflows/bats-pipeline.bash || fail "Unit tests are failed." ( echo "Installation...." + git config --global user.name "Elegant Git" + git config --global user.email elegant.git@email.com + git config --global core.editor some-editor + git config --global elegant.acquired true ./install.bash /usr/local src echo "'Unknown command' testing..." git elegant unknown-command | grep "Unknown command: git elegant unknown-command" diff --git a/docs/commands.md b/docs/commands.md index 6de2d15..a698262 100644 --- a/docs/commands.md +++ b/docs/commands.md @@ -82,6 +82,9 @@ usage: git elegant acquire-repository Applies the "basics", "standards", and "aliases" configurations to the current Git installation using `git config --global`. +During the first execution, you will be asked to provide some information. +After, Elegant Git will automatically detect what should be changed. + To find out what will be configured, please visit https://elegant-git.bees-hive.org/en/latest/configuration/ diff --git a/docs/configuration.md b/docs/configuration.md index a4a1bb0..117e108 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -1,7 +1,7 @@ # Approach -Elegant Git aims to standardize how a Git repository should be configured. It operates 3 levels of -configurations (basics, standards, and aliases - see below) which can be applied to a repository -(local configuration) or to a Git globally (global configuration). +Elegant Git aims to standardize how a work environment should be configured. It operates 3 levels +of configurations (basics, standards, and aliases - see below) which can be applied to a Git +repository (local configuration) and (or) to a Git installation globally (global configuration). The local configuration applies by running [`git elegant acquire-repository`](commands.md#acquire-repository) and configures current Git diff --git a/docs/getting-started.md b/docs/getting-started.md index fa77144..afc6021 100644 --- a/docs/getting-started.md +++ b/docs/getting-started.md @@ -1,9 +1,10 @@ # Several important words You can install Elegant Git by either executing `bash` script or using `homebrew`. After the -installation, please run [`git elegant acquire-repository`](commands.md#acquire-repository) -for an existing repository to apply [the needed Git configuration](configuration.md). +installation, please configure your environment by running +[`git elegant acquire-git`](commands.md#acquire-git) and follow the instructions. To find out more, +please read [the configuration approach](configuration.md). -Run in CLI any of +You can access Elegant Git in CLI using any of ```bash git git elegant diff --git a/install.bash b/install.bash index ea31aed..d736ca6 100755 --- a/install.bash +++ b/install.bash @@ -60,17 +60,7 @@ main() { copy "${path}" ${INSTALL_PATH} fi echo "Elegant Git is installed to '${INSTALL_PATH}/bin/git-elegant'." - echo " -The final step after installation is to run - git elegant acquire-git -to apply the latest Elegant Git's configuration. - -If this step is not completed, Elegant Git may behave unexpectedly -since the desired Git configuration (relevant for the current version) -is not applied. Please read more on - https://elegant-git.bees-hive.org/en/latest/configuration/ - -" + git-elegant acquire-git command -v git-elegant 1>/dev/null 2>&1 || next-steps ${INSTALL_PATH} } diff --git a/libexec/git-elegant-acquire-git b/libexec/git-elegant-acquire-git index 8b5d651..63b911d 100755 --- a/libexec/git-elegant-acquire-git +++ b/libexec/git-elegant-acquire-git @@ -18,6 +18,9 @@ command-description() { Applies the "basics", "standards", and "aliases" configurations to the current Git installation using \`git config --global\`. +During the first execution, you will be asked to provide some information. +After, Elegant Git will automatically detect what should be changed. + To find out what will be configured, please visit ${__site}/en/latest/configuration/ MESSAGE @@ -25,7 +28,43 @@ MESSAGE default() { source ${BINS}/plugins/configuration - basics-configuration --global user_name user_email core_editor + if ! $(is-acquired) ; then + info-box "Thank you for installing Elegant Git! Let's configure it..." + cat < ... + # usage: interactive-configuration <--global | --local> ... -- ... info-box "Configuring basics..." - info-text "Please hit enter if you wish {default value}." local scope=${1}; shift + local nonset=false + local notify=true for f in ${@}; do + local key=$(eval "echo -n \$${f}_key") + if [[ "${f}" == "--" ]]; then + nonset=true + continue + fi + if ${nonset}; then + local current="$(git config ${scope} --get ${key})" + if [[ -n ${current} ]] ; then + command-text "git config ${scope} ${key} ${current}" + continue + fi + fi + if ${notify}; then + info-text "Please hit enter if you wish {default value}." + notify=false + fi unset ANSWER while [[ -z "${ANSWER}" ]]; do __ask_question ${f} if [[ -n "${ANSWER}" ]]; then - git-verbose config ${scope} $(eval "echo -n \$${f}_key") "${ANSWER}" + git-verbose config ${scope} ${key} "${ANSWER}" fi done done @@ -105,3 +122,11 @@ aliases-configuration() { git-verbose config ${scope} "alias.${alias}" "${origin}" done } + +is-acquired() { + # usage: if $(acquired-state) ; then + if [[ "$(git config --global --get ${acquired[0]} || true)" == ${acquired[1]} ]]; then + return 0 + fi + return 1 +} diff --git a/tests/addons-read.bash b/tests/addons-read.bash index 183c136..643face 100755 --- a/tests/addons-read.bash +++ b/tests/addons-read.bash @@ -32,9 +32,11 @@ read-clean() { read() { export next_read=$((next_read + 1)) + local value if [[ -f "${answers_directory}/${next_read}" ]]; then - eval "export ${1}=$(cat ${answers_directory}/${next_read})" + value=$(cat ${answers_directory}/${next_read}) fi + eval "export ${1}=${value}" echo "" } diff --git a/tests/git-elegant-acquire-git.bats b/tests/git-elegant-acquire-git.bats index 069d630..89a0cb3 100644 --- a/tests/git-elegant-acquire-git.bats +++ b/tests/git-elegant-acquire-git.bats @@ -7,11 +7,13 @@ load addons-repo setup() { repo-new + read-answer "y" } teardown() { fake-clean repo-clean + read-clean } @test "'acquire-git': all configurations work as expected" { @@ -21,6 +23,7 @@ teardown() { @test "'acquire-git': basics are configured as expected" { check git-elegant acquire-git + [[ "${lines[@]}" =~ "Please hit enter if you wish {default value}." ]] [[ "${lines[@]}" =~ "What is your user name? {Elegant Git}: " ]] [[ "${lines[@]}" =~ "==>> git config --global user.name Elegant Git" ]] [[ "${lines[@]}" =~ "What is your user email? {elegant-git@example.com}: " ]] @@ -93,3 +96,23 @@ teardown() { [[ "$status" -eq 0 ]] [[ "${lines[@]}" =~ "2 Elegant Git aliases were removed." ]] } + +@test "'acquire-git': a message is displayed if global configuration is disabled" { + read-clean + read-answer "n" + check git-elegant acquire-git + [[ "$status" -eq 0 ]] + [[ "${lines[@]}" =~ "You've decided to stay with local configurations. Great!" ]] +} + +@test "'acquire-git': the basics are not changed if they are already configured" { + repo git config --global user.name aaaa + repo git config --global user.email aaaa + repo git config --global core.editor aaaa + check git-elegant acquire-git + [[ "$status" -eq 0 ]] + [[ "${lines[@]}" =~ "==>> git config --global user.name aaaa" ]] + [[ "${lines[@]}" =~ "==>> git config --global user.email aaaa" ]] + [[ "${lines[@]}" =~ "==>> git config --global core.editor aaaa" ]] + [[ ! "${lines[@]}" =~ "Please hit enter if you wish {default value}." ]] +} diff --git a/workflows b/workflows index 017308c..dae0df7 100755 --- a/workflows +++ b/workflows @@ -29,7 +29,14 @@ repository() { info-text "Init repository..." docker exec -it repository bash -c "source /eg/tests/addons-repo.bash;source /eg/tests/addons-common.bash; repo-new" info-text "Install Elegant Git..." - docker exec -it repository bash -c "cd /eg; ./install.bash /usr/local src" + docker exec -it repository bash -c " + cd /eg + git config --global user.name \"Elegant Git\" + git config --global user.email elegant.git@email.com + git config --global core.editor some-editor + git config --global elegant.acquired true + ./install.bash /usr/local src + " info-text "Ready! Enjoy experiments..." docker attach repository }