Skip to content

Commit

Permalink
PostmanCli
Browse files Browse the repository at this point in the history
Add new command PostmanCli able to pull/push postman collections.
Better configuration loading management.

# Breaking changes

N/A

# Bug fixes

- fixed regression - global options passed to the command was not
  taken into account(--verbose, -no-color, --log-level, ...)

# Binaries changes

- new binary PostmanCli able to pull/push postman collections
- all binaries are using
  - shopt -s lastpipe from bash-tools-framework headers change
  - bash-tools configuration loading improved with default
    configuration automatically created if missing from a template
  - POSTMAN_API_KEY configuration is automatically added to
    ~/.bash-tools/.env if missing

# Updated Bash tools functions

N/A

# New Bash tools functions

- BashTools::Conf::requireLoad

# Documentation

- [Postman documentation](src/Postman/Readme.md)

# Validation/Tooling

- bats headers now loads bash tools framework mandatory headers
  • Loading branch information
fchastanet committed Jan 5, 2024
1 parent ddba2b5 commit 66e9267
Show file tree
Hide file tree
Showing 119 changed files with 7,943 additions and 502 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ package*.json
yarn.lock

commit-msg.md

bin/test
15 changes: 13 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ repos:
- id: end-of-file-fixer
exclude: |
(?x)(
.svg$
.svg$|
^src\/Postman\/Model\/testsData\/pullMode\/GithubAPI\/notWritableFile.json$
)
- id: trailing-whitespace
- id: check-executables-have-shebangs
Expand All @@ -34,13 +35,23 @@ repos:
(?x)^(
conf\/.vscode\/settings.json|
.vscode\/settings.json|
.vscode\/launch.json
.vscode\/launch.json|
src\/Postman\/Collection\/testsData\/postmanCollections_invalidJsonFile.json|
src\/Postman\/Model\/testsData\/pushMode\/GithubAPI\/notValidJsonFile.json|
src\/Postman\/Model\/testsData\/getCollectionInvalid.json|
src\/Postman\/Model\/testsData\/pullMode\/GithubAPI\/notWritableFile.json
)$
- repo: https://github.com/pre-commit/mirrors-prettier
rev: v3.1.0
hooks:
- id: prettier
exclude: |
(?x)^(
src\/Postman\/Collection\/testsData\/postmanCollections_invalidJsonFile.json|
src\/Postman\/Model\/testsData\/getCollectionInvalid.json|
src\/Postman\/Model\/testsData\/pushMode\/GithubAPI\/notValidJsonFile.json
)$
- repo: https://github.com/fchastanet/jumanjihouse-pre-commit-hooks
rev: 3.0.2
Expand Down
7 changes: 7 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@
"name": "Bash-Debug (mysql2puml)",
"program": "bin/mysql2puml",
"args": ["src/_binaries/DbImport/testsData/dump.sql"]
},
{
"type": "bashdb",
"request": "launch",
"name": "Bash-Debug (postmanCli)",
"program": "bin/postmanCli",
"args": ["export", "--help"]
}
]
}
167 changes: 139 additions & 28 deletions bin/cli
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,16 @@ set -o errexit
# Command Substitution can inherit errexit option since bash v4.4
shopt -s inherit_errexit || true

# if set, and job control is not active, the shell runs the last command
# of a pipeline not executed in the background in the current shell
# environment.
shopt -s lastpipe

# a log is generated when a command fails
set -o errtrace

# use nullglob so that (file*.php) will return an empty array if no file matches the wildcard
# use nullglob so that (file*.php) will return an empty array if no file
# matches the wildcard
shopt -s nullglob

# ensure regexp are interpreted without accentuated characters
Expand Down Expand Up @@ -467,10 +473,6 @@ Env::requireLoad() {
if [[ -f "${FRAMEWORK_ROOT_DIR}/.framework-config" ]]; then
configFiles+=("${FRAMEWORK_ROOT_DIR}/.framework-config")
fi
if [[ -n "${optionBashFrameworkConfig}" && -f "${optionBashFrameworkConfig}" ]]; then
# shellcheck disable=SC2034
configFiles+=("${optionBashFrameworkConfig}")
fi
configFiles+=("${optionEnvFiles[@]}")
configFiles+=("${defaultFiles[@]}")

Expand All @@ -483,6 +485,14 @@ Env::requireLoad() {
done
}

# @description create a temp file using default TMPDIR variable
# initialized in _includes/_commonHeader.sh
# @env TMPDIR String (default value /tmp)
# @arg $1 templateName:String template name to use(optional)
Framework::createTempFile() {
mktemp -p "${TMPDIR:-/tmp}" -t "${1:-}.XXXXXXXXXXXX"
}

# @description Log namespace provides 2 kind of functions
# - Log::display* allows to display given message with
# given display level
Expand Down Expand Up @@ -690,6 +700,96 @@ UI::theme() {
fi
}

bashToolsDefaultConfigTemplate="${bashToolsDefaultConfigTemplate:-$(
cat <<'EOF'
# shellcheck disable=SC2034
# Default settings
# you can override these settings by creating ${HOME}/.bash-tools/.env file
###
### LOG Level
### minimum level of the messages that will be logged into LOG_FILE
###
### 0: NO LOG
### 1: ERROR
### 2: WARNING
### 3: INFO
### 4: DEBUG
###
BASH_FRAMEWORK_LOG_LEVEL=${BASH_FRAMEWORK_LOG_LEVEL:-0}
###
### DISPLAY Level
### minimum level of the messages that will be displayed on screen
###
### 0: NO LOG
### 1: ERROR
### 2: WARNING
### 3: INFO
### 4: DEBUG
###
BASH_FRAMEWORK_DISPLAY_LEVEL=${BASH_FRAMEWORK_DISPLAY_LEVEL:-3}
###
### Log to file
###
### all log messages will be redirected to log file specified
### this same path will be used inside and outside of the container
###
BASH_FRAMEWORK_LOG_FILE=${BASH_FRAMEWORK_LOG_FILE:-${FRAMEWORK_ROOT_DIR}/logs/bash.log}
# absolute directory containing db import sql dumps
DB_IMPORT_DUMP_DIR=${DB_IMPORT_DUMP_DIR:-${HOME}/.bash-tools/dbImportDumps}
# garbage collect all files for which modification is greater than eg: 30 days (+30)
# each time an existing file is used by dbImport/dbImportTable
# the file modification time is set to now
DB_IMPORT_GARBAGE_COLLECT_DAYS=${DB_IMPORT_GARBAGE_COLLECT_DAYS:-+30}
# absolute directory containing dbScripts used by dbScriptAllDatabases
SCRIPTS_FOLDER=${SCRIPTS_FOLDER:-${HOME}/.bash-tools/conf/dbScripts}
# -----------------------------------------------------
# AWS Parameters
# -----------------------------------------------------
S3_BASE_URL=${S3_BASE_URL:-}
# -----------------------------------------------------
# Postman Parameters
# -----------------------------------------------------
POSTMAN_API_KEY=
EOF
)}"

# @description loads ~/.bash-tools/.env if available
# if not creates it from a default template
# else check if new options need to be added
BashTools::Conf::requireLoad() {
local envFile="${HOME}/.bash-tools/.env"
if [[ ! -f "${envFile}" ]]; then
mkdir -p "${HOME}/.bash-tools"
(
echo "#!/usr/bin/env bash"
echo "${bashToolsDefaultConfigTemplate}"
) >"${envFile}"
Log::displayInfo "Configuration file '${envFile}' created"
else
if ! grep -q '^POSTMAN_API_KEY=' "${envFile}"; then
(
echo '# -----------------------------------------------------'
echo '# Postman Parameters'
echo '# -----------------------------------------------------'
echo 'POSTMAN_API_KEY='
) >>"${envFile}"
fi
fi
# shellcheck source=/conf/.env
source "${envFile}" || {
Log::displayError "impossible to load '${envFile}'"
exit 1
}
}

# @description ensure COMMAND_BIN_DIR env var is set
# and PATH correctly prepared
# @noargs
Expand Down Expand Up @@ -921,6 +1021,7 @@ FRAMEWORK_BIN_DIR="${FRAMEWORK_ROOT_DIR}/bin"
FRAMEWORK_VENDOR_DIR="${FRAMEWORK_ROOT_DIR}/vendor"
FRAMEWORK_VENDOR_BIN_DIR="${FRAMEWORK_ROOT_DIR}/vendor/bin"

# @require BashTools::Conf::requireLoad
if [[ -f "${HOME}/.bash-tools/.env" ]]; then
export BASH_FRAMEWORK_ENV_FILES=("${HOME}/.bash-tools/.env")
fi
Expand All @@ -929,6 +1030,7 @@ Env::requireLoad
UI::requireTheme
Linux::requireRealpathCommand
Log::requireLoad
BashTools::Conf::requireLoad
Compiler::Facade::requireCommandBinDir

# @require Compiler::Facade::requireCommandBinDir
Expand Down Expand Up @@ -1010,21 +1112,21 @@ optionEnvFileCallback() {
optionInfoVerboseCallback() {
BASH_FRAMEWORK_ARGS_VERBOSE_OPTION='--verbose'
BASH_FRAMEWORK_ARGS_VERBOSE=${__VERBOSE_LEVEL_INFO}
BASH_FRAMEWORK_DISPLAY_LEVEL=${__LEVEL_INFO}
echo "BASH_FRAMEWORK_DISPLAY_LEVEL=${__LEVEL_INFO}" >> "${overrideEnvFile}"
}

# shellcheck disable=SC2317 # if function is overridden
optionDebugVerboseCallback() {
BASH_FRAMEWORK_ARGS_VERBOSE_OPTION='-vv'
BASH_FRAMEWORK_ARGS_VERBOSE=${__VERBOSE_LEVEL_DEBUG}
BASH_FRAMEWORK_DISPLAY_LEVEL=${__LEVEL_DEBUG}
echo "BASH_FRAMEWORK_DISPLAY_LEVEL=${__LEVEL_DEBUG}" >> "${overrideEnvFile}"
}

# shellcheck disable=SC2317 # if function is overridden
optionTraceVerboseCallback() {
BASH_FRAMEWORK_ARGS_VERBOSE_OPTION='-vvv'
BASH_FRAMEWORK_ARGS_VERBOSE=${__VERBOSE_LEVEL_TRACE}
BASH_FRAMEWORK_DISPLAY_LEVEL=${__LEVEL_DEBUG}
echo "BASH_FRAMEWORK_DISPLAY_LEVEL=${__LEVEL_DEBUG}" >> "${overrideEnvFile}"
}

getLevel() {
Expand All @@ -1033,10 +1135,10 @@ getLevel() {
OFF)
echo "${__LEVEL_OFF}"
;;
ERROR)
ERR | ERROR)
echo "${__LEVEL_ERROR}"
;;
WARNING)
WARN | WARNING)
echo "${__LEVEL_WARNING}"
;;
INFO)
Expand All @@ -1057,7 +1159,7 @@ getVerboseLevel() {
OFF)
echo "${__VERBOSE_LEVEL_OFF}"
;;
ERROR | WARNING | INFO)
ERR | ERROR | WARN | WARNING | INFO)
echo "${__VERBOSE_LEVEL_INFO}"
;;
DEBUG)
Expand All @@ -1079,7 +1181,7 @@ optionDisplayLevelCallback() {
logLevel="$(getLevel "${level}")"
verboseLevel="$(getVerboseLevel "${level}")"
BASH_FRAMEWORK_ARGS_VERBOSE=${verboseLevel}
BASH_FRAMEWORK_DISPLAY_LEVEL=${logLevel}
echo "BASH_FRAMEWORK_DISPLAY_LEVEL=${logLevel}" >> "${overrideEnvFile}"
}

# shellcheck disable=SC2317 # if function is overridden
Expand All @@ -1089,18 +1191,18 @@ optionLogLevelCallback() {
logLevel="$(getLevel "${level}")"
verboseLevel="$(getVerboseLevel "${level}")"
BASH_FRAMEWORK_ARGS_VERBOSE=${verboseLevel}
BASH_FRAMEWORK_LOG_LEVEL=${logLevel}
echo "BASH_FRAMEWORK_LOG_LEVEL=${logLevel}" >> "${overrideEnvFile}"
}

# shellcheck disable=SC2317 # if function is overridden
optionLogFileCallback() {
local logFile="$2"
BASH_FRAMEWORK_LOG_FILE="${logFile}"
echo "BASH_FRAMEWORK_LOG_FILE='${logFile}'" >> "${overrideEnvFile}"
}

# shellcheck disable=SC2317 # if function is overridden
optionQuietCallback() {
BASH_FRAMEWORK_QUIET_MODE=1
echo "BASH_FRAMEWORK_QUIET_MODE=1" >> "${overrideEnvFile}"
}

# shellcheck disable=SC2317 # if function is overridden
Expand Down Expand Up @@ -1167,15 +1269,24 @@ BASH_FRAMEWORK_LOG_FILE_MAX_ROTATION="${BASH_FRAMEWORK_LOG_FILE_MAX_ROTATION:-5}
EOF
)"

overrideEnvFile="$(Framework::createTempFile "overrideEnvFile")"

commandOptionParseFinished() {
# load default template framework config
# shellcheck disable=SC2034
defaultEnvFile="${PERSISTENT_TMPDIR}/.framework-config"
echo "${defaultFrameworkConfig}" > "${defaultEnvFile}"

Env::requireLoad "${defaultEnvFile}"
local -a files=("${defaultEnvFile}")
if [[ -f "${envFile}" ]]; then
files+=("${envFile}")
fi
# shellcheck disable=SC2154
if [[ -f "${optionBashFrameworkConfig}" ]]; then
files+=("${optionBashFrameworkConfig}")
fi
files+=("${overrideEnvFile}")
Env::requireLoad "${files[@]}"
Log::requireLoad

# shellcheck disable=SC2154
if [[ "${optionConfig}" = "1" ]]; then
displayConfig
fi
Expand Down Expand Up @@ -1386,15 +1497,15 @@ cliCommand() {
updateArgListQuietCallback "${options_parse_arg}"
;;
# Option 12/14
# Option optionLogLevel --log-level variableType String min 0 max 1 authorizedValues 'OFF|ERROR|WARNING|INFO|DEBUG|TRACE' regexp ''
# Option optionLogLevel --log-level variableType String min 0 max 1 authorizedValues 'OFF|ERR|ERROR|WARN|WARNING|INFO|DEBUG|TRACE' regexp ''
--log-level)
shift
if (($# == 0)); then
Log::displayError "Command ${SCRIPT_NAME} - Option ${options_parse_arg} - a value needs to be specified"
return 1
fi
if [[ ! "$1" =~ OFF|ERROR|WARNING|INFO|DEBUG|TRACE ]]; then
Log::displayError "Command ${SCRIPT_NAME} - Option ${options_parse_arg} - value '$1' is not part of authorized values(OFF|ERROR|WARNING|INFO|DEBUG|TRACE)"
if [[ ! "$1" =~ OFF|ERR|ERROR|WARN|WARNING|INFO|DEBUG|TRACE ]]; then
Log::displayError "Command ${SCRIPT_NAME} - Option ${options_parse_arg} - value '$1' is not part of authorized values(OFF|ERR|ERROR|WARN|WARNING|INFO|DEBUG|TRACE)"
return 1
fi
if ((options_parse_optionParsedCountOptionLogLevel >= 1)); then
Expand Down Expand Up @@ -1426,15 +1537,15 @@ cliCommand() {
updateArgListLogFileCallback "${options_parse_arg}" "${optionLogFile}"
;;
# Option 14/14
# Option optionDisplayLevel --display-level variableType String min 0 max 1 authorizedValues 'OFF|ERROR|WARNING|INFO|DEBUG|TRACE' regexp ''
# Option optionDisplayLevel --display-level variableType String min 0 max 1 authorizedValues 'OFF|ERR|ERROR|WARN|WARNING|INFO|DEBUG|TRACE' regexp ''
--display-level)
shift
if (($# == 0)); then
Log::displayError "Command ${SCRIPT_NAME} - Option ${options_parse_arg} - a value needs to be specified"
return 1
fi
if [[ ! "$1" =~ OFF|ERROR|WARNING|INFO|DEBUG|TRACE ]]; then
Log::displayError "Command ${SCRIPT_NAME} - Option ${options_parse_arg} - value '$1' is not part of authorized values(OFF|ERROR|WARNING|INFO|DEBUG|TRACE)"
if [[ ! "$1" =~ OFF|ERR|ERROR|WARN|WARNING|INFO|DEBUG|TRACE ]]; then
Log::displayError "Command ${SCRIPT_NAME} - Option ${options_parse_arg} - value '$1' is not part of authorized values(OFF|ERR|ERROR|WARN|WARNING|INFO|DEBUG|TRACE)"
return 1
fi
if ((options_parse_optionParsedCountOptionDisplayLevel >= 1)); then
Expand Down Expand Up @@ -1562,7 +1673,7 @@ cliCommand() {
# shellcheck disable=SC2054
helpArray=(choose\ color\ theme\ -\ default-force\ means\ colors\ will\ be\ produced\ even\ if\ command\ is\ piped)
echo -e " $(Array::wrap2 " " 76 4 "${helpArray[@]}")"
echo ' Default value: default'
echo " Default value: default"
echo ' Possible values: default|default-force|noColor'
echo -e " ${__HELP_OPTION_COLOR}--help${__HELP_NORMAL}, ${__HELP_OPTION_COLOR}-h${__HELP_NORMAL} {single}"
local -a helpArray
Expand All @@ -1584,7 +1695,7 @@ cliCommand() {
# shellcheck disable=SC2054
helpArray=(Set\ log\ level)
echo -e " $(Array::wrap2 " " 76 4 "${helpArray[@]}")"
echo ' Possible values: OFF|ERROR|WARNING|INFO|DEBUG|TRACE'
echo ' Possible values: OFF|ERR|ERROR|WARN|WARNING|INFO|DEBUG|TRACE'
echo -e " ${__HELP_OPTION_COLOR}--log-file <String>${__HELP_NORMAL} {single}"
local -a helpArray
# shellcheck disable=SC2054
Expand All @@ -1595,7 +1706,7 @@ cliCommand() {
# shellcheck disable=SC2054
helpArray=(set\ display\ level)
echo -e " $(Array::wrap2 " " 76 4 "${helpArray[@]}")"
echo ' Possible values: OFF|ERROR|WARNING|INFO|DEBUG|TRACE'
echo ' Possible values: OFF|ERR|ERROR|WARN|WARNING|INFO|DEBUG|TRACE'
echo -e """
${__HELP_TITLE}AVAILABLE PROFILES (from ${PROFILES_DIR})${__HELP_NORMAL}
This list can be overridden in ${HOME_PROFILES_DIR}
Expand Down
Loading

0 comments on commit 66e9267

Please sign in to comment.