commit 43dc548a3dae4e050b1cb97337c753c8b319976f Author: Antonin Portelli Date: Fri Mar 25 18:27:35 2022 +0000 initial commit with find function diff --git a/functions.sh b/functions.sh new file mode 100755 index 0000000..e10750b --- /dev/null +++ b/functions.sh @@ -0,0 +1,12 @@ +miria_error() { + echo "error: $1" 1>&2 +} + +miria_get_url() { + if [ -e ~/.miria-url ]; then + miria_url="$(cat ~/.miria-url)" + else + echo 'error: file ~/.miria-url not found' 1>&2 + exit 1 + fi +} \ No newline at end of file diff --git a/miria-find b/miria-find new file mode 100755 index 0000000..4d19862 --- /dev/null +++ b/miria-find @@ -0,0 +1,55 @@ +#!/usr/bin/env bash + +set -uoe pipefail + +script_dir=$(dirname $(readlink -f $0)) +source ${script_dir}/functions.sh +miria_get_url + +print_usage() { + echo "usage: $(basename $0) -p -n [-x]" 1>&2 + echo "" 1>&2 + echo "flags:" 1>&2 + echo " -p: project name in Miria archive" 1>&2 + echo " -n: file pattern (use * as wildcard)" 1>&2 + echo " -x: show raw XML answer" 1>&2 +} + +project='' +pattern='' +xml='false' +while getopts 'xp:n:' flag; do + case "${flag}" in + p) project="${OPTARG}" ;; + n) pattern="${OPTARG}" ;; + x) xml='true' ;; + *) print_usage + exit 1 ;; + esac +done +if [[ -z "${project}" ]]; then + miria_error "missing -p flag" + print_usage + exit 1 +fi +if [[ -z "${pattern}" ]]; then + miria_error "missing -n flag" + print_usage + exit 1 +fi +tmp_xml="$(mktemp)" +base="$(basename "${pattern}")" +dir="$(dirname "${pattern}")" +if [[ "${dir}" == "." ]]; then + dir='' +fi +curl -s "${miria_url}/fileSearch?filename=${base}&path=${project}@/${dir}" > ${tmp_xml} +if [[ "${xml}" == "true" ]]; then + cat ${tmp_xml} +else + xmllint --xpath '//file/@name' ${tmp_xml} \ + | sed -E 's/ *name="(.+)"/\1/g' \ + | sed -E "s/archive@${project}://g" \ + | sed -E 's/\/\//\//g' +fi +rm -f ${tmp_xml}