我需要用Artifactory文件信息来完成csv文件,但是当我试图生成它时,我在格式上有一些问题。
在csv中等待的格式:

用错误格式生成的格式:

为此,我对每个组件进行了Artifactory,文件夹中的文件列表,以及每个组件的所有校验和:
function getArtifactoryMavenInfo {
completPath="$1$commandeAsk"
# get all files in one Artifactory folder => this is OK
list_MAVEN_files=($(curl -H "Content-Type: text/plain" -u XXX:XXX -X GET "$completPath" | jq ".files" | sed -n -e '/^.*\"uri\"\: \"/p' | sed 's/^.*\"uri\"\: \"\///' | sed 's/".*$//'))
# all_checksum will have all files with there checksums
all_checksum=""
for (( f=0; f<${#list_MAVEN_files[@]}; f++ )); do
filePath="$1/${list_MAVEN_files[f]}"
# all checksums have the good format => this is ok
checksums_MAVEN_sha1=($(curl "$filePath" | jq ".checksums.sha1" | sed 's/"//' | sed 's/"$//'))
checksums_MAVEN_md5=($(curl "$filePath" | jq ".checksums.md5" | sed 's/"//' | sed 's/"$//'))
checksums_MAVEN_sha256=($(curl "$filePath" | jq ".checksums.sha256" | sed 's/"//' | sed 's/"$//'))
# Trying to create an array with all files and there checksums => this is not sure ...
all_checksum+=$(echo "${list_MAVEN_files[f]} : sha1=${checksums_MAVEN_sha1} / md5=${checksums_MAVEN_md5} / sha256=${checksums_MAVEN_sha256}")
done
# put the previous array on a list => it will be the third column in the csv
finalListArtifactorySHA+=($(echo "$all_checksum"))
# put the path of the folder on a list => it will be for example the first column
finalListArtifactoryPath+=($(echo "$1/"))
}有了这些,我就有了所有需要的信息。然后,我尝试将所有这些放在csv文件中:
echo "Component name;Component version;Artifacts checksums;Artifactory path;Repo path" > $CSV_FILE_NAME
for (( i=0; i<${#finalListComponentName[@]}; i++ )); do
echo "${finalListComponentName[i]};${finalListComponentVersion[i]};${finalListArtifactorySHA[i]};${finalListArtifactoryPath[i]};${finalListRepoPath[i]}" >> $CSV_FILE_NAME
done你能帮帮我吗?我尝试了很多修改,并检查了所有的论坛,但我从来没有得到我需要的结果,我的csv输出。
非常感谢您的帮助!
发布于 2021-12-20 12:41:12
对不起,我给你我的完整脚本,我只是改变路径的艺术和git。注册表名参数是git超级回购:
#!/bin/bash
### launch command : ./Get_Component_Versions_Release.sh registry-name branch ###
printf "\n====> Get_Component_Versions_Release - BEGIN OF SCRIPT <====\n"
### GLOBAL
# | Component name | Repo Path | Component Version | Path artifact Artifactory (URI) | SHA 256 of each file (checksums) | Path artifact Harbor | SHA 256 of each file
declare -a finalListComponentName=()
declare -a finalListRepoPath=()
declare -a finalListComponentVersion=()
declare -a finalListArtifactoryPath=()
declare -a finalListArtifactorySHA=()
### FUNCTIONS
function getArtifactoryNPMInfo {
printf "\n====> Get_Component_Versions_Release - getArtifactoryNPMInfo - Begin function <====\n"
declare -a all_checksum=()
completPath="$1"
componentName="$2"
printf "Full path to packages : $completPath \n"
printf "\n====> Get_Component_Versions_Release - getArtifactoryNPMInfo - Get all checksums <====\n"
checksums_NPM_sha1=($(curl "$completPath" | jq ".checksums.sha1" | sed 's/"//' | sed 's/"$//'))
checksums_NPM_md5=($(curl "$completPath" | jq ".checksums.md5" | sed 's/"//' | sed 's/"$//'))
checksums_NPM_sha256=($(curl "$completPath" | jq ".checksums.sha256" | sed 's/"//' | sed 's/"$//'))
all_checksum=$(echo "${componentName}:sha1=${checksums_NPM_sha1}/md5=${checksums_NPM_md5}/sha256=${checksums_NPM_sha256}")
# all_checksum=($(echo "${componentName}"))
# all_checksum+=($(echo "sha1=${checksums_NPM_sha1}"))
# all_checksum+=($(echo "md5=${checksums_NPM_md5}"))
# all_checksum+=($(echo "sha256=${checksums_NPM_sha256}"))
printf "\n====> Get_Component_Versions_Release - getArtifactoryNPMInfo - Get all uri <====\n"
uri_NPM=($(curl "$completPath" | jq ".uri" | sed 's/"//' | sed 's/"$//'))
printf "\n====> Get_Component_Versions_Release - getArtifactoryNPMInfo - Push values to list <====\n"
#finalListArtifactorySHA+=($(echo "${all_checksum}"))
finalListArtifactorySHA+=($(echo "N/A"))
finalListArtifactoryPath+=($(echo "$uri_NPM"))
}
function getArtifactoryMavenInfo {
printf "\n====> Get_Component_Versions_Release - getArtifactoryMavenInfo - Begin function <====\n"
commandeAsk="?list&deep=1&listFolders=1&mdTimestamps=1"
completPath="$1$commandeAsk"
printf "Full path to packages : $completPath \n"
printf "\n====> Get_Component_Versions_Release - getArtifactoryMavenInfo - Generate List of files <====\n"
list_MAVEN_files=($(curl -H "Content-Type: text/plain" -u 502779997:AKCp8hzCzoGCHhwzPst2vHz4gs8vfZRphEqtsoz87pwwHWEWdwD1Yp1qZG3eBF7WJVH9mQD4H -X GET "$completPath" | jq ".files" | sed -n -e '/^.*\"uri\"\: \"/p' | sed 's/^.*\"uri\"\: \"\///' | sed 's/".*$//'))
all_checksum=""
for (( f=0; f<${#list_MAVEN_files[@]}; f++ )); do
filePath="$1/${list_MAVEN_files[f]}"
printf "filePath = $filePath \n"
checksums_MAVEN_sha1=($(curl "$filePath" | jq ".checksums.sha1" | sed 's/"//' | sed 's/"$//'))
checksums_MAVEN_md5=($(curl "$filePath" | jq ".checksums.md5" | sed 's/"//' | sed 's/"$//'))
checksums_MAVEN_sha256=($(curl "$filePath" | jq ".checksums.sha256" | sed 's/"//' | sed 's/"$//'))
all_checksum="$all_checksum$(echo "${list_MAVEN_files[f]}:sha1=${checksums_MAVEN_sha1}/md5=${checksums_MAVEN_md5}/sha256=${checksums_MAVEN_sha256}")"
# all_checksum+=($(echo "${list_MAVEN_files[f]}"))
# all_checksum+=($(echo "sha1 = ${checksums_MAVEN_sha1}"))
# all_checksum+=($(echo "md5 = ${checksums_MAVEN_md5}"))
# all_checksum+=($(echo "sha256 = ${checksums_MAVEN_sha256}"))
done
printf "\n====> Get_Component_Versions_Release - getArtifactoryMavenInfo - Push values to list <====\n"
finalListArtifactorySHA+=($(echo "$all_checksum"))
#finalListArtifactorySHA+=($(echo "N/A"))
finalListArtifactoryPath+=($(echo "$1/"))
}
function getArtifactoryHelmInfo {
printf "\n====> Get_Component_Versions_Release - getArtifactoryHelmInfo - Begin function <====\n"
completPath="$1"
printf "Full path to packages : $completPath \n"
printf "\n====> Get_Component_Versions_Release - getArtifactoryHelmInfo - Generate List of files <====\n"
#curl "$completPath"
printf "\n====> Get_Component_Versions_Release - getArtifactoryHelmInfo - Push values to list <====\n"
finalListArtifactorySHA+=($(echo "N/A"))
finalListArtifactoryPath+=($(echo "N/A"))
}
### PARAMETERS
printf "\n====> Get_Component_Versions_Release - Get parameters <====\n"
SDKR_TO_USE=$1
BRANCH_TO_USE=$2
printf "\n====> Get_Component_Versions_Release - Print parameters <====\n"
printf "SDKRegistry to use : $SDKR_TO_USE \n"
printf "Branch to update : $BRANCH_TO_USE \n"
printf "\n====> Get_Component_Versions_Release - Check parameters <====\n"
if [ "$#" -eq 2 ]; then
printf "Number of Parameters OK "
else
printf "ERROR : Please give all parameters needed : ./Get_Component_Versions_Release.sh registry-name branch"
exit 1
fi
printf "\n====> Get_REPO - Put Marvin infos <====\n"
git config --global user.email "502779997@ge.com"
git config --global user.name "502779997"
### CODE EXECUTION
printf "\n====> Get_Component_Versions_Release - step 1 - Check SDKRegistry exist <====\n"
if [ -d "$SDKR_TO_USE" ]; then
printf "Directory $SDKR_TO_USE exists.\n"
cd $SDKR_TO_USE/
else
printf "ERROR : No SDKRegistry to use, please launch Get_REPO.sh to get one \n"
exit 1
fi
printf "\n====> Get_Component_Versions_Release - step 2 - Change branches on SDKR and submodules <====\n"
#git checkout $BRANCH_TO_USE
#git submodule foreach "git checkout '$BRANCH_TO_USE'"
printf "\n====> Get_Component_Versions_Release - step 3 - Declaration of all paths <====\n"
ARTIFACTORY_PATH="https://XXX/artifactory/api/storage"
ARTIFACTORY_MAVEN_PATH="XXX"
ARTIFACTORY_NPM_PATH="XXX"
GIT_PATH="XXX"
SDKR_PATH=$(pwd)
printf "\n====> Get_Component_Versions_Release - step 4 - Get listes <====\n"
cmdGetULRList=($(grep url .gitmodules | sed "s/.*= //"))
#printf "cmdGetULRList : $cmdGetULRList \n"
listComponentName=($(grep url .gitmodules | sed -e "s|.*/||" -e "s/.git//"))
#printf "listComponentName : $listComponentName \n"
listComponentURL=($(grep url .gitmodules | sed "s/.*://"))
#printf "listComponentURL : $listComponentURL \n"
cmdGetPATHonSDKRList=($(grep path .gitmodules | sed "s/.*= //"))
#printf "cmdGetPATHonSDKRList : $cmdGetPATHonSDKRList \n"
printf "\n====> Get_Component_Versions_Release - step 5 - Complete lites <====\n"
currentPos=0
for (( i=0; i<${#cmdGetPATHonSDKRList[@]}; i++ )); do
printf "\n====> Go to submodule <====\n"
cd ${cmdGetPATHonSDKRList[i]}
printf "\n====> Path of submodule <====\n"
pwd
name="N/A"
version="N/A"
printf "\n====> Generate list of config files <====\n"
listPOM=($(find -iname "pom.xml"))
listJSON=($(find -iname "package.json"))
listCHART=($(find -iname "chart.yaml"))
printf "\n====> pom.xml case <====\n"
if [ -z "$listPOM" ]; then
printf "\n listPOM is empty \n"
else
for (( pom=0; pom<${#listPOM[@]}; pom++ )); do
printf "\n====> generate finalListComponentName <====\n"
#name=$(cat ${listPOM[pom]} | grep "<artifactId>.*</artifactId>" | head -1 |awk -F'[><]' '{print $3}')
name=$(xmllint --xpath "//*[local-name()='project']/*[local-name()='artifactId']/text()" ${listPOM[pom]})
finalListComponentName+=($(echo "$name"))
printf "\n====> generate finalListRepoPath <====\n"
finalListRepoPath+=($(echo "$GIT_PATH${listComponentURL[i]}"))
printf "\n====> generate finalListComponentVersion <====\n"
#version=$(cat ${listPOM[pom]} | grep "<version>.*</version>" | head -1 |awk -F'[><]' '{print $3}')
name=$(xmllint --xpath "//*[local-name()='project']/*[local-name()='version']/text()" ${listPOM[pom]})
finalListComponentVersion+=($(echo "$version"))
printf "\n====> generate finalListArtifactoryPath and generate finalListArtifactorySHA <====\n"
fullPath="$ARTIFACTORY_PATH/$ARTIFACTORY_MAVEN_PATH/$name/$version"
getArtifactoryMavenInfo $fullPath
# printf "New line : Name => ${finalListComponentName[currentPos]} Path => ${finalListRepoPath[currentPos]} Version => ${finalListComponentVersion[currentPos]} \n"
currentPos=$((currentPos + 1))
done
fi
printf "\n====> package.json case <====\n"
if [ -z "$listJSON" ]; then
printf "\n listJSON is empty \n"
else
for (( json=0; json<${#listJSON[@]}; json++ )); do
printf "\n====> generate finalListComponentName <====\n"
name=$(cat ${listJSON[json]} | grep name | head -1 | awk -F: '{ print $2 }' | sed 's/[",]//g' | tr -d '[[:space:]]' | sed 's/.*\///g')
finalListComponentName+=($(echo "$name"))
printf "\n====> generate finalListRepoPath <====\n"
finalListRepoPath+=($(echo "$GIT_PATH${listComponentURL[i]}"))
printf "\n====> generate finalListComponentVersion <====\n"
version=$(cat ${listJSON[json]}| grep version | head -1 | awk -F: '{ print $2 }' | sed 's/[",]//g' | tr -d '[[:space:]]')
finalListComponentVersion+=($(echo "$version"))
printf "\n====> generate finalListArtifactoryPath and finalListArtifactorySHA <====\n"
fullPath="$ARTIFACTORY_PATH/$ARTIFACTORY_NPM_PATH/$name/-/@ifabric/$name-$version.tgz"
fileName="$name-$version.tgz"
getArtifactoryNPMInfo $fullPath $fileName
# printf "New line : Name => ${finalListComponentName[currentPos]} Path => ${finalListRepoPath[currentPos]} Version => ${finalListComponentVersion[currentPos]} \n"
currentPos=$((currentPos + 1))
done
fi
printf "\n====> chart.yaml case <====\n"
if [ -z "$listCHART" ]; then
printf "\n listCHART is empty \n"
else
for (( chart=0; chart<${#listCHART[@]}; chart++ )); do
printf "\n====> generate finalListComponentName <====\n"
name=$(cat ${listCHART[chart]} | grep name | head -1 | awk -F: '{ print $2 }' | tr -d '[[:space:]]')
finalListComponentName+=($(echo "$name"))
printf "\n====> generate finalListRepoPath <====\n"
finalListRepoPath+=($(echo "$GIT_PATH${listComponentURL[i]}"))
printf "\n====> generate finalListComponentVersion <====\n"
version=$(cat ${listCHART[chart]}| grep version | head -1 | awk -F: '{ print $2 }' | tr -d '[[:space:]]')
finalListComponentVersion+=($(echo "$version"))
printf "\n====> generate finalListArtifactoryPath and finalListArtifactorySHA <====\n"
finalListArtifactorySHA+=($(echo "N/A"))
finalListArtifactoryPath+=($(echo "N/A"))
# printf "New line : Name => ${finalListComponentName[currentPos]} Path => ${finalListRepoPath[currentPos]} Version => ${finalListComponentVersion[currentPos]} \n"
currentPos=$((currentPos + 1))
done
fi
printf "Go back to SDKR root \n"
cd $SDKR_PATH
done
printf "\n====> Get_Component_Versions_Release - step 6 - Create CSV file <====\n"
CSV_FILE_NAME="componentFullDocumentation_$BRANCH_TO_USE.csv"
cd ..
echo "Component name;Component version;Artifacts checksums;Artifactory path;Repo path" > $CSV_FILE_NAME
for (( i=0; i<${#finalListComponentName[@]}; i++ )); do
echo "${finalListComponentName[i]};${finalListComponentVersion[i]};${finalListArtifactorySHA[i]};${finalListArtifactoryPath[i]};${finalListRepoPath[i]}" >> $CSV_FILE_NAME
done
printf "\n====> Get_Component_Versions_Release - END OF SCRIPT <====\n"https://stackoverflow.com/questions/70361927
复制相似问题