From 291c3212bde7fed7b647b1fc25b5f50145126ca3 Mon Sep 17 00:00:00 2001 From: Olivier Hartmann Date: Wed, 16 Jun 2021 15:12:32 +0200 Subject: [PATCH] Make From Git action local config agnostic (#2) * Extend load-remote action to work with local config or from a remote branch --- README.md | 21 ++++++++++--- pms | 94 ++++++++++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 96 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index e5fa18c..d035c68 100644 --- a/README.md +++ b/README.md @@ -86,7 +86,7 @@ pms init Help set up user and project configuration files if missing. -### "to disk" or "save" +### "to-disk" or "save" ``` pms to-disk @@ -107,15 +107,26 @@ Upload local collection and environment to Postman cloud. ### "from-git" or "load-remote" +*With* a `pms.config` configuration in your directory, retrieve collection +and environment files from a git `remote-repository` branch, and upload +them to Postman cloud. + ``` pms from-git pms load-remote ``` -Retrieve collection and environment files from a git `remote-repository` -master branch, and uploads to Postman cloud. This is meant as a lightweight -way to keep a collection up-to-date without using a local clone of the -repository. You still need a `pms.config` configuration, and `~/.pms`. +With or *without* a `pms.config` configuration in your directory, retrieve +configuration, collection and environment files from an explicitly stated +git remote branch given as arguments, and upload to Postman cloud. + +``` +pms from-git git@somehost.com:repo/project.git +pms load-remote git@somehost.com:repo/project.git main +``` + +This is meant as a lightweight way to keep a collection up-to-date without +using a local clone of the repository. You still need a `~/.pms`. # Note about environment diff --git a/pms b/pms index f0e7a2f..c32b084 100755 --- a/pms +++ b/pms @@ -30,10 +30,16 @@ usage() { echo "" echo "Project configuration file: $PROJECT_CONFIG_PATH" echo "User configuration file: $USER_CONFIG_PATH" + echo "" + echo "Example to load collection from a git repository:" + echo -e "\t$0 from-git git@somehost.com:repo/project.git" + echo -e "\t$0 from-git git@somehost.com:repo/project.git master" exit 1 } action="" +remoteName="" +remoteBranch="" while [[ "$#" -gt 0 ]]; do case "$1" in help) @@ -45,7 +51,18 @@ while [[ "$#" -gt 0 ]]; do save | to-disk) action="to-disk" ;; load-remote | from-git) - action="from-git" ;; + if [ $# -eq 1 ]; then + action="from-git-local-config" + else + action="from-git-remote-config" + remoteName="$2" + if [ $# -ge 3 ]; then + remoteBranch="$3" + shift + fi + shift + fi + ;; --) break ;; *) @@ -163,10 +180,12 @@ loadEnvironment() { } remoteGitArchive() { - echo "Retrieving from remote repository $REMOTE_REPOSITORY (branch $REMOTE_REPOSITORY_BRANCH)" local paths=("$@") + remote_repository="$1" + remote_repository_branch="$2" + local paths=("${@:3}") if [ ${#paths[@]} -gt 0 ]; then - git archive --remote="$REMOTE_REPOSITORY" "$REMOTE_REPOSITORY_BRANCH" "${paths[@]}" | tar -x + git archive --remote="$remote_repository" "$remote_repository_branch" "${paths[@]}" | tar -x fi } @@ -188,12 +207,44 @@ fromDisk() { fi } -fromGit() { +fromGitRemoteConfig() { + remote_repository="$1" + remote_repository_branch="${2:-master}" + + if [ -z "$remote_repository" ]; then + echo "No remote repository set." + exit 2 + fi + + echo "Retrieving from remote repository $remote_repository:$remote_repository_branch" + + pushd $(mktemp -d) >/dev/null + + remoteGitArchive "$remote_repository" "$remote_repository_branch" "$PROJECT_CONFIG_PATH" + readProjectConfig + + local paths=() + if [ "${COLLECTION_NAME}" ]; then + paths+=("${COLLECTION_FILE}") + fi + if [ "${ENVIRONMENT_NAME}" ]; then + paths+=("${ENVIRONMENT_FILE}") + fi + if [ ${#paths[@]} -eq 0 ]; then + echo "No collection or environment file to retrieve, skipping." + return 0 + fi + + remoteGitArchive "$remote_repository" "$remote_repository_branch" "${paths[@]}" + fromDisk + popd >/dev/null +} + +fromGitLocalConfig() { if [ -z "$REMOTE_REPOSITORY" ]; then echo "No '${PROJECT_CONFIG_REMOTE_REPOSITORY}' configuration set." exit 2 fi - if [ -z "$REMOTE_REPOSITORY_BRANCH" ]; then echo "No '${PROJECT_CONFIG_REMOTE_REPOSITORY_BRANCH}' configuration set." exit 2 @@ -211,9 +262,8 @@ fromGit() { return 0 fi - TMP_DIR=$(mktemp -d) - pushd "$TMP_DIR" >/dev/null - remoteGitArchive "${paths[@]}" + pushd $(mktemp -d) >/dev/null + remoteGitArchive "$REMOTE_REPOSITORY" "$REMOTE_REPOSITORY_BRANCH" "${paths[@]}" fromDisk popd >/dev/null } @@ -268,6 +318,7 @@ init() { echo "${USER_CONFIG_API_KEY}=${INIT_USER_API_KEY}" >> "${USER_CONFIG_PATH}" unset INIT_USER_API_KEY fi + if [ ! -f "${PROJECT_CONFIG_PATH}" ]; then echo "Creating project configuration file '${PROJECT_CONFIG_PATH}'" initProjectProperty "${PROJECT_CONFIG_NAME}" "Enter name, used for collection and environment files names by default" "Name: " @@ -291,14 +342,29 @@ init() { #region RUN PROGRAM -readUserConfig -readProjectConfig - case "$action" in init) init ;; - from-disk) fromDisk ;; - to-disk) toDisk ;; - from-git) fromGit ;; + + from-disk) + readUserConfig + readProjectConfig + fromDisk ;; + + to-disk) + readUserConfig + readProjectConfig + toDisk ;; + + from-git-local-config) + readUserConfig + readProjectConfig + fromGitLocalConfig ;; + + from-git-remote-config) + readUserConfig + fromGitRemoteConfig "$remoteName" "$remoteBranch" ;; + + *) usage ;; esac #endregion