0

我需要使用 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 输出中得到我需要的结果。

非常感谢您的帮助 !!!

4

1 回答 1

0

对不起,我给你我的完整脚本,我只是改变了工件和 git 的路径。registry-name 参数是 git super-repo :

#!/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"
于 2021-12-20T12:41:12.580 回答