From: Matthias Kruk Date: Mon, 14 Feb 2022 05:09:39 +0000 (+0900) Subject: include: Move files to foundry directory X-Git-Url: https://git.corax.cc/?a=commitdiff_plain;h=f8a2e66e0589884c16690ca36396708624d0b9a6;p=foundry include: Move files to foundry directory The toolbox modules that come with foundry are losely placed in the include directory, which goes against toolbox conventions. The modules should be placed in a subdirectory of include. This commit moves the foundry modules into the foundry subdirectory. --- diff --git a/include/context.sh b/include/context.sh deleted file mode 100644 index 0a5747b..0000000 --- a/include/context.sh +++ /dev/null @@ -1,189 +0,0 @@ -#!/bin/bash - -__init() { - if ! include "log" "array"; then - return 1 - fi - - declare -gxr __foundry_context_root="/var/lib/foundry/contexts" - - return 0 -} - -_foundry_context_new_id() { - local context_name="$1" - - local timestamp - local number - - if ! timestamp=$(date +"%Y%m%d-%H%M"); then - return 1 - fi - - if ! number=$(printf "%04d" "$((RANDOM % 10000))"); then - return 1 - fi - - echo "$context_name-$timestamp-$number" - return 0 -} - -foundry_context_new() { - local context_name="$1" - - local tid - local context_path - - if ! tid=$(_foundry_context_new_id "$context_name"); then - return 1 - fi - - context_path="$__foundry_context_root/$tid" - - if ! mkdir -p "$context_path/files" \ - "$context_path/logs"; then - return 1 - fi - - echo "$tid" - return 0 -} - -foundry_context_remove() { - local context="$1" - - local context_path - - context_path="$__foundry_context_root/$context" - - if [[ -z "$context" ]]; then - return 1 - fi - - if ! rm -r "$context_path"; then - return 1 - fi - - return 0 -} - -foundry_context_add_file() { - local context="$1" - local filetype="$2" - local file="$3" - - local context_path - local file_path - - context_path="$__foundry_context_root/$context" - file_path="$context_path/$filetype" - - if ! mkdir -p "$file_path"; then - return 1 - fi - - if ! cp "$file" "$file_path/."; then - return 1 - fi - - return 0 -} - -foundry_context_get_files() { - local context="$1" - local file_type="$2" - - local file_dir - local files - local file - - # file_type may be omitted to get all files - file_dir="$__foundry_context_root/$context/$file_type" - files=() - - while read -r file; do - local absolute - - if ! absolute=$(realpath "$file"); then - continue - fi - - files+=("$file") - done < <(find "$file_dir" -type f) - - array_to_lines "${files[@]}" - return 0 -} - -foundry_context_add_log() { - local context="$1" - local logtype="$2" - local log="$3" - - local logdir - - logdir="$__foundry_context_root/$context/logs/$logtype" - - if ! mkdir -p "$logdir"; then - return 1 - fi - - if ! cp "$log" "$logdir/."; then - return 1 - fi - - return 0 -} - -foundry_context_get_logs() { - local context="$1" - local logtype="$2" - - local logdir - local logs - local log - - logdir="$__foundry_context_root/$context/logs/$logtype" - logs=() - - while read -r log; do - local absolute - - if ! absolute=$(realpath "$log"); then - continue - fi - - logs+=("$absolute") - done < <(find "$logdir" -type f) - - array_to_lines "${logs[@]}" - return 0 -} - -foundry_context_log() { - local context="$1" - local logtype="$2" - local messages=("${@:3}") - - local logdir - local logname - - logdir="$__foundry_context_root/$context/logs/$logtype" - logname="$logdir/default.log" - - if ! mkdir -p "$logdir"; then - return 1 - fi - - if (( ${#messages[@]} > 0 )); then - if ! array_to_lines "${messages[@]}" >> "$logname"; then - return 1 - fi - else - if ! cat /dev/stdin >> "$logname"; then - return 1 - fi - fi - - return 0 -} diff --git a/include/foundry/context.sh b/include/foundry/context.sh new file mode 100644 index 0000000..0a5747b --- /dev/null +++ b/include/foundry/context.sh @@ -0,0 +1,189 @@ +#!/bin/bash + +__init() { + if ! include "log" "array"; then + return 1 + fi + + declare -gxr __foundry_context_root="/var/lib/foundry/contexts" + + return 0 +} + +_foundry_context_new_id() { + local context_name="$1" + + local timestamp + local number + + if ! timestamp=$(date +"%Y%m%d-%H%M"); then + return 1 + fi + + if ! number=$(printf "%04d" "$((RANDOM % 10000))"); then + return 1 + fi + + echo "$context_name-$timestamp-$number" + return 0 +} + +foundry_context_new() { + local context_name="$1" + + local tid + local context_path + + if ! tid=$(_foundry_context_new_id "$context_name"); then + return 1 + fi + + context_path="$__foundry_context_root/$tid" + + if ! mkdir -p "$context_path/files" \ + "$context_path/logs"; then + return 1 + fi + + echo "$tid" + return 0 +} + +foundry_context_remove() { + local context="$1" + + local context_path + + context_path="$__foundry_context_root/$context" + + if [[ -z "$context" ]]; then + return 1 + fi + + if ! rm -r "$context_path"; then + return 1 + fi + + return 0 +} + +foundry_context_add_file() { + local context="$1" + local filetype="$2" + local file="$3" + + local context_path + local file_path + + context_path="$__foundry_context_root/$context" + file_path="$context_path/$filetype" + + if ! mkdir -p "$file_path"; then + return 1 + fi + + if ! cp "$file" "$file_path/."; then + return 1 + fi + + return 0 +} + +foundry_context_get_files() { + local context="$1" + local file_type="$2" + + local file_dir + local files + local file + + # file_type may be omitted to get all files + file_dir="$__foundry_context_root/$context/$file_type" + files=() + + while read -r file; do + local absolute + + if ! absolute=$(realpath "$file"); then + continue + fi + + files+=("$file") + done < <(find "$file_dir" -type f) + + array_to_lines "${files[@]}" + return 0 +} + +foundry_context_add_log() { + local context="$1" + local logtype="$2" + local log="$3" + + local logdir + + logdir="$__foundry_context_root/$context/logs/$logtype" + + if ! mkdir -p "$logdir"; then + return 1 + fi + + if ! cp "$log" "$logdir/."; then + return 1 + fi + + return 0 +} + +foundry_context_get_logs() { + local context="$1" + local logtype="$2" + + local logdir + local logs + local log + + logdir="$__foundry_context_root/$context/logs/$logtype" + logs=() + + while read -r log; do + local absolute + + if ! absolute=$(realpath "$log"); then + continue + fi + + logs+=("$absolute") + done < <(find "$logdir" -type f) + + array_to_lines "${logs[@]}" + return 0 +} + +foundry_context_log() { + local context="$1" + local logtype="$2" + local messages=("${@:3}") + + local logdir + local logname + + logdir="$__foundry_context_root/$context/logs/$logtype" + logname="$logdir/default.log" + + if ! mkdir -p "$logdir"; then + return 1 + fi + + if (( ${#messages[@]} > 0 )); then + if ! array_to_lines "${messages[@]}" >> "$logname"; then + return 1 + fi + else + if ! cat /dev/stdin >> "$logname"; then + return 1 + fi + fi + + return 0 +} diff --git a/include/foundry/msg.sh b/include/foundry/msg.sh new file mode 100644 index 0000000..d0f9b24 --- /dev/null +++ b/include/foundry/msg.sh @@ -0,0 +1,161 @@ +#!/bin/bash + +__init() { + local submodules + local deps + + submodules=( + "foundry/msg/artifact" + "foundry/msg/build" + "foundry/msg/commit" + "foundry/msg/dist" + "foundry/msg/merge" + "foundry/msg/sign" + "foundry/msg/test" + ) + + deps=( + "ipc" + "json" + ) + + if ! include "${submodules[@]}" "${deps[@]}"; then + return 1 + fi + + declare -gxir __foundry_msg_version=1 + + declare -gxir __foundry_msg_type_build=1 + declare -gxir __foundry_msg_type_commit=2 + declare -gxir __foundry_msg_type_dist=3 + declare -gxir __foundry_msg_type_merge=4 + declare -gxir __foundry_msg_type_sign=5 + declare -gxir __foundry_msg_type_test=6 + + declare -gxA __foundry_msg_typemap + + __foundry_msg_typemap["build"]="$__foundry_msg_type_build" + __foundry_msg_typemap["commit"]="$__foundry_msg_type_commit" + __foundry_msg_typemap["dist"]="$__foundry_msg_type_dist" + __foundry_msg_typemap["merge"]="$__foundry_msg_type_merge" + __foundry_msg_typemap["sign"]="$__foundry_msg_type_sign" + __foundry_msg_typemap["test"]="$__foundry_msg_type_test" + + return 0 +} + +foundry_msg_get_version() { + local msg="$1" + + local version + + if ! version=$(json_object_get "$msg" "version"); then + return 1 + fi + + echo "$version" + return 0 +} + +foundry_msg_get_type() { + local msg="$1" + + local type + + if ! type=$(json_object_get "$msg" "type"); then + return 1 + fi + + echo "$type" + return 0 +} + +foundry_msg_get_data() { + local msg="$1" + + local data + + if ! data=$(json_object_get "$msg" "data"); then + return 1 + fi + + echo "$data" + return 0 +} + +foundry_msg_get_data_field() { + local msg="$1" + local field="$2" + + local value + + if ! value=$(json_object_get "$msg" "data.$field"); then + return 1 + fi + + echo "$value" + return 0 +} + +_foundry_msg_version_supported() { + local basemsg="$1" + + local version + + if ! version=$(foundry_msg_get_version "$basemsg"); then + return 1 + fi + + if (( version != __foundry_msg_version )); then + return 1 + fi + + return 0 +} + +foundry_msg_from_ipc_msg() { + local ipc_msg="$1" + + local basemsg + local type + local typeno + local data + + if ! basemsg=$(ipc_msg_get_data "$ipc_msg"); then + return 255 + fi + + if ! _foundry_msg_version_supported "$basemsg"; then + return 255 + fi + + if ! type=$(foundry_msg_get_type "$basemsg") || + ! data=$(foundry_msg_get_data "$basemsg"); then + return 255 + fi + + if ! array_contains "$type" "${!__foundry_msg_typemap[@]}"; then + return 255 + fi + + typeno="${__foundry_msg_typemap[$type]}" + + echo "$data" + return "$typeno" +} + +foundry_msg_new() { + local type="$1" + local data="$2" + + local msg + + if ! msg=$(json_object "version" "i:$__foundry_msg_version" \ + "type" "s:$type" \ + "data" "o:$data"); then + return 1 + fi + + echo "$msg" + return 0 +} diff --git a/include/foundry/msg/artifact.sh b/include/foundry/msg/artifact.sh new file mode 100644 index 0000000..a737c74 --- /dev/null +++ b/include/foundry/msg/artifact.sh @@ -0,0 +1,50 @@ +#!/bin/bash + +__init() { + if ! include "json"; then + return 1 + fi + + return 0 +} + +foundry_msg_artifact_new() { + local uri="$1" + local checksum="$2" + + local artifact + + if ! artifact=$(json_object "uri" "$uri" \ + "checksum" "$checksum"); then + return 1 + fi + + echo "$artifact" + return 0 +} + +foundry_msg_artifact_get_uri() { + local artifact="$1" + + local uri + + if ! uri=$(jq -e -r ".uri" <<< "$artifact"); then + return 1 + fi + + echo "$uri" + return 0 +} + +foundry_msg_artifact_get_checksum() { + local artifact="$1" + + local checksum + + if ! checksum=$(jq -e -e ".checksum" <<< "$artifact"); then + return 1 + fi + + echo "$checksum" + return 0 +} diff --git a/include/foundry/msg/build.sh b/include/foundry/msg/build.sh new file mode 100644 index 0000000..fc08082 --- /dev/null +++ b/include/foundry/msg/build.sh @@ -0,0 +1,139 @@ +#!/bin/bash + +__init() { + if ! include "json" "foundry/msg/artifact"; then + return 1 + fi + + declare -gxr __foundry_msg_build_msgtype="build" + + return 0 +} + +foundry_msg_build_new() { + local context="$1" + local repository="$2" + local branch="$3" + local ref="$4" + local result="$5" + local -n __foundry_msg_build_new_artifacts="$6" + + local artifact_array + local json + local msg + + if ! artifact_array=$(json_array "${__foundry_msg_build_new_artifacts[@]}"); then + return 1 + fi + + if ! json=$(json_object "context" "$context" \ + "repository" "$repository" \ + "branch" "$branch" \ + "ref" "$ref" \ + "result" "$result" \ + "artifacts" "$artifact_array"); then + return 1 + fi + + if ! msg=$(foundry_msg_new "$__foundry_msg_build_msgtype" "$json"); then + return 1 + fi + + echo "$msg" + return 0 +} + +foundry_msg_build_get_context() { + local msg="$1" + + local context + + if ! context=$(foundry_msg_get_data_field "$msg" "context"); then + return 1 + fi + + echo "$context" + return 0 +} + +foundry_msg_build_get_repository() { + local msg="$1" + + local repository + + if ! repository=$(foundry_msg_get_data_field "$msg" "repository"); then + return 1 + fi + + echo "$repository" + return 0 +} + +foundry_msg_build_get_branch() { + local msg="$1" + + local branch + + if ! branch=$(foundry_msg_get_data_field "$msg" "branch"); then + return 1 + fi + + echo "$branch" + return 0 +} + +foundry_msg_build_get_ref() { + local msg="$1" + + local ref + + if ! ref=$(foundry_msg_get_data_field "$msg" "ref"); then + return 1 + fi + + echo "$ref" + return 0 +} + +foundry_msg_build_get_result() { + local msg="$1" + + local result + + if ! result=$(foundry_msg_get_data_field "$msg" "result"); then + return 1 + fi + + echo "$result" + return 0 +} + +foundry_msg_build_get_artifacts() { + local msg="$1" + + local query + local raw_artifacts + local artifacts + local checksum + local uri + + query='artifacts[] | "\(.checksum) \(.uri)"' + artifacts=() + + if ! raw_artifacts=$(foundry_msg_get_data_field "$msg" "$query"); then + return 1 + fi + + while read -r checksum uri; do + local artifact + + if ! artifact=$(foundry_msg_artifact_new "$uri" "$checksum"); then + return 1 + fi + + artifacts+=("$artifact") + done <<< "$raw_artifacts" + + array_to_lines "${artifacts[@]}" + return 0 +} diff --git a/include/foundry/msg/commit.sh b/include/foundry/msg/commit.sh new file mode 100644 index 0000000..e287a9f --- /dev/null +++ b/include/foundry/msg/commit.sh @@ -0,0 +1,72 @@ +#!/bin/bash + +__init() { + if ! include "json"; then + return 1 + fi + + declare -gxr __foundry_msg_commit_msgtype="commit" + + return 0 +} + +foundry_msg_commit_new() { + local repository="$1" + local branch="$2" + local ref="$3" + + local data + local msg + + if ! data=$(json_object "repository" "$repository" \ + "branch" "$branch" \ + "ref" "$ref"); then + return 1 + fi + + if ! msg=$(foundry_msg_new "$__foundry_msg_commit_msgtype" "$data"); then + return 1 + fi + + echo "$msg" + return 0 +} + +foundry_msg_commit_get_repository() { + local msg="$1" + + local repository + + if ! repository=$(foundry_msg_get_data_field "$msg" "repository"); then + return 1 + fi + + echo "$repository" + return 0 +} + +foundry_msg_commit_get_branch() { + local msg="$1" + + local branch + + if ! branch=$(foundry_msg_get_data_field "$msg" "branch"); then + return 1 + fi + + echo "$branch" + return 0 +} + +foundry_msg_commit_get_ref() { + local msg="$1" + + local ref + + if ! ref=$(foundry_msg_get_data_field "$msg" "ref"); then + return 1 + fi + + echo "$ref" + return 0 +} diff --git a/include/foundry/msg/dist.sh b/include/foundry/msg/dist.sh new file mode 100644 index 0000000..5ffa17d --- /dev/null +++ b/include/foundry/msg/dist.sh @@ -0,0 +1,124 @@ +#!/bin/bash + +__init() { + if ! include "json" "foundry/msg/artifact"; then + return 1 + fi + + declare -gxr __foundry_msg_dist_msgtype="dist" + + return 0 +} + +foundry_msg_dist_new() { + local repository="$1" + local branch="$2" + local ref="$3" + local distribution="$4" + local artifacts=("${@:5}") + + local artifacts_json + local json + local msg + + if ! artifacts_json=$(json_array "${artifacts[@]}"); then + return 1 + fi + + if ! json=$(json_object "repository" "$repository" \ + "branch" "$branch" \ + "ref" "$ref" \ + "distribution" "$distribution" \ + "artifacts" "$artifacts_json"); then + return 1 + fi + + if ! msg=$(foundry_msg_new "$__foundry_msg_dist_msgtype" "$json"); then + return 1 + fi + + echo "$msg" + return 0 +} + +foundry_msg_dist_get_repository() { + local msg="$1" + + local repository + + if ! repository=$(foundry_msg_get_data_field "$msg" "repository"); then + return 1 + fi + + echo "$repository" + return 0 +} + +foundry_msg_dist_get_branch() { + local msg="$1" + + local branch + + if ! branch=$(foundry_msg_get_data_field "$msg" "branch"); then + return 1 + fi + + echo "$branch" + return 0 +} + +foundry_msg_dist_get_ref() { + local msg="$1" + + local ref + + if ! ref=$(foundry_msg_get_data_field "$msg" "ref"); then + return 1 + fi + + echo "$ref" + return 0 +} + +foundry_msg_dist_get_distribution() { + local msg="$1" + + local distribution + + if ! distribution=$(foundry_msg_get_data_field "$msg" "distribution"); then + return 1 + fi + + echo "$distribution" + return 0 +} + +foundry_msg_dist_get_artifacts() { + local msg="$1" + + local query + local raw_artifacts + local artifacts + local checksum + local uri + + query='artifacts[] | "\(.checksum) \(.uri)"' + artifacts=() + + if ! raw_artifacts=$(foundry_msg_get_data_field "$msg" "$query"); then + return 1 + fi + + while read -r checksum uri; do + local artifact + + if ! artifact=$(foundry_msg_artifact_new "$uri" "$checksum"); then + return 1 + fi + + artifacts+=("$artifact") + done <<< "$raw_artifacts" + + array_to_lines "${artifacts[@]}" + return 0 +} diff --git a/include/foundry/msg/merge.sh b/include/foundry/msg/merge.sh new file mode 100644 index 0000000..3b64d61 --- /dev/null +++ b/include/foundry/msg/merge.sh @@ -0,0 +1,102 @@ +#!/bin/bash + +__init() { + if ! include "json"; then + return 1 + fi + + declare -gxr __foundry_msg_merge_msgtype="merge" + + return 0 +} + +foundry_msg_merge_new() { + local context="$1" + local repository="$2" + local srcbranch="$3" + local dstbranch="$4" + local status="$5" + + local json + local msg + + if ! json=$(json_object "context" "$context" \ + "repository" "$repository" \ + "srcbranch" "$srcbranch" \ + "dstbranch" "$dstbranch" \ + "status" "$status"); then + return 1 + fi + + if ! msg=$(foundry_msg_new "$__foundry_msg_merge_msgtype" "$json"); then + return 1 + fi + + echo "$msg" + return 0 +} + +foundry_msg_merge_get_context() { + local msg="$1" + + local context + + if ! context=$(foundry_msg_get_data_field "$msg" "context"); then + return 1 + fi + + echo "$context" + return 0 +} + +foundry_msg_merge_get_repository() { + local msg="$1" + + local repository + + if ! repository=$(foundry_msg_get_data_field "$msg" "repository"); then + return 1 + fi + + echo "$repository" + return 0 +} + +foundry_msg_merge_get_source_branch() { + local msg="$1" + + local srcbranch + + if ! srcbranch=$(foundry_msg_get_data_field "$msg" "srcbranch"); then + return 1 + fi + + echo "$srcbranch" + return 0 +} + +foundry_msg_merge_get_destination_branch() { + local msg="$1" + + local dstbranch + + if ! dstbranch=$(foundry_msg_get_data_field "$msg" "dstbranch"); then + return 1 + fi + + echo "$dstbranch" + return 0 +} + +foundry_msg_merge_get_status() { + local msg="$1" + + local status + + if ! status=$(foundry_msg_get_data_field "$msg" "status"); then + return 1 + fi + + echo "$status" + return 0 +} diff --git a/include/foundry/msg/sign.sh b/include/foundry/msg/sign.sh new file mode 100644 index 0000000..a9e75d0 --- /dev/null +++ b/include/foundry/msg/sign.sh @@ -0,0 +1,139 @@ +#!/bin/bash + +__init() { + if ! include "json" "foundry/msg/artifact"; then + return 1 + fi + + declare -gxr __foundry_msg_sign_msgtype="sign" + + return 0 +} + +foundry_msg_sign_new() { + local context="$1" + local key="$2" + local repository="$3" + local branch="$4" + local ref="$5" + local artifacts=("${@:6}") + + local artifacts_json + local json + local msg + + if ! artifacts_json=$(json_array "${artifacts[@]}"); then + return 1 + fi + + if ! json=$(json_object "context" "$context" \ + "key" "$key" \ + "repository" "$repository" \ + "branch" "$branch" \ + "ref" "$ref" \ + "artifacts" "$artifacts_json"); then + return 1 + fi + + if ! msg=$(foundry_msg_new "$__foundry_msg_sign_msgtype" "$json"); then + return 1 + fi + + echo "$msg" + return 0 +} + +foundry_msg_sign_get_context() { + local msg="$1" + + local context + + if ! context=$(foundry_msg_get_data_field "$msg" "context"); then + return 1 + fi + + echo "$context" + return 0 +} + +foundry_msg_sign_get_key() { + local msg="$1" + + local key + + if ! key=$(foundry_msg_get_data_field "$msg" "key"); then + return 1 + fi + + echo "$key" + return 0 +} + +foundry_msg_sign_get_repository() { + local msg="$1" + + local repository + + if ! repository=$(foundry_msg_get_data_field "$msg" "repository"); then + return 1 + fi + + echo "$repository" + return 0 +} + +foundry_msg_sign_get_branch() { + local msg="$1" + + local branch + + if ! branch=$(foundry_msg_get_data_field "$msg" "branch"); then + return 1 + fi + + echo "$branch" + return 0 +} + +foundry_msg_sign_get_ref() { + local msg="$1" + + local ref + + if ! ref=$(foundry_msg_get_data_field "$msg" "ref"); then + return 1 + fi + + echo "$ref" + return 0 +} + +foundry_msg_sign_get_artifacts() { + local msg="$1" + + local query + local raw_artifacts + local artifacts + local checksum + local uri + + query='artifacts[] | "\(.checksum) \(.uri)"' + artifacts=() + + if ! raw_artifacts=$(foundry_msg_get_data_field "$msg" "$query"); then + return 1 + fi + + while read -r checksum uri; do + local artifact + + if ! artifact=$(foundry_msg_artifact_new "$uri" "$checksum"); then + return 1 + fi + + artifacts+=("$artifact") + done <<< "$raw_artifacts" + + array_to_lines "${artifacts[@]}" + return 0 +} diff --git a/include/foundry/msg/test.sh b/include/foundry/msg/test.sh new file mode 100644 index 0000000..c382511 --- /dev/null +++ b/include/foundry/msg/test.sh @@ -0,0 +1,87 @@ +#!/bin/bash + +__init() { + if ! include "json"; then + return 1 + fi + + declare -gxr __foundry_msg_test_msgtype="test" + + return 0 +} + +foundry_msg_test_new() { + local context="$1" + local repository="$2" + local branch="$3" + local result="$4" + + local json + local msg + + if ! json=$(json_object "context" "$context" \ + "repository" "$repository" \ + "branch" "$branch" \ + "result" "$result"); then + return 1 + fi + + if ! msg=$(foundry_msg_new "$__foundry_msg_test_msgtype" "$json"); then + return 1 + fi + + echo "$msg" + return 0 +} + +foundry_msg_test_get_context() { + local msg="$1" + + local context + + if ! context=$(foundry_msg_get_data_field "$msg" "context"); then + return 1 + fi + + echo "$context" + return 0 +} + +foundry_msg_test_get_repository() { + local msg="$1" + + local repository + + if ! repository=$(foundry_msg_get_data_field "$msg" "repository"); then + return 1 + fi + + echo "$repository" + return 0 +} + +foundry_msg_test_get_branch() { + local msg="$1" + + local branch + + if ! branch=$(foundry_msg_get_data_field "$msg" "branch"); then + return 1 + fi + + echo "$branch" + return 0 +} + +foundry_msg_test_get_result() { + local msg="$1" + + local result + + if ! result=$(foundry_msg_get_data_field "$msg" "result"); then + return 1 + fi + + echo "$result" + return 0 +} diff --git a/include/msg.sh b/include/msg.sh deleted file mode 100644 index d0f9b24..0000000 --- a/include/msg.sh +++ /dev/null @@ -1,161 +0,0 @@ -#!/bin/bash - -__init() { - local submodules - local deps - - submodules=( - "foundry/msg/artifact" - "foundry/msg/build" - "foundry/msg/commit" - "foundry/msg/dist" - "foundry/msg/merge" - "foundry/msg/sign" - "foundry/msg/test" - ) - - deps=( - "ipc" - "json" - ) - - if ! include "${submodules[@]}" "${deps[@]}"; then - return 1 - fi - - declare -gxir __foundry_msg_version=1 - - declare -gxir __foundry_msg_type_build=1 - declare -gxir __foundry_msg_type_commit=2 - declare -gxir __foundry_msg_type_dist=3 - declare -gxir __foundry_msg_type_merge=4 - declare -gxir __foundry_msg_type_sign=5 - declare -gxir __foundry_msg_type_test=6 - - declare -gxA __foundry_msg_typemap - - __foundry_msg_typemap["build"]="$__foundry_msg_type_build" - __foundry_msg_typemap["commit"]="$__foundry_msg_type_commit" - __foundry_msg_typemap["dist"]="$__foundry_msg_type_dist" - __foundry_msg_typemap["merge"]="$__foundry_msg_type_merge" - __foundry_msg_typemap["sign"]="$__foundry_msg_type_sign" - __foundry_msg_typemap["test"]="$__foundry_msg_type_test" - - return 0 -} - -foundry_msg_get_version() { - local msg="$1" - - local version - - if ! version=$(json_object_get "$msg" "version"); then - return 1 - fi - - echo "$version" - return 0 -} - -foundry_msg_get_type() { - local msg="$1" - - local type - - if ! type=$(json_object_get "$msg" "type"); then - return 1 - fi - - echo "$type" - return 0 -} - -foundry_msg_get_data() { - local msg="$1" - - local data - - if ! data=$(json_object_get "$msg" "data"); then - return 1 - fi - - echo "$data" - return 0 -} - -foundry_msg_get_data_field() { - local msg="$1" - local field="$2" - - local value - - if ! value=$(json_object_get "$msg" "data.$field"); then - return 1 - fi - - echo "$value" - return 0 -} - -_foundry_msg_version_supported() { - local basemsg="$1" - - local version - - if ! version=$(foundry_msg_get_version "$basemsg"); then - return 1 - fi - - if (( version != __foundry_msg_version )); then - return 1 - fi - - return 0 -} - -foundry_msg_from_ipc_msg() { - local ipc_msg="$1" - - local basemsg - local type - local typeno - local data - - if ! basemsg=$(ipc_msg_get_data "$ipc_msg"); then - return 255 - fi - - if ! _foundry_msg_version_supported "$basemsg"; then - return 255 - fi - - if ! type=$(foundry_msg_get_type "$basemsg") || - ! data=$(foundry_msg_get_data "$basemsg"); then - return 255 - fi - - if ! array_contains "$type" "${!__foundry_msg_typemap[@]}"; then - return 255 - fi - - typeno="${__foundry_msg_typemap[$type]}" - - echo "$data" - return "$typeno" -} - -foundry_msg_new() { - local type="$1" - local data="$2" - - local msg - - if ! msg=$(json_object "version" "i:$__foundry_msg_version" \ - "type" "s:$type" \ - "data" "o:$data"); then - return 1 - fi - - echo "$msg" - return 0 -} diff --git a/include/msg/artifact.sh b/include/msg/artifact.sh deleted file mode 100644 index a737c74..0000000 --- a/include/msg/artifact.sh +++ /dev/null @@ -1,50 +0,0 @@ -#!/bin/bash - -__init() { - if ! include "json"; then - return 1 - fi - - return 0 -} - -foundry_msg_artifact_new() { - local uri="$1" - local checksum="$2" - - local artifact - - if ! artifact=$(json_object "uri" "$uri" \ - "checksum" "$checksum"); then - return 1 - fi - - echo "$artifact" - return 0 -} - -foundry_msg_artifact_get_uri() { - local artifact="$1" - - local uri - - if ! uri=$(jq -e -r ".uri" <<< "$artifact"); then - return 1 - fi - - echo "$uri" - return 0 -} - -foundry_msg_artifact_get_checksum() { - local artifact="$1" - - local checksum - - if ! checksum=$(jq -e -e ".checksum" <<< "$artifact"); then - return 1 - fi - - echo "$checksum" - return 0 -} diff --git a/include/msg/build.sh b/include/msg/build.sh deleted file mode 100644 index fc08082..0000000 --- a/include/msg/build.sh +++ /dev/null @@ -1,139 +0,0 @@ -#!/bin/bash - -__init() { - if ! include "json" "foundry/msg/artifact"; then - return 1 - fi - - declare -gxr __foundry_msg_build_msgtype="build" - - return 0 -} - -foundry_msg_build_new() { - local context="$1" - local repository="$2" - local branch="$3" - local ref="$4" - local result="$5" - local -n __foundry_msg_build_new_artifacts="$6" - - local artifact_array - local json - local msg - - if ! artifact_array=$(json_array "${__foundry_msg_build_new_artifacts[@]}"); then - return 1 - fi - - if ! json=$(json_object "context" "$context" \ - "repository" "$repository" \ - "branch" "$branch" \ - "ref" "$ref" \ - "result" "$result" \ - "artifacts" "$artifact_array"); then - return 1 - fi - - if ! msg=$(foundry_msg_new "$__foundry_msg_build_msgtype" "$json"); then - return 1 - fi - - echo "$msg" - return 0 -} - -foundry_msg_build_get_context() { - local msg="$1" - - local context - - if ! context=$(foundry_msg_get_data_field "$msg" "context"); then - return 1 - fi - - echo "$context" - return 0 -} - -foundry_msg_build_get_repository() { - local msg="$1" - - local repository - - if ! repository=$(foundry_msg_get_data_field "$msg" "repository"); then - return 1 - fi - - echo "$repository" - return 0 -} - -foundry_msg_build_get_branch() { - local msg="$1" - - local branch - - if ! branch=$(foundry_msg_get_data_field "$msg" "branch"); then - return 1 - fi - - echo "$branch" - return 0 -} - -foundry_msg_build_get_ref() { - local msg="$1" - - local ref - - if ! ref=$(foundry_msg_get_data_field "$msg" "ref"); then - return 1 - fi - - echo "$ref" - return 0 -} - -foundry_msg_build_get_result() { - local msg="$1" - - local result - - if ! result=$(foundry_msg_get_data_field "$msg" "result"); then - return 1 - fi - - echo "$result" - return 0 -} - -foundry_msg_build_get_artifacts() { - local msg="$1" - - local query - local raw_artifacts - local artifacts - local checksum - local uri - - query='artifacts[] | "\(.checksum) \(.uri)"' - artifacts=() - - if ! raw_artifacts=$(foundry_msg_get_data_field "$msg" "$query"); then - return 1 - fi - - while read -r checksum uri; do - local artifact - - if ! artifact=$(foundry_msg_artifact_new "$uri" "$checksum"); then - return 1 - fi - - artifacts+=("$artifact") - done <<< "$raw_artifacts" - - array_to_lines "${artifacts[@]}" - return 0 -} diff --git a/include/msg/commit.sh b/include/msg/commit.sh deleted file mode 100644 index e287a9f..0000000 --- a/include/msg/commit.sh +++ /dev/null @@ -1,72 +0,0 @@ -#!/bin/bash - -__init() { - if ! include "json"; then - return 1 - fi - - declare -gxr __foundry_msg_commit_msgtype="commit" - - return 0 -} - -foundry_msg_commit_new() { - local repository="$1" - local branch="$2" - local ref="$3" - - local data - local msg - - if ! data=$(json_object "repository" "$repository" \ - "branch" "$branch" \ - "ref" "$ref"); then - return 1 - fi - - if ! msg=$(foundry_msg_new "$__foundry_msg_commit_msgtype" "$data"); then - return 1 - fi - - echo "$msg" - return 0 -} - -foundry_msg_commit_get_repository() { - local msg="$1" - - local repository - - if ! repository=$(foundry_msg_get_data_field "$msg" "repository"); then - return 1 - fi - - echo "$repository" - return 0 -} - -foundry_msg_commit_get_branch() { - local msg="$1" - - local branch - - if ! branch=$(foundry_msg_get_data_field "$msg" "branch"); then - return 1 - fi - - echo "$branch" - return 0 -} - -foundry_msg_commit_get_ref() { - local msg="$1" - - local ref - - if ! ref=$(foundry_msg_get_data_field "$msg" "ref"); then - return 1 - fi - - echo "$ref" - return 0 -} diff --git a/include/msg/dist.sh b/include/msg/dist.sh deleted file mode 100644 index 5ffa17d..0000000 --- a/include/msg/dist.sh +++ /dev/null @@ -1,124 +0,0 @@ -#!/bin/bash - -__init() { - if ! include "json" "foundry/msg/artifact"; then - return 1 - fi - - declare -gxr __foundry_msg_dist_msgtype="dist" - - return 0 -} - -foundry_msg_dist_new() { - local repository="$1" - local branch="$2" - local ref="$3" - local distribution="$4" - local artifacts=("${@:5}") - - local artifacts_json - local json - local msg - - if ! artifacts_json=$(json_array "${artifacts[@]}"); then - return 1 - fi - - if ! json=$(json_object "repository" "$repository" \ - "branch" "$branch" \ - "ref" "$ref" \ - "distribution" "$distribution" \ - "artifacts" "$artifacts_json"); then - return 1 - fi - - if ! msg=$(foundry_msg_new "$__foundry_msg_dist_msgtype" "$json"); then - return 1 - fi - - echo "$msg" - return 0 -} - -foundry_msg_dist_get_repository() { - local msg="$1" - - local repository - - if ! repository=$(foundry_msg_get_data_field "$msg" "repository"); then - return 1 - fi - - echo "$repository" - return 0 -} - -foundry_msg_dist_get_branch() { - local msg="$1" - - local branch - - if ! branch=$(foundry_msg_get_data_field "$msg" "branch"); then - return 1 - fi - - echo "$branch" - return 0 -} - -foundry_msg_dist_get_ref() { - local msg="$1" - - local ref - - if ! ref=$(foundry_msg_get_data_field "$msg" "ref"); then - return 1 - fi - - echo "$ref" - return 0 -} - -foundry_msg_dist_get_distribution() { - local msg="$1" - - local distribution - - if ! distribution=$(foundry_msg_get_data_field "$msg" "distribution"); then - return 1 - fi - - echo "$distribution" - return 0 -} - -foundry_msg_dist_get_artifacts() { - local msg="$1" - - local query - local raw_artifacts - local artifacts - local checksum - local uri - - query='artifacts[] | "\(.checksum) \(.uri)"' - artifacts=() - - if ! raw_artifacts=$(foundry_msg_get_data_field "$msg" "$query"); then - return 1 - fi - - while read -r checksum uri; do - local artifact - - if ! artifact=$(foundry_msg_artifact_new "$uri" "$checksum"); then - return 1 - fi - - artifacts+=("$artifact") - done <<< "$raw_artifacts" - - array_to_lines "${artifacts[@]}" - return 0 -} diff --git a/include/msg/merge.sh b/include/msg/merge.sh deleted file mode 100644 index 3b64d61..0000000 --- a/include/msg/merge.sh +++ /dev/null @@ -1,102 +0,0 @@ -#!/bin/bash - -__init() { - if ! include "json"; then - return 1 - fi - - declare -gxr __foundry_msg_merge_msgtype="merge" - - return 0 -} - -foundry_msg_merge_new() { - local context="$1" - local repository="$2" - local srcbranch="$3" - local dstbranch="$4" - local status="$5" - - local json - local msg - - if ! json=$(json_object "context" "$context" \ - "repository" "$repository" \ - "srcbranch" "$srcbranch" \ - "dstbranch" "$dstbranch" \ - "status" "$status"); then - return 1 - fi - - if ! msg=$(foundry_msg_new "$__foundry_msg_merge_msgtype" "$json"); then - return 1 - fi - - echo "$msg" - return 0 -} - -foundry_msg_merge_get_context() { - local msg="$1" - - local context - - if ! context=$(foundry_msg_get_data_field "$msg" "context"); then - return 1 - fi - - echo "$context" - return 0 -} - -foundry_msg_merge_get_repository() { - local msg="$1" - - local repository - - if ! repository=$(foundry_msg_get_data_field "$msg" "repository"); then - return 1 - fi - - echo "$repository" - return 0 -} - -foundry_msg_merge_get_source_branch() { - local msg="$1" - - local srcbranch - - if ! srcbranch=$(foundry_msg_get_data_field "$msg" "srcbranch"); then - return 1 - fi - - echo "$srcbranch" - return 0 -} - -foundry_msg_merge_get_destination_branch() { - local msg="$1" - - local dstbranch - - if ! dstbranch=$(foundry_msg_get_data_field "$msg" "dstbranch"); then - return 1 - fi - - echo "$dstbranch" - return 0 -} - -foundry_msg_merge_get_status() { - local msg="$1" - - local status - - if ! status=$(foundry_msg_get_data_field "$msg" "status"); then - return 1 - fi - - echo "$status" - return 0 -} diff --git a/include/msg/sign.sh b/include/msg/sign.sh deleted file mode 100644 index a9e75d0..0000000 --- a/include/msg/sign.sh +++ /dev/null @@ -1,139 +0,0 @@ -#!/bin/bash - -__init() { - if ! include "json" "foundry/msg/artifact"; then - return 1 - fi - - declare -gxr __foundry_msg_sign_msgtype="sign" - - return 0 -} - -foundry_msg_sign_new() { - local context="$1" - local key="$2" - local repository="$3" - local branch="$4" - local ref="$5" - local artifacts=("${@:6}") - - local artifacts_json - local json - local msg - - if ! artifacts_json=$(json_array "${artifacts[@]}"); then - return 1 - fi - - if ! json=$(json_object "context" "$context" \ - "key" "$key" \ - "repository" "$repository" \ - "branch" "$branch" \ - "ref" "$ref" \ - "artifacts" "$artifacts_json"); then - return 1 - fi - - if ! msg=$(foundry_msg_new "$__foundry_msg_sign_msgtype" "$json"); then - return 1 - fi - - echo "$msg" - return 0 -} - -foundry_msg_sign_get_context() { - local msg="$1" - - local context - - if ! context=$(foundry_msg_get_data_field "$msg" "context"); then - return 1 - fi - - echo "$context" - return 0 -} - -foundry_msg_sign_get_key() { - local msg="$1" - - local key - - if ! key=$(foundry_msg_get_data_field "$msg" "key"); then - return 1 - fi - - echo "$key" - return 0 -} - -foundry_msg_sign_get_repository() { - local msg="$1" - - local repository - - if ! repository=$(foundry_msg_get_data_field "$msg" "repository"); then - return 1 - fi - - echo "$repository" - return 0 -} - -foundry_msg_sign_get_branch() { - local msg="$1" - - local branch - - if ! branch=$(foundry_msg_get_data_field "$msg" "branch"); then - return 1 - fi - - echo "$branch" - return 0 -} - -foundry_msg_sign_get_ref() { - local msg="$1" - - local ref - - if ! ref=$(foundry_msg_get_data_field "$msg" "ref"); then - return 1 - fi - - echo "$ref" - return 0 -} - -foundry_msg_sign_get_artifacts() { - local msg="$1" - - local query - local raw_artifacts - local artifacts - local checksum - local uri - - query='artifacts[] | "\(.checksum) \(.uri)"' - artifacts=() - - if ! raw_artifacts=$(foundry_msg_get_data_field "$msg" "$query"); then - return 1 - fi - - while read -r checksum uri; do - local artifact - - if ! artifact=$(foundry_msg_artifact_new "$uri" "$checksum"); then - return 1 - fi - - artifacts+=("$artifact") - done <<< "$raw_artifacts" - - array_to_lines "${artifacts[@]}" - return 0 -} diff --git a/include/msg/test.sh b/include/msg/test.sh deleted file mode 100644 index c382511..0000000 --- a/include/msg/test.sh +++ /dev/null @@ -1,87 +0,0 @@ -#!/bin/bash - -__init() { - if ! include "json"; then - return 1 - fi - - declare -gxr __foundry_msg_test_msgtype="test" - - return 0 -} - -foundry_msg_test_new() { - local context="$1" - local repository="$2" - local branch="$3" - local result="$4" - - local json - local msg - - if ! json=$(json_object "context" "$context" \ - "repository" "$repository" \ - "branch" "$branch" \ - "result" "$result"); then - return 1 - fi - - if ! msg=$(foundry_msg_new "$__foundry_msg_test_msgtype" "$json"); then - return 1 - fi - - echo "$msg" - return 0 -} - -foundry_msg_test_get_context() { - local msg="$1" - - local context - - if ! context=$(foundry_msg_get_data_field "$msg" "context"); then - return 1 - fi - - echo "$context" - return 0 -} - -foundry_msg_test_get_repository() { - local msg="$1" - - local repository - - if ! repository=$(foundry_msg_get_data_field "$msg" "repository"); then - return 1 - fi - - echo "$repository" - return 0 -} - -foundry_msg_test_get_branch() { - local msg="$1" - - local branch - - if ! branch=$(foundry_msg_get_data_field "$msg" "branch"); then - return 1 - fi - - echo "$branch" - return 0 -} - -foundry_msg_test_get_result() { - local msg="$1" - - local result - - if ! result=$(foundry_msg_get_data_field "$msg" "result"); then - return 1 - fi - - echo "$result" - return 0 -}