Fix: Skip sigul installation on Ubuntu
[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 # Count file upload successes/failures
7 SUCCESSES="0"; FAILURES="0"
8 # Shared functions
9 show_help() {
10     # Command usage help
11     cat << EOF
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
20 EOF
21 }
22 error_help() {
23     show_help >&2
24     exit 1
25 }
26 transfer_report() {
27     echo "Successes: $SUCCESSES   Failures: $FAILURES"
28     if [ "$FAILURES" -gt 0 ]; then
29         exit 1
30     else
31         exit 0
32     fi
33 }
34 curl_upload() {
35     FILE="$1"
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))
40     else
41         FAILURES=$((FAILURES+1))
42     fi
43 }
44 process_files() {
45     for FILE in "${UPLOAD_FILES_ARRAY[@]}"; do
46         curl_upload "$FILE"
47     done
48 }
49 # Validate/check arguments and variables
50 CURL=$(which curl)
51 if [ ! -x "$CURL" ];then
52     echo "CURL was not found in your PATH"; exit 1
53 fi
54 while getopts hu:p:s:d:e: opt; do
55     case $opt in
56         u)  NEXUS_USERNAME="$OPTARG"
57             ;;
58         p)  NEXUS_PASSWORD="$OPTARG"
59             ;;
60         s)  NEXUS_URL="$OPTARG"
61             ;;
62         e)  FILE_EXTENSION="$OPTARG"
63             ;;
64         d)  DIRECTORY="$OPTARG"
65             if [ ! -d "$DIRECTORY" ]; then
66                 echo "Error: specified directory invalid"; exit 1
67             fi
68             ;;
69         h|?)
70             show_help
71             exit 0
72             ;;
73         *)
74             error_help
75         esac
76 done
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
84 else
85     echo "Error: no files found to process matching pattern"
86     exit 1
87 fi
88 if [ -z "$NEXUS_URL" ]; then
89     echo "ERROR: Specifying the upload/repository URL is mandatory"; exit 1
90 else
91     if  [[ ! "$NEXUS_URL" == "http://"* ]] && \
92         [[ ! "$NEXUS_URL" == "https://"* ]]; then
93         echo "Error: Nexus server must be specified as a URL"; exit 1
94     fi
95 fi
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
102     fi
103 fi
104 if [ -z "$NEXUS_PASSWORD" ]; then
105     echo -n "Enter password: "
106     read -s -r NEXUS_PASSWORD  # Does not echo to terminal/console
107     echo ""
108     if [[ -z "$NEXUS_PASSWORD" ]]; then
109         echo "ERROR: Password cannot be empty"; exit 1
110     fi
111 fi
112 CREDENTIALS="$NEXUS_USERNAME:$NEXUS_PASSWORD"
113 # Main script entry point
114 echo "Uploading to: $NEXUS_URL"
115 process_files
116 transfer_report