From: Matthias Kruk Date: Sun, 4 Jul 2021 09:10:52 +0000 (+0900) Subject: include/msg: Add modules for foundry IPC message handling X-Git-Url: https://git.corax.cc/?a=commitdiff_plain;h=4ca186d4c4349249b74df5d5641b100720c107f4;p=foundry include/msg: Add modules for foundry IPC message handling This commit adds modules providing a convenient interface to generate and parse any of the message types that are used for IPC between foundry components. --- diff --git a/include/msg/artifact.sh b/include/msg/artifact.sh new file mode 100644 index 0000000..a737c74 --- /dev/null +++ b/include/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/msg/build.sh b/include/msg/build.sh new file mode 100644 index 0000000..a724ebb --- /dev/null +++ b/include/msg/build.sh @@ -0,0 +1,152 @@ +#!/bin/bash + +__init() { + if ! include "json" "foundry/msg/artifact"; then + return 1 + fi + + return 0 +} + +foundry_msg_build_new() { + local tid="$1" + local repository="$2" + local branch="$3" + local commit="$4" + local result="$5" + local -n __foundry_msg_build_new_logs="$6" + local -n __foundry_msg_build_new_artifacts="$7" + + local artifact_array + local log_array + local json + + if ! artifact_array=$(json_array "${__foundry_msg_build_new_artifacts[@]}"); then + return 1 + fi + + if ! log_array=$(json_array "${__foundry_msg_build_new_logs[@]}"); then + return 1 + fi + + if ! json=$(json_object "tid" "$tid" \ + "repository" "$repository" \ + "branch" "$branch" \ + "commit" "$commit" \ + "result" "$result" \ + "logs" "$log_array" \ + "artifacts" "$artifact_array"); then + return 1 + fi + + echo "$json" + return 0 +} + +foundry_msg_build_get_tid() { + local msg="$1" + + local tid + + if ! tid=$(json_object_get "$msg" "tid"); then + return 1 + fi + + echo "$tid" + return 0 +} + +foundry_msg_build_get_repository() { + local msg="$1" + + local repository + + if ! repository=$(json_object_get "$msg" "repository"); then + return 1 + fi + + echo "$repository" + return 0 +} + +foundry_msg_build_get_branch() { + local msg="$1" + + local branch + + if ! branch=$(json_object_get "$msg" "branch"); then + return 1 + fi + + echo "$branch" + return 0 +} + +foundry_msg_build_get_commit() { + local msg="$1" + + local commit + + if ! commit=$(json_object_get "$msg" "commit"); then + return 1 + fi + + echo "$commit" + return 0 +} + +foundry_msg_build_get_result() { + local msg="$1" + + local result + + if ! result=$(json_object_get "$msg" "result"); then + return 1 + fi + + echo "$result" + return 0 +} + +foundry_msg_build_get_logs() { + local msg="$1" + + local logs + + if ! logs=$(json_object_get "$msg" "logs[]"); then + return 1 + fi + + echo "$logs" + 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=$(json_object_get "$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/buildrequest.sh b/include/msg/buildrequest.sh new file mode 100644 index 0000000..a75e92a --- /dev/null +++ b/include/msg/buildrequest.sh @@ -0,0 +1,80 @@ +#!/bin/bash + +__init() { + if ! include "json"; then + return 1 + fi + + return 0 +} + +foundry_msg_buildrequest_new() { + local tid="$1" + local repository="$2" + local branch="$3" + local commit="$4" + + local json + + if ! json=$(json_object "tid" "$tid" \ + "repository" "$repository" \ + "branch" "$branch" \ + "commit" "$commit"); then + return 1 + fi + + echo "$json" + return 0 +} + +foundry_msg_buildrequest_get_tid() { + local msg="$1" + + local tid + + if ! tid=$(json_object_get "$msg" "tid"); then + return 1 + fi + + echo "$tid" + return 0 +} + +foundry_msg_buildrequest_get_repository() { + local msg="$1" + + local repository + + if ! repository=$(json_object_get "$msg" "repository"); then + return 1 + fi + + echo "$repository" + return 0 +} + +foundry_msg_buildrequest_get_branch() { + local msg="$1" + + local branch + + if ! branch=$(json_object_get "$msg" "branch"); then + return 1 + fi + + echo "$branch" + return 0 +} + +foundry_msg_buildrequest_get_commit() { + local msg="$1" + + local commit + + if ! commit=$(json_object_get "$msg" "commit"); then + return 1 + fi + + echo "$commit" + return 0 +} diff --git a/include/msg/commit.sh b/include/msg/commit.sh new file mode 100644 index 0000000..d3b23b8 --- /dev/null +++ b/include/msg/commit.sh @@ -0,0 +1,79 @@ +#!/bin/bash + +__init() { + if ! include "json"; then + return 1 + fi + + return 0 +} + +foundry_msg_commit_new() { + local repository="$1" + local branch="$2" + local commit="$3" + + local msg + + if ! msg=$(json_object "repository" "$repository" \ + "commit" "$commit" \ + "branch" "$branch"); then + return 1 + fi + + echo "$msg" + return 0 +} + +_foundry_msg_commit_get_field() { + local msg="$1" + local field="$2" + + local value + + if ! value=$(echo "$msg" | jq -e -r ".$field"); then + return 1 + fi + + echo "$value" + return 0 +} + +foundry_msg_commit_get_repository() { + local msg="$1" + + local repository + + if ! repository=$(_foundry_msg_commit_get_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_commit_get_field "$msg" "branch"); then + return 1 + fi + + echo "$branch" + return 0 +} + +foundry_msg_commit_get_commit() { + local msg="$1" + + local commit + + if ! commit=$(_foundry_msg_commit_get_field "$msg" "commit"); then + return 1 + fi + + echo "$commit" + return 0 +} diff --git a/include/msg/dist.sh b/include/msg/dist.sh new file mode 100644 index 0000000..1988599 --- /dev/null +++ b/include/msg/dist.sh @@ -0,0 +1,111 @@ +#!/bin/bash + +__init() { + if ! include "json" "foundry/msg/artifact"; then + return 1 + fi + + return 0 +} + +foundry_msg_dist_new() { + local tid="$1" + local repository="$2" + local artifact_data=("${@:3}") + + local msg + local artifacts + local artifact_array + local i + + if (( ${#artifact_data} & 1 != 0 )); then + return 1 + fi + + artifacts=() + + for (( i = 0; i + 1 < ${#artifact_data[@]}; i += 2 )); do + local artifact + local uri + local checksum + + uri="${artifact_data[$i]}" + checksum="${artifact_data[$((i + 1))]}" + + if ! artifact=$(foundry_msg_artifact_new "$uri" \ + "$checksum"); then + return 1 + fi + + artifacts+=("$artifact") + done + + if ! artifact_array=$(json_array "${artifacts[@]}"); then + return 1 + fi + + if ! msg=$(json_object "tid" "$tid" \ + "repository" "$repository" \ + "artifacts" "$artifact_array"); then + return 1 + fi + + echo "$msg" + return 0 +} + +foundry_msg_dist_get_tid() { + local msg="$1" + + local tid + + if ! tid=$(json_object_get "$msg" "tid"); then + return 1 + fi + + echo "$tid" + return 0 +} + +foundry_msg_dist_get_repository() { + local msg="$1" + + local repository + + if ! repository=$(json_object_get "$msg" "repository"); then + return 1 + fi + + echo "$repository" + 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=$(json_object_get "$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/distrequest.sh b/include/msg/distrequest.sh new file mode 100644 index 0000000..7f9f53e --- /dev/null +++ b/include/msg/distrequest.sh @@ -0,0 +1,91 @@ +#!/bin/bash + +__init() { + if ! include "json"; then + return 1 + fi + + return 0 +} + +foundry_msg_distrequest_new() { + local tid="$1" + local artifact_data=("${@:2}") + + local artifacts_array + local artifacts + local distrequest + local i + + if ! (( $# & 1 )); then + # Invalid number of arguments + return 1 + fi + + artifacts=() + + for (( i = 0; (i + 1) < $#; i += 2 )); do + local artifact + + if ! artifact=$(foundry_msg_artifact_new "${artifact_data[$i]}" \ + "${artifact_data[$((i+1))]}"); then + continue + fi + + artifacts+=("$artifact") + done + + if ! artifacts_array=$(json_array "${artifacts[@]}"); then + return 1 + fi + + if ! distrequest=$(json_object "tid" "$tid" \ + "artifacts" "$artifacts_array"); then + return 1 + fi + + echo "$distrequest" + return 0 +} + +foundry_msg_distrequest_get_tid() { + local distrequest="$1" + + local tid + + if ! tid=$(json_object_get "$distrequest" "tid"); then + return 1 + fi + + echo "$tid" + return 0 +} + +foundry_msg_distrequest_get_artifacts() { + local distrequest="$1" + + local raw_artifacts + local artifacts + local artifact + local checksum + local uri + + if ! raw_artifacts=$(json_object_get "$distrequest" 'artifacts[] | "\(.checksum) \(.uri)"'); then + return 1 + fi + + while read -r checksum uri; do + if ! artifact=$(json_object "uri" "$uri" \ + "checksum" "$checksum"); then + return 1 + fi + + artifacts+=("$artifact") + done <<< "$raw_artifacts" + + for artifact in "${artifacts[@]}"; do + echo "$artifact" + done + + return 0 +} diff --git a/include/msg/merge.sh b/include/msg/merge.sh new file mode 100644 index 0000000..3c3ae9f --- /dev/null +++ b/include/msg/merge.sh @@ -0,0 +1,110 @@ +#!/bin/bash + +__init() { + if ! include "json"; then + return 1 + fi + + return 0 +} + +foundry_msg_merge_new() { + local tid="$1" + local repository="$2" + local srcbranch="$3" + local dstbranch="$4" + local status="$5" + local log="$6" + + local json + + if ! json=$(json_object "tid" "$tid" \ + "repository" "$repository" \ + "srcbranch" "$srcbranch" \ + "dstbranch" "$dstbranch" \ + "status" "$status" \ + "log" "$log"); then + return 1 + fi + + echo "$json" + return 0 +} + +foundry_msg_merge_get_tid() { + local msg="$1" + + local tid + + if ! tid=$(json_object_get "$msg" "tid"); then + return 1 + fi + + echo "$tid" + return 0 +} + +foundry_msg_merge_get_repository() { + local msg="$1" + + local repository + + if ! repository=$(json_object_get "$msg" "repository"); then + return 1 + fi + + echo "$repository" + return 0 +} + +foundry_msg_merge_get_source_branch() { + local msg="$1" + + local srcbranch + + if ! srcbranch=$(json_object_get "$msg" "source_branch"); then + return 1 + fi + + echo "$srcbranch" + return 0 +} + +foundry_msg_merge_get_destination_branch() { + local msg="$1" + + local dstbranch + + if ! dstbranch=$(json_object_get "$msg" "destination_branch"); then + return 1 + fi + + echo "$dstbranch" + return 0 +} + +foundry_msg_merge_get_status() { + local msg="$1" + + local status + + if ! status=$(json_object_get "$msg" "status"); then + return 1 + fi + + echo "$status" + return 0 +} + +foundry_msg_merge_get_log() { + local msg="$1" + + local log + + if ! log=$(json_object_get "$msg" "log"); then + return 1 + fi + + echo "$log" + return 0 +} diff --git a/include/msg/mergerequest.sh b/include/msg/mergerequest.sh new file mode 100644 index 0000000..b95a5fa --- /dev/null +++ b/include/msg/mergerequest.sh @@ -0,0 +1,79 @@ +#!/bin/bash + +__init() { + if ! include "json"; then + return 1 + fi + + return 0 +} + +foundry_msg_mergerequest_new() { + local tid="$1" + local repository="$2" + local srcbranch="$3" + local dstbranch="$4" + + local json + + if ! json=$(json_object "tid" "$tid" \ + "repository" "$repository" \ + "source_branch" "$srcbranch" \ + "destination_branch" "$dstbranch"); then + return 1 + fi + + echo "$json" + return 0 +} + +foundry_msg_mergerequest_get_tid() { + local msg="$1" + + local tid + + if ! tid=$(json_object_get "$msg" "tid"); then + return 1 + fi + + return 0 +} + +foundry_msg_mergerequest_get_repository() { + local msg="$1" + + local repository + + if ! repository=$(json_object_get "$msg" "repository"); then + return 1 + fi + + echo "$repository" + return 0 +} + +foundry_msg_mergerequest_get_source_branch() { + local msg="$1" + + local srcbranch + + if ! srcbranch=$(json_object_get "$msg" "source_branch"); then + return 1 + fi + + echo "$srcbranch" + return 0 +} + +foundry_msg_mergerequest_get_destination_branch() { + local msg="$1" + + local dstbranch + + if ! dstbranch=$(json_object_get "$msg" "destination_branch"); then + return 1 + fi + + echo "$dstbranch" + return 0 +} diff --git a/include/msg/sign.sh b/include/msg/sign.sh new file mode 100644 index 0000000..eff9e35 --- /dev/null +++ b/include/msg/sign.sh @@ -0,0 +1,111 @@ +#!/bin/bash + +__init() { + if ! include "json" "foundry/msg/artifact"; then + return 1 + fi + + return 0 +} + +foundry_msg_sign_new() { + local tid="$1" + local key="$2" + local artifact_data=("${@:3}") + + local msg + local artifacts + local artifact_array + local i + + if (( ${#artifact_data} & 1 != 0 )); then + return 1 + fi + + artifacts=() + + for (( i = 0; i + 1 < ${#artifact_data[@]}; i += 2 )); do + local artifact + local uri + local checksum + + uri="${artifact_data[$i]}" + checksum="${artifact_data[$((i + 1))]}" + + if ! artifact=$(foundry_msg_artifact_new "$uri" \ + "$checksum"); then + return 1 + fi + + artifacts+=("$artifact") + done + + if ! artifact_array=$(json_array "${artifacts[@]}"); then + return 1 + fi + + if ! msg=$(json_object "tid" "$tid" \ + "key" "$key" \ + "artifacts" "$artifact_array"); then + return 1 + fi + + echo "$msg" + return 0 +} + +foundry_msg_sign_get_tid() { + local msg="$1" + + local tid + + if ! tid=$(json_object_get "$msg" "tid"); then + return 1 + fi + + echo "$tid" + return 0 +} + +foundry_msg_sign_get_key() { + local msg="$1" + + local key + + if ! key=$(json_object_get "$msg" "key"); then + return 1 + fi + + echo "$key" + 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=$(json_object_get "$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/signrequest.sh b/include/msg/signrequest.sh new file mode 100644 index 0000000..b11e101 --- /dev/null +++ b/include/msg/signrequest.sh @@ -0,0 +1,92 @@ +#!/bin/bash + +__init() { + if ! include "json" "foundry/msg/artifact"; then + return 1 + fi + + return 0 +} + +foundry_msg_signrequest_new() { + local tid="$1" + local artifact_data=("${@:2}") + + local artifacts_array + local artifacts + local signrequest + local i + + if ! (( $# & 1 )); then + # Invalid number of arguments + return 1 + fi + + artifacts=() + + for (( i = 0; (i + 1) < $#; i += 2 )); do + local artifact + + if ! artifact=$(foundry_msg_artifact_new "${artifact_data[$i]}" \ + "${artifact_data[$((i+1))]}"); then + continue + fi + + artifacts+=("$artifact") + done + + if ! artifacts_array=$(json_array "${artifacts[@]}"); then + return 1 + fi + + if ! signrequest=$(json_object "tid" "$tid" \ + "artifacts" "$artifacts_array"); then + return 1 + fi + + echo "$signrequest" + return 0 +} + +foundry_msg_signrequest_get_tid() { + local signrequest="$1" + + local tid + + if ! tid=$(json_object_get "$signrequest" "tid"); then + return 1 + fi + + echo "$tid" + return 0 +} + +foundry_msg_signrequest_get_artifacts() { + local signrequest="$1" + + local query + local raw_artifacts + local artifacts + local checksum + local uri + + query='artifacts[] | "\(.checksum) \(.uri)"' + artifacts=() + + if ! raw_artifacts=$(json_object_get "$signrequest" "$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 new file mode 100644 index 0000000..8aa16e3 --- /dev/null +++ b/include/msg/test.sh @@ -0,0 +1,115 @@ +#!/bin/bash + +__init() { + if ! include "json"; then + return 1 + fi + + return 0 +} + +foundry_msg_test_new() { + local tid="$1" + local repository="$2" + local branch="$3" + local commit="$4" + local result="$5" + local logs=("${@:6}") + + local logs_array + local msg + + if ! logs_array=$(json_array "${logs[@]}"); then + return 1 + fi + + if ! msg=$(json_object "tid" "$tid" \ + "repository" "$repository" \ + "branch" "$branch" \ + "commit" "$commit" \ + "result" "$result" \ + "logs" "$logs_array"); then + return 1 + fi + + echo "$msg" + return 0 +} + +foundry_msg_test_get_tid() { + local msg="$1" + + local tid + + if ! tid=$(json_object_get "$msg" "tid"); then + return 1 + fi + + echo "$tid" + return 0 +} + +foundry_msg_test_get_repository() { + local msg="$1" + + local repository + + if ! repository=$(json_object_get "$msg" "repository"); then + return 1 + fi + + echo "$repository" + return 0 +} + +foundry_msg_test_get_branch() { + local msg="$1" + + local branch + + if ! branch=$(json_object_get "$msg" "branch"); then + return 1 + fi + + echo "$branch" + return 0 +} + +foundry_msg_test_get_commit() { + local msg="$1" + + local commit + + if ! commit=$(json_object_get "$msg" "commit"); then + return 1 + fi + + echo "$commit" + return 0 +} + +foundry_msg_test_get_result() { + local msg="$1" + + local result + + if ! result=$(json_object_get "$msg" "result"); then + return 1 + fi + + echo "$result" + return 0 +} + +foundry_msg_test_get_logs() { + local msg="$1" + + local logs + + if ! logs=$(json_object_get "$msg" "logs[]"); then + return 1 + fi + + echo "$logs" + return 0 +} diff --git a/include/msg/testrequest.sh b/include/msg/testrequest.sh new file mode 100644 index 0000000..ab9324c --- /dev/null +++ b/include/msg/testrequest.sh @@ -0,0 +1,81 @@ +#!/bin/bash + +__init() { + if ! include "json"; then + return 1 + fi + + return 0 +} + +foundry_msg_testrequest_new() { + local tid="$1" + local repository="$2" + local branch="$3" + local commit="$4" + + local json + + if ! json=$(json_object "tid" "$tid" \ + "repository" "$repository" \ + "branch" "$branch" \ + "commit" "$commit"); then + return 1 + fi + + echo "$json" + return 0 +} + +foundry_msg_testrequest_get_tid() { + local msg="$1" + + local tid + + if ! tid=$(jq -e -r ".tid" <<< "$msg"); then + return 1 + fi + + echo "$tid" + return 0 +} + + +foundry_msg_testrequest_get_repository() { + local msg="$1" + + local repository + + if ! repository=$(jq -e -r ".repository" <<< "$msg"); then + return 1 + fi + + echo "$repository" + return 0 +} + +foundry_msg_testrequest_get_branch() { + local msg="$1" + + local branch + + if ! branch=$(jq -e -r ".branch" <<< "$msg"); then + return 1 + fi + + echo "$branch" + return 0 +} + +foundry_msg_testrequest_get_commit() { + local msg="$1" + + local commit + + if ! commit=$(jq -e -r ".commit" <<< "$msg"); then + return 1 + fi + + echo "$commit" + return 0 +}