Fix: Update Gradle publish job
[releng/global-jjb.git] / shell / nexus-upload.sh
1 #!/usr/bin/env bash
2 # SPDX-License-Identifier: Apache-2.0
3 # Copyright 2024 The LMinux Foundation <matthew.watkins@linuxfoundation.org>
4 # Uncomment to enable debugging
5 # set -vx
6 # Initialise variables
7 DIRECTORY="."
8 FILE_EXTENSION=""
9 # Count file upload successes/failures
10 SUCCESSES="0"; FAILURES="0"
11 # Shared functions
12 show_help() {
13     # Command usage help
14     cat << EOF
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
23 EOF
24 }
25 error_help() {
26     show_help >&2
27     exit 1
28 }
29 transfer_report() {
30     echo "Successes: $SUCCESSES   Failures: $FAILURES"
31     if [ "$FAILURES" -gt 0 ]; then
32         exit 1
33     else
34         exit 0
35     fi
36 }
37 curl_upload() {
38     FILE="$1"
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))
43     else
44         FAILURES=$((FAILURES+1))
45     fi
46 }
47 process_files() {
48     for FILE in "${UPLOAD_FILES_ARRAY[@]}"; do
49         curl_upload "$FILE"
50     done
51 }
52 # Validate/check arguments and variables
53 CURL=$(which curl)
54 if [ ! -x "$CURL" ];then
55     echo "CURL was not found in your PATH"; exit 1
56 fi
57 while getopts hu:p:s:d:e: opt; do
58     case $opt in
59         u)  NEXUS_USERNAME="$OPTARG"
60             ;;
61         p)  NEXUS_PASSWORD="$OPTARG"
62             ;;
63         s)  NEXUS_URL="$OPTARG"
64             ;;
65         e)  FILE_EXTENSION="$OPTARG"
66             ;;
67         d)  DIRECTORY="$OPTARG"
68             if [ ! -d "$DIRECTORY" ]; then
69                 echo "Error: specified directory invalid"; exit 1
70             fi
71             ;;
72         h|?)
73             show_help
74             exit 0
75             ;;
76         *)
77             error_help
78         esac
79 done
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
87 else
88     echo "Error: no files found to process matching pattern"
89     exit 1
90 fi
91 if [ -z "$NEXUS_URL" ]; then
92     echo "ERROR: Specifying the upload/repository URL is mandatory"; exit 1
93 else
94     if  [[ ! "$NEXUS_URL" == "http://"* ]] && \
95         [[ ! "$NEXUS_URL" == "https://"* ]]; then
96         echo "Error: Nexus server must be specified as a URL"; exit 1
97     fi
98 fi
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
105     fi
106 fi
107 if [ -z "$NEXUS_PASSWORD" ]; then
108     echo -n "Enter password: "
109     read -s -r NEXUS_PASSWORD  # Does not echo to terminal/console
110     echo ""
111     if [[ -z "$NEXUS_PASSWORD" ]]; then
112         echo "ERROR: Password cannot be empty"; exit 1
113     fi
114 fi
115 CREDENTIALS="$NEXUS_USERNAME:$NEXUS_PASSWORD"
116 # Main script entry point
117 echo "Uploading to: $NEXUS_URL"
118 process_files
119 transfer_report