Skip to content

Commit

Permalink
Simplify the configuration process
Browse files Browse the repository at this point in the history
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
  • Loading branch information
extsoft committed Nov 3, 2019
1 parent f9b7606 commit 0d70a6e
Show file tree
Hide file tree
Showing 11 changed files with 118 additions and 24 deletions.
4 changes: 4 additions & 0 deletions .workflows/ci-pipeline.bash
Original file line number Diff line number Diff line change
Expand Up @@ -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 [email protected]
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"
Expand Down
3 changes: 3 additions & 0 deletions docs/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/

Expand Down
6 changes: 3 additions & 3 deletions docs/configuration.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
7 changes: 4 additions & 3 deletions docs/getting-started.md
Original file line number Diff line number Diff line change
@@ -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 <command>
git elegant <command>
Expand Down
12 changes: 1 addition & 11 deletions install.bash
Original file line number Diff line number Diff line change
Expand Up @@ -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}
}

Expand Down
41 changes: 40 additions & 1 deletion libexec/git-elegant-acquire-git
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,53 @@ 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
}

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 <<MESSAGE
Elegant Git aims to standardize how a work environment should be configured.
It operates 3 levels of configurations (basics, standards, and aliases) which
can be applied to a Git repository (local configuration) and (or) to a Git
installation globally (global configuration).
A global configuration is preferred since it simplifies the upgrades to the new
versions.
If needed, please read more about the configuration approach to be used on
${__site}/en/latest/configuration/.
MESSAGE
question-text "Would you like to apply a global configuration? (y/n) "
read answer
if [[ ${answer} == "y" ]]; then
info-text "Applying global configuration..."
else
cat <<MESSAGE
You've decided to stay with local configurations. Great!
Now you have to follow some rules:
1. if you want to acquire existing local repository
git-elegant acquire-repository
2. if you need to clone a repository
git-elegant clone-repository
3. if you need to create a new repository
git-elegant init-repository
MESSAGE
exit 0
fi
fi
basics-configuration --global -- user_name user_email core_editor
standards-configuration --global \
core_comment \
apply_whitespace \
Expand Down
2 changes: 1 addition & 1 deletion libexec/git-elegant-acquire-repository
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ MESSAGE

default() {
source ${BINS}/plugins/configuration
if [[ $(git config ${acquired[0]}) == ${acquired[1]} ]]; then
if $(is-acquired) ; then
basics-configuration --local user_name user_email
aliases-removing --local
else
Expand Down
31 changes: 28 additions & 3 deletions libexec/plugins/configuration
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,33 @@ __ask_question() {
}

basics-configuration() {
# usage: interactive-configuration <--global | --local> <name> ...
# usage: interactive-configuration <--global | --local> <name> ... -- <name if not set> ...
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
Expand Down Expand Up @@ -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
}
4 changes: 3 additions & 1 deletion tests/addons-read.bash
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""
}

Expand Down
23 changes: 23 additions & 0 deletions tests/git-elegant-acquire-git.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand All @@ -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? {[email protected]}: " ]]
Expand Down Expand Up @@ -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}." ]]
}
9 changes: 8 additions & 1 deletion workflows
Original file line number Diff line number Diff line change
Expand Up @@ -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 [email protected]
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
}
Expand Down

0 comments on commit 0d70a6e

Please sign in to comment.