Skip to content

Commit

Permalink
Bash completion script fix (#900)
Browse files Browse the repository at this point in the history
Co-authored-by: Jayasimha Raghavan <[email protected]>
  • Loading branch information
jayasimha-raghavan-unskript and Jayasimha Raghavan authored Oct 19, 2023
1 parent f48eaea commit 9c61b44
Show file tree
Hide file tree
Showing 7 changed files with 430 additions and 165 deletions.
1 change: 1 addition & 0 deletions .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ disable=abstract-method,
C0303,
C0304,
C0301,
C0412,
E0203,
R1732

Expand Down
6 changes: 3 additions & 3 deletions README_extending_docker.md
Original file line number Diff line number Diff line change
Expand Up @@ -247,16 +247,16 @@ docker exec -it $CONTAINER_ID bash

For example, if you want to get Slack Notification, here is the command to follow -
```
add_notification.sh -c Slack -u https://hooks.slack.com/services/T12345/B12345/XXXXXXX --channel test-alerting
add_notification.sh -c Slack -u https://hooks.slack.com/services/T12345/B12345/XXXXXXX --channel-name test-alerting
```
> Here `-c` Option is used for creating a new Notification. Options are either Slack or SMTP (email)
> `-u` Option is the webhook URL of Slack
> `--channel` Channel to where the notification should be sent
> `--channel-name` Channel to where the notification should be sent
This snippet shows how to configure SMTP (email) Notification

```
./add_notification.sh -c SMTP -s smtp.server.com -u [email protected] -p <password_of_username> -t [email protected]
add_notification.sh -c SMTP -s smtp.server.com -u [email protected] -p <password_of_username> -t [email protected]
```
> Here `-s` Option is to specify the SMTP server
> `-u` Option is to specify the SMTP username. Note, you need to specify the username with the domain like [email protected]
Expand Down
9 changes: 7 additions & 2 deletions unskript-ctl/add_notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import os
import sys
import json
import yaml
from pathlib import Path
import subprocess

Expand All @@ -27,11 +28,15 @@
# Note, the earlier name we used was unskript_config.yaml.
if os.path.exists('/unskript/data/action/unskript_config.yaml') is True:
try:
os.makedirs(Path(GLOBAL_CONFIG_PATH).parent, exist_ok=True)
os.makedirs(Path(GLOBAL_CONFIG_PATH).parent, exist_ok=True)
Path('/unskript/data/action/unskript_config.yaml').rename(GLOBAL_CONFIG_PATH)
except:
pass

# Lets ensure the Global Config file exists in the
# path
if os.path.exists(GLOBAL_CONFIG_PATH) is False:
Path(GLOBAL_CONFIG_PATH).touch()

"""
This class define basic parser that shall be extended based on each notification
Expand Down Expand Up @@ -167,4 +172,4 @@ def read_notification(self, type):

if __name__ == "__main__":
"""Notifcation class does the create of Crud"""
Notification()
Notification()
96 changes: 80 additions & 16 deletions unskript-ctl/bash_completion_unskript_ctl.bash
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ _unskript-client-completion() {
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
connector_list=("aws" "k8s" "postgres" "mongodb" "elasticsearch" "vault" "ssh" "keycloak" "github" "redis")


# Find the absolute path of unskript-client.py
Expand All @@ -14,10 +15,12 @@ _unskript-client-completion() {

if [ -n "$unskript_client_script" ]; then
# Check if the script exists and save check names
/usr/bin/env python "$unskript_client_script" --save-check-names /tmp/checknames.txt
/usr/bin/env python "$unskript_client_script" -h > /tmp/allopts.txt
if [ -f "/tmp/checknames.txt" ]; then
opt2="$(cat /tmp/checknames.txt)"
if [ ! -f "/tmp/allopts.txt" ]; then
/usr/bin/env python "$unskript_client_script" -h > /tmp/allopts.txt
fi
if [ ! -f "/tmp/checknames.txt" ]; then
/usr/bin/env python "$unskript_client_script" --save-check-names /tmp/checknames.txt

fi
fi
# Define options with each option on a separate line using newline characters
Expand All @@ -27,58 +30,119 @@ _unskript-client-completion() {
case "${prev}" in
-rr|--run-runbook)
# Provide completion suggestions for runbook filenames
COMPREPLY=( $(compgen -f -- "${cur}") )
COMPREPLY=( $(compgen -f -- "${cur}" -o nospace) )
return 0
;;

-rc|--run-checks)
case ${prev} in
-rc|--run-checks)
case ${cur} in
-f)
COMPREPLY=( ${opt2} )
--check)
cur=${cur#--check}
opt2="$(cat /tmp/checknames.txt)"
COMPREPLY=( $(compgen -W "${opt2}" -o nospace) )
;;
*)
COMPREPLY=( $(compgen -W "--all | --type | --failed | --check" -- "${cur}") )
#COMPREPLY=( $(compgen -W "--all --type --failed --check" -- "${cur}" -o nospace) )
COMPREPLY=( $(compgen -W "--all --type --failed --check" -o nospace) )
;;
esac
return 0
;;
*)
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
COMPREPLY=( $(compgen -W "${opts}" "${cur}" -o nospace) )
;;

esac
return 0
;;

-lc|--list-checks)
# Provide completion suggestions for list-checks options
COMPREPLY=( $(compgen -W "--all | --type" -- "${cur}") )
COMPREPLY=( $(compgen -W "--all --type" -- "${cur}" -o nospace) )
return 0
;;

--credential-list)
# Provide completion suggestions for list-checks options
COMPREPLY=()
return 0
;;

-sa|--show-audit-trail)
# Provide completion suggestions for show-audit-trail options
COMPREPLY=( $(compgen -W "--all | --type | --execution_id" -- "${cur}") )
COMPREPLY=( $(compgen -W "--all --type --execution_id" -- "${cur}" -o nospace) )
return 0
;;

-dl|--display-failed-logs)
# Provide completion suggestions for display-failed-logs options
COMPREPLY=( $(compgen -W "--execution_id <EXECUTION_ID>" -- "${cur}") )
COMPREPLY=( $(compgen -W "--execution_id" -- "${cur}" -o nospace) )
return 0
;;

-cc|--create-credentials)
# Provide completion suggestions for create-credentials options
COMPREPLY=( $(compgen -W "--type creds_file_path" -- "${cur}") )
COMPREPLY=( $(compgen -W "--type" -- "${cur}" -o nospace) )
return 0
;;

*) # Default: Provide completion suggestions for global options
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}" -o nospace) )
return 0
;;
_cmd="${COMP_WORDS[COMP_CWORD-2]}"
if [ "${prev}" = "--check" ];
then
cur=${cur#--check}
opt2="$(grep -E "^${cur}" /tmp/checknames.txt)"
COMPREPLY=( $(compgen -W "${opt2}" -o nospace) )
compopt -o nospace
elif [ "${_cmd}" = "--check" ];
then
COMPREPLY=( $(compgen -W "--report" -o nospace) )
elif [ "${cur}" = "--report" ];
then
COMPREPLY=()
elif [[ "${_cmd}" = '-cc' || "${_cmd}" = '--create-credentials' ]];
then
COMPREPLY+=("-k8s <KUBECONFIG_FILE_PATH>")
elif [[ "${_cmd}" = '-rc' || "${_cmd}" = '--run-checks' || "${_cmd}" = '--list-checks' || "${_cmd}" = '-lc' ]];
then
if [ "${prev}" = "--type" ];
then
COMPREPLY=( $(compgen -W "aws k8s postgres mongodb elasticsearch vault ssh keycloak github redis" -o nospace) )
elif [[ "${prev}" = "--all" || "${prev}" = "--failed" ]];
then
COMPREPLY+=('--report')
fi
elif [[ "${_cmd}" = "--type" && "${COMP_WORDS[COMP_CWORD-3]}" = '-rc' || "${COMP_WORDS[COMP_CWORD-3]}" = '--run-checks' ]];
then
for item in "${connector_list[@]}";
do
if [ "$item" == "${prev}" ];
then
COMPREPLY+=("--report")
break
fi
done
elif [[ "${_cmd}" = '-sa' || "${_cmd}" = '--show-audit-trail' ]];
then
if [ "${prev}" = '--type' ];
then
COMPREPLY=( $(compgen -W "aws k8s postgres mongodb elasticsearch vault ssh keycloak github redis" -o nospace) )
elif [ "${prev}" = '--execution_id' ];
then
COMPREPLY=( $(compgen -W "<EXECUTION_ID>" -o nospace) )
fi
else
if [ "${#COMP_WORDS[@]}" != "2" ];
then
return 0
fi
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}" -o nospace) )
fi

return 0
;;
esac
}

Expand Down
2 changes: 1 addition & 1 deletion unskript-ctl/db_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ def get_checks_by_connector(connector_name: str, full_snippet: bool = False):
if connector_name.lower() != 'all' and not re.match(connector_name.lower(), s_connector):
continue
if full_snippet is False:
list_checks.append([s_connector.capitalize(), d.get('name'), d.get('uuid')])
list_checks.append([s_connector.capitalize(), d.get('name'), d.get('metadata').get('action_entry_function')])
else:
list_checks.append(d)

Expand Down
Loading

0 comments on commit 9c61b44

Please sign in to comment.