Golang
在本例中,您将创建一个 "包 "蓝图,使用 Port'sAPI 和webhook functionality 的组合来引用 Go 模块、版本和依赖关系。然后,您将把该蓝图与 "服务 "蓝图关联起来,以便映射服务所引用的所有包。
为了将软件包引用到 Port,需要使用一个脚本,根据 webhook 配置发送软件包信息。
先决条件
创建以下蓝图定义和 webhook 配置:
Service blueprint
{
"identifier": "service",
"title": "Service",
"icon": "Service",
"schema": {
"properties": {
"description": {
"title": "Description",
"type": "string"
}
},
"required": []
},
"mirrorProperties": {},
"calculationProperties": {},
"relations": {}
}
Package blueprint
{
"identifier": "package",
"description": "This blueprint represents a Go dependency",
"title": "Go Package",
"icon": "Go",
"schema": {
"properties": {
"packageUrl": {
"icon": "DefaultProperty",
"title": "Package URL",
"description": "The URL of the dependency package",
"type": "string",
"format": "url"
},
"version": {
"type": "string",
"title": "Version",
"description": "The version of the dependency"
},
"indirect": {
"type": "boolean",
"title": "Indirect Dependency",
"description": "Whether the dependency is indirect"
},
"packageName": {
"type": "string",
"title": "Package Name",
"description": "The name of the dependency package"
}
},
"required": ["packageName", "packageUrl", "version"]
},
"mirrorProperties": {},
"calculationProperties": {},
"relations": {
"service": {
"title": "Service",
"target": "service",
"required": false,
"many": true
}
}
}
Package webhook configuration
{
"identifier": "goDependencyMapper",
"title": "Go Dependency Mapper",
"description": "A webhook configuration to map packages and dependencies from a file",
"icon": "Go",
"mappings": [
{
"blueprint": "package",
"entity": {
"identifier": ".body.package_name",
"title": ".body.package_url",
"properties": {
"packageName": ".body.package_name",
"packageUrl": ".body.package_url",
"version": ".body.version",
"indirect": ".body.indirect"
}
"relations": {
"service": ".body.service"
}
}
}
],
"enabled": true,
"security": {}
}
使用 Port 的 API 和 Bash 脚本
下面的示例片段展示了如何使用 Python 和 Bash 将 Port 的 API 和 Webhook 与现有管道集成:
- Python
- Bash
在版本库中创建以下 Bash 脚本,以创建或更新作为 Pipelines 一部分的 Port 实体:
Go Bash script
#!/bin/bash
# Get environment variables
WEBHOOK_URL="$WEBHOOK_URL"
SERVICE_ID="$SERVICE_ID"
set -e
# Create or clear the output file
echo "[]" > output.json
# Extract require lines from go.mod excluding the first and the last lines
mapfile -t requires < <(sed -n '/require (/,/)/p' go.mod | tail -n +2 | head -n -1)
# Parse each require line into a package JSON
for require in "${requires[@]}"; do
# Ignore if line is 'require (' or ')'
if [[ "$require" == "require (" ]] || [[ "$require" == ")" ]]; then
continue
fi
# Split line into an array
IFS=' ' read -r -a parts <<< "$require"
# Assign array items to variables
package_url="${parts[0]}"
version="${parts[1]}"
indirect=false
# Check if line is indirect
if [[ "${parts[2]}" == "//" && "${parts[3]}" == "indirect" ]]; then
indirect=true
fi
# Extract the package name from the URL
package_name=$(basename "$package_url")
# Prepend 'https://' to package URL if not already there and remove any white spaces
package_url=$(echo "$package_url" | tr -d '[:space:]')
if [[ "$package_url" != http* ]]; then
package_url="https://$package_url"
fi
# Create the package JSON
package_json=$(jq -n \
--arg pn "$package_name" \
--arg id "$package_name" \
--arg pu "$package_url" \
--arg v "$version" \
--argjson i "$indirect" \
'{
identifier: $id,
packageName: $pn,
packageUrl: $pu,
version: $v,
indirect: $i,
service: $SERVICE_ID
}')
# Add the package JSON to the output file
jq --argjson p "$package_json" '. += [$p]' output.json > temp.json && mv temp.json output.json
# Send the package JSON to the webhook
curl --location '$WEBHOOK_URL' \
--header 'Content-Type: application/json' \
--data "$package_json"
done
note
- 该脚本利用 Bash shell 中的内置命令
mapfile
来读取go.mod
文件中的行并将其存储到数组中。请注意,并非所有 shell 默认都支持该命令。如果你被用于了不同的 shell,如 Dash 或 Zsh,你可能需要切换到 Bash 或修改脚本来实现类似的功能。 - 脚本依赖于
jq
命令来操作 JSON 数据。它被用来根据从go.mod
文件中提取的 package 详细信息创建 JSON 对象,并将这些对象追加到输出 JSON 文件中。值得注意的是,"jq "是一个功能强大的命令行 JSON 处理器,但许多系统默认情况下并不包含它。您可能需要单独安装才能使用它。