2 # SPDX-License-Identifier: Apache-2.0
3 # Copyright 2024 The LMinux Foundation <matthew.watkins@linuxfoundation.org>
4 # Uncomment to enable debugging
9 # Count file upload successes/failures
10 SUCCESSES="0"; FAILURES="0"
15 Usage: ${0##*/} [-h] [-u user] [-p password] [-s upload-url] [-e extensions] [-d folder]
16 -h display this help and exit
17 -u username (or export variable NEXUS_USERNAME)
18 -p password (or export variable NEXUS_PASSWORD)
19 -s upload URL (or export variable NEXUS_URL)
20 e.g. https://nexus3.o-ran-sc.org/repository/datasets/
21 -e file extensions to match, e.g. csv, txt
22 -d local directory hosting files/content to be uploaded
30 echo "Successes: $SUCCESSES Failures: $FAILURES"
31 if [ "$FAILURES" -gt 0 ]; then
39 echo "Sending: ${FILE}"
40 # echo "Running: $CURL --fail [CREDENTIALS] --upload-file $FILE $NEXUS_URL"
41 if ("$CURL" --fail -u "$CREDENTIALS" --upload-file "$FILE" "$NEXUS_URL"); then #> /dev/null 2>&1
42 SUCCESSES=$((SUCCESSES+1))
44 FAILURES=$((FAILURES+1))
48 for FILE in "${UPLOAD_FILES_ARRAY[@]}"; do
52 # Validate/check arguments and variables
54 if [ ! -x "$CURL" ];then
55 echo "CURL was not found in your PATH"; exit 1
57 while getopts hu:p:s:d:e: opt; do
59 u) NEXUS_USERNAME="$OPTARG"
61 p) NEXUS_PASSWORD="$OPTARG"
63 s) NEXUS_URL="$OPTARG"
65 e) FILE_EXTENSION="$OPTARG"
67 d) DIRECTORY="$OPTARG"
68 if [ ! -d "$DIRECTORY" ]; then
69 echo "Error: specified directory invalid"; exit 1
80 shift "$((OPTIND -1))" # Discard the options
81 # Gather location of files to upload (in an array)
82 mapfile -t UPLOAD_FILES_ARRAY < <(find "$DIRECTORY" -name "*$FILE_EXTENSION" -type f -print )
83 if [ "${#UPLOAD_FILES_ARRAY[@]}" -ne 0 ]; then
84 echo "Files found to upload: ${#UPLOAD_FILES_ARRAY[@]}"
85 # echo "Files matching pattern:" # Uncomment for debugging
86 # echo "${UPLOAD_FILES_ARRAY[@]}" # Uncomment for debugging
88 echo "Error: no files found to process matching pattern"
91 if [ -z "$NEXUS_URL" ]; then
92 echo "ERROR: Specifying the upload/repository URL is mandatory"; exit 1
94 if [[ ! "$NEXUS_URL" == "http://"* ]] && \
95 [[ ! "$NEXUS_URL" == "https://"* ]]; then
96 echo "Error: Nexus server must be specified as a URL"; exit 1
99 # Prompt for credentials if not specified explicitly or present in the shell environment
100 if [ -z "$NEXUS_USERNAME" ]; then
101 echo -n "Enter username: "
102 read -r NEXUS_USERNAME
103 if [[ -z "$NEXUS_USERNAME" ]]; then
104 echo "ERROR: Username cannot be empty"; exit 1
107 if [ -z "$NEXUS_PASSWORD" ]; then
108 echo -n "Enter password: "
109 read -s -r NEXUS_PASSWORD # Does not echo to terminal/console
111 if [[ -z "$NEXUS_PASSWORD" ]]; then
112 echo "ERROR: Password cannot be empty"; exit 1
115 CREDENTIALS="$NEXUS_USERNAME:$NEXUS_PASSWORD"
116 # Main script entry point
117 echo "Uploading to: $NEXUS_URL"