|
- #!/bin/env bash
-
- # © Copyright 2024 Maxime Wack
- # This file is part of git ommix.
-
- # Git ommix 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.
-
- # Git ommix 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/>.
-
- ###############################################################################
- # Use bash "strict mode"
-
- set -uo pipefail
- IFS=$'\n\t'
-
- ###############################################################################
- # Load git ommix functions
- # Load global and local settings if they exist
-
- # Save env variables
- envs=$(env | grep ^GIT_OMMIX)
-
- TEST_OMMIX="${TEST_OMMIX:-}"
- if [[ "$TEST_OMMIX" ]]; then
- source $(dirname $0)/functions
- else
- source /usr/share/git-ommix/functions
- fi
-
- [[ -f "/etc/gitommix.conf" ]] && source "/etc/gitommix.conf"
- [[ -f "$HOME/.config/gitommix.conf" ]] && source "$HOME/.config/gitommix.conf"
-
- # Restore env variables after config load
- eval $envs
-
- ###############################################################################
- # Parse arguments
- # Initialize default values
-
- # If no arg, full help
- (( $# > 0 )) || usage root
- VERB="$1"
-
- # If no object, help for the verb
- (( $# > 1 )) || usage "$VERB"
- OBJECT="$2"
- shift 2
-
- # Default values
- PROVIDER="${PROVIDER:-}"
- METHOD=
- PATIENT="${PATIENT:-}"
- SAMPLE="${SAMPLE:-}"
- DATE="${DATE:-$(date +'%Y-%m-%dT%H:%M:%S')}"
- ID=$(base64 /dev/urandom | tr --delete "/+=" | head --bytes=10)
- INVALIDATE=()
- USE=()
- REVISION_OF=
- MESSAGE2=
- DEBUG="${DEBUG:-}"
-
- # Declare global vars
- HASH=()
- NAME=()
-
- ###############################################################################
- # GETOPT
- #
- # Test getopt
- # Allow a command to fail with !’s side effect on errexit
- # Use return value from ${PIPESTATUS[0]}, because ! hosed $?
- #
- # Set getopt short and long options
- # -temporarily store output to be able to check for errors
- # -activate quoting/enhanced mode (e.g. by writing out “--options”)
- # -pass arguments only via -- "$@" to separate them correctly
- #
- # Read getopt’s quoted output
-
- ! getopt --test > /dev/null
- [[ ${PIPESTATUS[0]} -eq 4 ]] || die 'I’m sorry, `getopt --test` failed in this environment.'
-
- OPTIONS=p:s:m:dv
- LONGOPTS=provider:,method:,patient:,sample:,date:,id:,invalidate:,use:,revision_of:,message:,debug,dry,verbose
-
- ! PARSED=$(getopt --options=$OPTIONS --longoptions=$LONGOPTS --name "git-ommix" -- "$@")
- [[ ${PIPESTATUS[0]} -eq 0 ]] || usage root
-
- eval set -- "$PARSED"
-
- ###############################################################################
- # Read the options and set the parameters values
- # USE and INVALIDATE can be called multiple times
- # and the values are stored in an array
-
- while true; do
- case "$1" in
- --provider)
- PROVIDER="$2"
- shift 2
- ;;
- --method)
- METHOD="$2"
- shift 2
- ;;
- -p|--patient)
- PATIENT="$2"
- shift 2
- ;;
- -s|--sample)
- SAMPLE="$2"
- shift 2
- ;;
- --date)
- DATE="$2"
- shift 2
- ;;
- --id)
- ID="$2"
- shift 2
- ;;
- --use)
- USE+=("$2")
- shift 2
- ;;
- --invalidate)
- INVALIDATE+=("$2")
- shift 2
- ;;
- --revision_of)
- REVISION_OF="$2"
- shift 2
- ;;
- -m|--message)
- MESSAGE2="$2"
- shift 2
- ;;
- -d|--debug)
- DEBUG="debug"
- shift
- ;;
- --dry)
- DEBUG="dry"
- shift
- ;;
- -v|--verbose)
- DEBUG="verbose"
- shift
- ;;
- --)
- shift
- break
- ;;
- *)
- echo "Programming error"
- exit 3
- ;;
- esac
- done
-
- # Replace AUTHOR with PROVIDER if given
- AUTHOR="${PROVIDER:-$GIT_OMMIX_DEFAULT_AUTHOR}"
- PROVIDER="${PROVIDER%% <*}"
- PROVIDER="${PROVIDER/ /_}"
-
- # The remaining arguments will be files when adding data|result
- # or a list of commit hashes or name when listing
- # If no file is given, use '*'
- QUERY=("${1:-}")
- FILES=("${@:-*}")
- HASHES=("${@:-}")
-
- [[ "$HASHES" == "-" ]] && HASHES="$(cat /dev/stdin)"
-
- # Output parameter values for debug
- [[ "$DEBUG" == "debug" ]] && echo "####################################
- Verb: $VERB
- Object: $OBJECT
- Provider: $PROVIDER
- Author: $AUTHOR
- Method: $METHOD
- Patient: $PATIENT
- Sample: $SAMPLE
- Date: $DATE
- Id: $ID
- Use: ${USE[@]}
- Invalidate: ${INVALIDATE[@]}
- Revision_of: $REVISION_OF
- Message: $MESSAGE2
- Files: ${FILES[@]}
- Hashes: ${HASHES[@]}
- "
-
- ###############################################################################
- # Setup git ommix
- # Record the current directory for file operations
- # Create the git ommix directory if it doesn't exist yet and cd
-
- CURRENT_DIR="$PWD"
- mkdir -p "$GIT_OMMIX_REPO_DIR" || die "Unable to create the git ommix base directory at: $GIT_OMMIX_REPO_DIR
- Please check your permissions"
- cd "$GIT_OMMIX_REPO_DIR" || die "Unable to access the git ommix base directory at: $GIT_OMMIX_REPO_DIR
- Please check your permissions"
-
- ###############################################################################
- # Run the given verb
-
- case "$VERB" in
- add) add;;
- list) list;;
- get) get;;
- # Unknown verb -> usage root
- *) usage root;;
- esac
|