mirror of
https://github.com/ivuorinen/dotfiles.git
synced 2026-03-12 01:03:33 +00:00
feat(bin): update scripts to function format
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
#!/usr/bin/env bash
|
||||
##
|
||||
# This script contains helper for sha256 validating your downloads
|
||||
#
|
||||
# This script contains a helper for sha256 validating your downloads
|
||||
#
|
||||
# Source: https://gist.github.com/onnimonni/b49779ebc96216771a6be3de46449fa1
|
||||
# Author: Onni Hakala
|
||||
@@ -9,44 +9,60 @@
|
||||
# Updated by Ismo Vuorinen <https://github.com/ivuorinen> 2022
|
||||
##
|
||||
|
||||
if ! command -v sha256 &> /dev/null; then
|
||||
echo "git could not be found, please install it first"
|
||||
exit
|
||||
fi
|
||||
set -euo pipefail
|
||||
|
||||
# Stop program and give error message
|
||||
# $1 - error message (string)
|
||||
function error
|
||||
error()
|
||||
{
|
||||
echo "(!) ERROR: $1"
|
||||
echo "(!) ERROR: $1" >&2
|
||||
exit 1
|
||||
}
|
||||
|
||||
# return sha256sum for file
|
||||
# Check for sha256sum command
|
||||
if ! command -v sha256sum &> /dev/null; then
|
||||
error "sha256sum could not be found, please install it first"
|
||||
fi
|
||||
|
||||
# Return sha256sum for file
|
||||
# $1 - filename (string)
|
||||
function get_sha256sum
|
||||
get_sha256sum()
|
||||
{
|
||||
sha256sum "$1" | head -c 64
|
||||
}
|
||||
|
||||
# Good variable names pls
|
||||
filename=$1
|
||||
file_hash=$2
|
||||
# Validate input arguments
|
||||
validate_inputs()
|
||||
{
|
||||
if [ -z "${filename:-}" ]; then
|
||||
error "You need to provide filename as the first parameter"
|
||||
fi
|
||||
|
||||
# Check input
|
||||
if [ -z "$filename" ]; then
|
||||
error "You need to provide filename in first parameter"
|
||||
fi
|
||||
if [ -z "${file_hash:-}" ]; then
|
||||
error "You need to provide sha256sum as the second parameter"
|
||||
fi
|
||||
}
|
||||
|
||||
if [ -z "$file_hash" ]; then
|
||||
error "You need to provide sha256sum in second parameter"
|
||||
fi
|
||||
# Main validation logic
|
||||
validate_file()
|
||||
{
|
||||
if [ ! -f "$filename" ]; then
|
||||
error "File $filename doesn't exist"
|
||||
elif [ "$(get_sha256sum "$filename")" = "$file_hash" ]; then
|
||||
echo "(*) Success: $filename matches provided sha256sum"
|
||||
else
|
||||
error "$filename doesn't match provided sha256sum"
|
||||
fi
|
||||
}
|
||||
|
||||
# Check if the file is valid
|
||||
if [ ! -f "$filename" ]; then
|
||||
error "File $filename doesn't exist"
|
||||
elif [ "$(get_sha256sum "$filename")" = "$file_hash" ]; then
|
||||
echo "(*) Success: $filename matches provided sha256sum"
|
||||
else
|
||||
error "$filename doesn't match provided sha256sum"
|
||||
fi
|
||||
# Main function
|
||||
main()
|
||||
{
|
||||
filename=$1
|
||||
file_hash=$2
|
||||
|
||||
validate_inputs
|
||||
validate_file
|
||||
}
|
||||
|
||||
main "$@"
|
||||
|
||||
Reference in New Issue
Block a user