feat: Add script.sh with tests
This commit is contained in:
		
							
								
								
									
										101
									
								
								src/lib/script.sh
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										101
									
								
								src/lib/script.sh
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,101 @@
 | 
			
		||||
#! /usr/bin/env bash
 | 
			
		||||
 | 
			
		||||
# script.sh - utilitary functions.
 | 
			
		||||
# Copyright (c) 2022 ge <https://nixhacks.net/>
 | 
			
		||||
#
 | 
			
		||||
# This program is free software: you can redistribute it and/or modify
 | 
			
		||||
# it under the terms of the GNU General Public License as published by
 | 
			
		||||
# the Free Software Foundation, either version 3 of the License, or
 | 
			
		||||
# (at your option) any later version.
 | 
			
		||||
#
 | 
			
		||||
# This program is distributed in the hope that it will be useful,
 | 
			
		||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
			
		||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
			
		||||
# GNU General Public License for more details.
 | 
			
		||||
#
 | 
			
		||||
# You should have received a copy of the GNU General Public License
 | 
			
		||||
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
 | 
			
		||||
 | 
			
		||||
validate_sources() {
 | 
			
		||||
    # Check sources array.
 | 
			
		||||
    #
 | 
			
		||||
    # Usage: validate_sources ARRAY
 | 
			
		||||
 | 
			
		||||
    # Allowed URI schemes: file, mysql postgres, sqlite
 | 
			
		||||
    # No required schemes.
 | 
			
		||||
 | 
			
		||||
    local array=("$@")
 | 
			
		||||
    local scheme=
 | 
			
		||||
 | 
			
		||||
    for uri in "${array[@]}"; do
 | 
			
		||||
        scheme="${uri%%:*}"
 | 
			
		||||
        case "$scheme" in
 | 
			
		||||
            file|mysql|postgres|sqlite) : ;; # do nothing, this is OK
 | 
			
		||||
            *) echo "Error: Unsupported URI scheme: $scheme" >&2; exit 1;;
 | 
			
		||||
        esac
 | 
			
		||||
    done
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
validate_targets() {
 | 
			
		||||
    # Check targets array.
 | 
			
		||||
    #
 | 
			
		||||
    # Usage: validate_targets ARRAY
 | 
			
		||||
 | 
			
		||||
    # Allowed URI schemes: file, ftp, sftp, rsync, s3, swift, sj, dav, davs
 | 
			
		||||
    # Required schemes (one or more times): file
 | 
			
		||||
 | 
			
		||||
    local array=("$@")
 | 
			
		||||
    local scheme=
 | 
			
		||||
    local file_scheme=0
 | 
			
		||||
 | 
			
		||||
    for uri in "${array[@]}"; do
 | 
			
		||||
        scheme="${uri%%:*}"
 | 
			
		||||
        case "$scheme" in
 | 
			
		||||
            file|ftp|sftp|rsync|s3|swift|sj|dav|davs)
 | 
			
		||||
                if [[ "$scheme" == file ]]; then
 | 
			
		||||
                    (( file_scheme++ )) || true
 | 
			
		||||
                fi
 | 
			
		||||
            ;;
 | 
			
		||||
            *) echo "Error: Unsupported URI scheme: $scheme" >&2; exit 1;;
 | 
			
		||||
        esac
 | 
			
		||||
    done
 | 
			
		||||
 | 
			
		||||
    if [ "$file_scheme" -eq 0 ]; then
 | 
			
		||||
        echo "Error: 'file' scheme is not set in targets." \
 | 
			
		||||
            "You must provide one or more targets with 'file' scheme." >&2
 | 
			
		||||
        exit 1
 | 
			
		||||
    fi
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
source_script() {
 | 
			
		||||
    # Safely as possible source backup script.
 | 
			
		||||
    #
 | 
			
		||||
    # Usage: source_script SCRIPT
 | 
			
		||||
 | 
			
		||||
    local script="$1"
 | 
			
		||||
 | 
			
		||||
    if ! test -f "$script"; then
 | 
			
		||||
        echo "Error: No such file: $script" >&2; exit 1
 | 
			
		||||
    fi
 | 
			
		||||
 | 
			
		||||
    # Dry run script, check syntax. See set(1p)
 | 
			
		||||
    if ! bash -n "$script"; then
 | 
			
		||||
        echo Error: Please check your syntax >&2; exit 1
 | 
			
		||||
    fi
 | 
			
		||||
 | 
			
		||||
    # Source script
 | 
			
		||||
    . "$@"
 | 
			
		||||
 | 
			
		||||
    # Check required variables
 | 
			
		||||
    if [[ "$sources" ]]; then
 | 
			
		||||
        validate_sources "${sources[@]}"
 | 
			
		||||
    else
 | 
			
		||||
        echo Error: sources array is not set >&2; exit 1
 | 
			
		||||
    fi
 | 
			
		||||
 | 
			
		||||
    if [[ "$targets" ]]; then
 | 
			
		||||
        validate_targets "${targets[@]}"
 | 
			
		||||
    else
 | 
			
		||||
        echo Error: targets array is not set >&2; exit 1
 | 
			
		||||
    fi
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user