Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ansible): Add Ansible section #1283

Merged
merged 9 commits into from Nov 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/config/prompt.md
Expand Up @@ -74,6 +74,7 @@ SPACESHIP_PROMPT_ORDER=(
dotnet # .NET section
ocaml # OCaml section
kubectl # Kubectl context section
ansible # Ansible section
terraform # Terraform workspace section
pulumi # Pulumi stack section
ibmcloud # IBM Cloud section
Expand Down
6 changes: 6 additions & 0 deletions docs/registry/internal.json
Expand Up @@ -324,6 +324,12 @@
"url": "https://spaceship-prompt.sh/sections/pulumi",
"description": "The current Pulumi stack.",
"internal": true
},
{
"name": "ansible",
"url": "https://spaceship-prompt.sh/sections/ansible",
"description": "The current Ansible version.",
"internal": true
}
]
}
24 changes: 24 additions & 0 deletions docs/sections/ansible.md
@@ -0,0 +1,24 @@
# Ansible (`ansible`)

!!! important "This section is rendered asynchronously by default"

!!! info
[**Ansible**](https://www.ansible.com/) is a suite of software tools that enables infrastructure as code.

The `ansible` section shows Ansible version.

This section is only displayed when you have a `ansible` command available and:

* Upsearch finds a `ansible.cfg` or `.ansible.cfg` file
* Current directory contains a file with `.yml`, `.yaml` extensions containing ansible playbooks.

## Options

| Variable | Default | Meaning |
| :------------------------ | :--------------------------------: | ----------------------------------- |
| `SPACESHIP_ANSIBLE_SHOW` | `true` | Show section |
| `SPACESHIP_ANSIBLE_ASYNC` | `true` | Render section asynchronously |
| `SPACESHIP_ANSIBLE_PREFIX` | `$SPACESHIP_PROMPT_DEFAULT_PREFIX` | Section's prefix |
| `SPACESHIP_ANSIBLE_SUFFIX` | `$SPACESHIP_PROMPT_DEFAULT_SUFFIX` | Section's suffix |
| `SPACESHIP_ANSIBLE_SYMBOL` | `🅐 ` | Symbol displayed before the section |
| `SPACESHIP_ANSIBLE_COLOR` | `white` | Section's color |
1 change: 1 addition & 0 deletions mkdocs.yml
Expand Up @@ -91,6 +91,7 @@ nav:
- .NET (dotnet): sections/dotnet.md
- OCaml (ocaml): sections/ocaml.md
- Kubernetes (kubectl): sections/kubectl.md
- Ansible (Ansible): sections/ansible.md
- Terraform (terraform): sections/terraform.md
- Pulumi (pulumi): sections/pulumi.md
- IBM Cloud (ibmcloud): sections/ibmcloud.md
Expand Down
53 changes: 53 additions & 0 deletions sections/ansible.zsh
@@ -0,0 +1,53 @@
#
# Ansible
#
# Ansible is a suite of software tools that enables infrastructure as code.
# Link: https://docs.ansible.com/ansible/latest/index.html

# ------------------------------------------------------------------------------
# Configuration
# ------------------------------------------------------------------------------

SPACESHIP_ANSIBLE_SHOW="${SPACESHIP_ANSIBLE_SHOW=true}"
SPACESHIP_ANSIBLE_ASYNC="${SPACESHIP_ANSIBLE_ASYNC=true}"
SPACESHIP_ANSIBLE_PREFIX="${SPACESHIP_ANSIBLE_PREFIX="$SPACESHIP_PROMPT_DEFAULT_PREFIX"}"
SPACESHIP_ANSIBLE_SUFFIX="${SPACESHIP_ANSIBLE_SUFFIX="$SPACESHIP_PROMPT_DEFAULT_SUFFIX"}"
SPACESHIP_ANSIBLE_SYMBOL="${SPACESHIP_ANSIBLE_SYMBOL="🅐 "}"
SPACESHIP_ANSIBLE_COLOR="${SPACESHIP_ANSIBLE_COLOR="white"}"

# ------------------------------------------------------------------------------
# Section
# ------------------------------------------------------------------------------

spaceship_ansible() {
[[ $SPACESHIP_ANSIBLE_SHOW == false ]] && return

# Check if ansible is installed
spaceship::exists ansible || return

# Show ansible section only when there are ansible-specific files in current
# working directory.
# Here glob qualifiers are used to check if files with specific extension are
# present in directory. Read more about them here:
# https://zsh.sourceforge.net/Doc/Release/Expansion.html
local ansible_configs="$(spaceship::upsearch ansible.cfg .ansible.cfg)"
local yaml_files="$(echo ?(*.yml|*.yaml)([1]N^/))"
local detected_playbooks

if [[ -n "$yaml_files" ]]; then
detected_playbooks="$(spaceship::grep -oE "tasks|hosts|roles" $yaml_files)"
fi

[[ -n "$ansible_configs" || -n "$detected_playbooks" ]] || return

# Retrieve ansible version
local ansible_version=$(ansible --version | head -1 | spaceship::grep -oE '[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]')

# Display ansible section
spaceship::section \
--color "$SPACESHIP_ANSIBLE_COLOR" \
--prefix "$SPACESHIP_ANSIBLE_PREFIX" \
--suffix "$SPACESHIP_ANSIBLE_SUFFIX" \
--symbol "$SPACESHIP_ANSIBLE_SYMBOL" \
"v$ansible_version"
}
1 change: 1 addition & 0 deletions spaceship.zsh
Expand Up @@ -76,6 +76,7 @@ if [ -z "$SPACESHIP_PROMPT_ORDER" ]; then
dotnet # .NET section
ocaml # OCaml section
kubectl # Kubectl context section
ansible # Ansible section
terraform # Terraform workspace section
pulumi # Pulumi stack section
ibmcloud # IBM Cloud section
Expand Down
108 changes: 108 additions & 0 deletions tests/ansible.test.zsh
@@ -0,0 +1,108 @@
#!/usr/bin/env zsh

# Required for shunit2 to run correctly
setopt shwordsplit
SHUNIT_PARENT=$0

# ------------------------------------------------------------------------------
# SHUNIT2 HOOKS
# ------------------------------------------------------------------------------

oneTimeSetUp() {
export TERM="xterm-256color"
export PATH=$PWD/tests/stubs:$PATH

ANSIBLE_VERSION="2.13.5"

SPACESHIP_PROMPT_ASYNC=false
SPACESHIP_PROMPT_FIRST_PREFIX_SHOW=true
SPACESHIP_PROMPT_ADD_NEWLINE=false
SPACESHIP_PROMPT_ORDER=(ansible)

source "spaceship.zsh"
}

setUp() {
SPACESHIP_ANSIBLE_SHOW="true"
SPACESHIP_ANSIBLE_PREFIX="via "
SPACESHIP_ANSIBLE_SUFFIX=""
SPACESHIP_ANSIBLE_SYMBOL="🅐 "
SPACESHIP_ANSIBLE_COLOR="white"

cd $SHUNIT_TMPDIR
}

oneTimeTearDown() {
unset SPACESHIP_PROMPT_FIRST_PREFIX_SHOW
unset SPACESHIP_PROMPT_ADD_NEWLINE
unset SPACESHIP_PROMPT_ORDER
}

tearDown() {
unset SPACESHIP_ANSIBLE_SHOW
unset SPACESHIP_ANSIBLE_PREFIX
unset SPACESHIP_ANSIBLE_SUFFIX
unset SPACESHIP_ANSIBLE_SYMBOL
unset SPACESHIP_ANSIBLE_COLOR
}

# ------------------------------------------------------------------------------
# TEST CASES
# ------------------------------------------------------------------------------

test_ansible_no_files() {
local no_files_expected=""
local no_files_actual="$(spaceship::testkit::render_prompt)"
assertEquals "should not render without files" "$no_files_expected" "$no_files_actual"
}

test_ansible_configs() {
FILES=(ansible.cfg .ansible.cfg)
for file in $FILES; do
touch $file
local expected=(
"%{%B%}$SPACESHIP_ANSIBLE_PREFIX%{%b%}"
"%{%B%F{$SPACESHIP_ANSIBLE_COLOR}%}"
"${SPACESHIP_ANSIBLE_SYMBOL}"
"v$ANSIBLE_VERSION%{%b%f%}"
)
local actual="$(spaceship::testkit::render_prompt)"
assertEquals "should render with $file" "${(j::)expected}" "$actual"
rm $file
done
}

test_ansible_playbooks() {
FILES=(playbook.yml playbook.yaml)
for file in $FILES; do
touch $file
echo "tasks: []" > $file
local expected=(
"%{%B%}$SPACESHIP_ANSIBLE_PREFIX%{%b%}"
"%{%B%F{$SPACESHIP_ANSIBLE_COLOR}%}"
"${SPACESHIP_ANSIBLE_SYMBOL}"
"v$ANSIBLE_VERSION%{%b%f%}"
)
local actual="$(spaceship::testkit::render_prompt)"
assertEquals "should render with $file" "${(j::)expected}" "$actual"
rm $file
done
}

test_ansible_yml_files() {
FILES=(regular.yml regular.yaml)
for file in $FILES; do
touch $file
local expected=""
local actual="$(spaceship::testkit::render_prompt)"
assertEquals "should render with $file" "$expected" "$actual"
rm $file
done
}

# ------------------------------------------------------------------------------
# SHUNIT2
# Run tests with shunit2
# ------------------------------------------------------------------------------

source tests/shunit2/shunit2
11 changes: 11 additions & 0 deletions tests/stubs/ansible
@@ -0,0 +1,11 @@
#!/usr/bin/env zsh

echo "ansible [core 2.13.5]"
echo " config file = None"
echo " configured module search path = ['path/to/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']"
echo " ansible python module location = /opt/homebrew/Cellar/ansible/6.5.0/libexec/lib/python3.10/site-packages/ansible"
echo " ansible collection location = path/to/.ansible/collections:/usr/share/ansible/collections"
echo " executable location = /opt/homebrew/bin/ansible"
echo " python version = 3.10.8 (main, Oct 13 2022, 09:48:40) [Clang 14.0.0 (clang-1400.0.29.102)]"
echo " jinja version = 3.1.2"
echo " libyaml = True"