2 # SPDX-License-Identifier: Apache-2.0
3 # Copyright 2024 The LMinux Foundation <matthew.watkins@linuxfoundation.org>
4 # Uncomment to enable debugging
6 # Count file upload successes/failures
7 SUCCESSES="0"; FAILURES="0"
12 Usage: ${0##*/} [-h] [-u user] [-p password] [-s upload-url] [-e extensions] [-d folder]
13 -h display this help and exit
14 -u username (or export variable NEXUS_USERNAME)
15 -p password (or export variable NEXUS_PASSWORD)
16 -s upload URL (or export variable NEXUS_URL)
17 e.g. https://nexus3.o-ran-sc.org/repository/datasets/
18 -e file extensions to match, e.g. csv, txt
19 -d local directory hosting files/content to be uploaded
27 echo "Successes: $SUCCESSES Failures: $FAILURES"
28 if [ "$FAILURES" -gt 0 ]; then
36 echo "Sending: ${FILE}"
37 # echo "Running: $CURL --fail [CREDENTIALS] --upload-file $FILE $NEXUS_URL"
38 if ("$CURL" --fail -u "$CREDENTIALS" --upload-file "$FILE" "$NEXUS_URL"); then #> /dev/null 2>&1
39 SUCCESSES=$((SUCCESSES+1))
41 FAILURES=$((FAILURES+1))
45 for FILE in "${UPLOAD_FILES_ARRAY[@]}"; do
49 # Validate/check arguments and variables
51 if [ ! -x "$CURL" ];then
52 echo "CURL was not found in your PATH"; exit 1
54 while getopts hu:p:s:d:e: opt; do
56 u) NEXUS_USERNAME="$OPTARG"
58 p) NEXUS_PASSWORD="$OPTARG"
60 s) NEXUS_URL="$OPTARG"
62 e) FILE_EXTENSION="$OPTARG"
64 d) DIRECTORY="$OPTARG"
65 if [ ! -d "$DIRECTORY" ]; then
66 echo "Error: specified directory invalid"; exit 1
77 shift "$((OPTIND -1))" # Discard the options
78 # Gather location of files to upload (in an array)
79 mapfile -t UPLOAD_FILES_ARRAY < <(find "$DIRECTORY" -name "*$FILE_EXTENSION" -type f -print )
80 if [ "${#UPLOAD_FILES_ARRAY[@]}" -ne 0 ]; then
81 echo "Files found to upload: ${#UPLOAD_FILES_ARRAY[@]}"
82 # echo "Files matching pattern:" # Uncomment for debugging
83 # echo "${UPLOAD_FILES_ARRAY[@]}" # Uncomment for debugging
85 echo "Error: no files found to process matching pattern"
88 if [ -z "$NEXUS_URL" ]; then
89 echo "ERROR: Specifying the upload/repository URL is mandatory"; exit 1
91 if [[ ! "$NEXUS_URL" == "http://"* ]] && \
92 [[ ! "$NEXUS_URL" == "https://"* ]]; then
93 echo "Error: Nexus server must be specified as a URL"; exit 1
96 # Prompt for credentials if not specified explicitly or present in the shell environment
97 if [ -z "$NEXUS_USERNAME" ]; then
98 echo -n "Enter username: "
99 read -r NEXUS_USERNAME
100 if [[ -z "$NEXUS_USERNAME" ]]; then
101 echo "ERROR: Username cannot be empty"; exit 1
104 if [ -z "$NEXUS_PASSWORD" ]; then
105 echo -n "Enter password: "
106 read -s -r NEXUS_PASSWORD # Does not echo to terminal/console
108 if [[ -z "$NEXUS_PASSWORD" ]]; then
109 echo "ERROR: Password cannot be empty"; exit 1
112 CREDENTIALS="$NEXUS_USERNAME:$NEXUS_PASSWORD"
113 # Main script entry point
114 echo "Uploading to: $NEXUS_URL"