mirror of
https://github.com/kvz/bash3boilerplate.git
synced 2025-04-14 03:12:57 +00:00
Add tests for megamount
and follow Library export best practices
This commit is contained in:
parent
583efe734b
commit
1271bf807d
@ -54,6 +54,7 @@ Although *3* introduces a node.js dependency, this does allow for easy version p
|
||||
- Add tests for `templater` and follow Library export best practices
|
||||
- Add tests for `ini_val` and follow Library export best practices
|
||||
- Add tests for `parse_url` and follow Library export best practices
|
||||
- Add tests for `megamount` and follow Library export best practices
|
||||
|
||||
### v1.2.1 (2016-02-17)
|
||||
|
||||
@ -111,11 +112,10 @@ $ my_script some more args --blah
|
||||
|
||||
(taken from the [bpkg](https://raw.githubusercontent.com/bpkg/bpkg/master/README.md) project)
|
||||
|
||||
## Todo
|
||||
### Miscellaneous
|
||||
|
||||
- [ ] Make `src` libs adhere to Best practices
|
||||
- [ ] `make build` system for generating custom builds
|
||||
- [x] tests via Travis
|
||||
- In functions, use `local` before every variable declaration
|
||||
- This project settles on two spaces for tabs
|
||||
|
||||
## Authors
|
||||
|
||||
|
@ -1,18 +1,54 @@
|
||||
__dir=$(cd `dirname "${BASH_SOURCE[0]}"` && pwd)
|
||||
#!/usr/bin/env bash
|
||||
# BASH3 Boilerplate: megamount
|
||||
#
|
||||
# This file:
|
||||
#
|
||||
# - Takes a URL (smb, nfs, afs) and tries to mount it at a given target directory
|
||||
# - Forceully unmounts any active mount at the target directory first
|
||||
# - Displays the mounts contents for verification
|
||||
#
|
||||
# Depends on:
|
||||
#
|
||||
# - ./parse_url.sh
|
||||
#
|
||||
# More info:
|
||||
#
|
||||
# - https://github.com/kvz/bash3boilerplate
|
||||
# - http://kvz.io/blog/2013/02/26/introducing-bash3boilerplate/
|
||||
#
|
||||
# Version: 1.2.1
|
||||
#
|
||||
# Authors:
|
||||
#
|
||||
# - Kevin van Zonneveld (http://kvz.io)
|
||||
#
|
||||
# Usage as a function:
|
||||
#
|
||||
# source megamount.sh
|
||||
# megamount smb://janedoe:abc123@192.168.0.1/documents /mnt/documents
|
||||
#
|
||||
# Usage as a command:
|
||||
#
|
||||
# megamount.sh smb://janedoe:abc123@192.168.0.1/documents /mnt/documents
|
||||
#
|
||||
# Licensed under MIT
|
||||
# Copyright (c) 2016 Kevin van Zonneveld (http://kvz.io)
|
||||
|
||||
__dir=$(cd `dirname "${BASH_SOURCE[0]}"` && pwd)
|
||||
source "${__dir}/parse_url.sh"
|
||||
|
||||
function megamount () {
|
||||
local url="${1}"
|
||||
local target="${2}"
|
||||
|
||||
proto=$(parse_url "${url}" "proto")
|
||||
user=$(parse_url "${url}" "user")
|
||||
pass=$(parse_url "${url}" "pass")
|
||||
host=$(parse_url "${url}" "host")
|
||||
port=$(parse_url "${url}" "port")
|
||||
path=$(parse_url "${url}" "path")
|
||||
local proto=$(parse_url "${url}" "proto")
|
||||
local user=$(parse_url "${url}" "user")
|
||||
local pass=$(parse_url "${url}" "pass")
|
||||
local host=$(parse_url "${url}" "host")
|
||||
local port=$(parse_url "${url}" "port")
|
||||
local path=$(parse_url "${url}" "path")
|
||||
|
||||
umount -lf "${target}" || true
|
||||
(umount -lf "${target}" || umount -f "${target}") > /dev/null 2>&1 || true
|
||||
mkdir -p "${target}"
|
||||
if [ "${proto}" = "smb://" ]; then
|
||||
mount -t cifs --verbose -o "username=${user},password=${pass},hard" "//${host}/${path}" "${target}"
|
||||
@ -21,12 +57,19 @@ function megamount () {
|
||||
# afpfsd || echo "Unable to run afpfsd. Does /dev/log exist?" && exit 1
|
||||
mount_afp "${url}" "${target}"
|
||||
elif [ "${proto}" = "nfs://" ]; then
|
||||
mount -t nfs --verbose -o "vers=3,nolock,soft,intr,rsize=32768,wsize=32768" ${host}:/${path} ${target}
|
||||
mount -t nfs --verbose -o "vers=3,nolock,soft,intr,rsize=32768,wsize=32768" "${host}:/${path}" "${target}"
|
||||
else
|
||||
echo "ERR: Unknown protocol ${proto}"
|
||||
echo "ERR: Unknown protocol: '${proto}'"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# chmod 777 "${target}"
|
||||
ls -al "${target}/"
|
||||
}
|
||||
|
||||
if [ "${BASH_SOURCE[0]}" != ${0} ]; then
|
||||
export -f megamount
|
||||
else
|
||||
megamount "${@}"
|
||||
exit ${?}
|
||||
fi
|
||||
|
1
test/fixture/megamount.exitcode
Normal file
1
test/fixture/megamount.exitcode
Normal file
@ -0,0 +1 @@
|
||||
1
|
2
test/fixture/megamount.stdio
Normal file
2
test/fixture/megamount.stdio
Normal file
@ -0,0 +1,2 @@
|
||||
ERR: Unknown protocol: 'foobarfs://'
|
||||
ERR: Unknown protocol: 'foobarfs://'
|
22
test/scenario/megamount/run.sh
Normal file
22
test/scenario/megamount/run.sh
Normal file
@ -0,0 +1,22 @@
|
||||
#!/usr/bin/env bash
|
||||
set -o pipefail
|
||||
set -o errexit
|
||||
set -o nounset
|
||||
# set -o xtrace
|
||||
|
||||
# Set magic variables for current FILE & DIR
|
||||
__dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
__file="${__dir}/$(basename "${BASH_SOURCE[0]}")"
|
||||
__base="$(basename ${__file} .sh)"
|
||||
__root="$(cd "$(dirname $(dirname $(dirname "${__dir}")))" && pwd)"
|
||||
|
||||
__sysTmpDir="${TMPDIR:-/tmp}"
|
||||
__sysTmpDir="${__sysTmpDir%/}" # <-- remove trailing slash on macosx
|
||||
|
||||
# Currently I only know how to test a failing condition here since
|
||||
# it's too invasive to create actual mounts to play with on a system
|
||||
|
||||
bash "${__root}/src/megamount.sh" 'foobarfs://janedoe:abc123@192.168.0.1/documents' "${__sysTmpDir}/mnt/documents" || true
|
||||
|
||||
source ${__root}/src/megamount.sh
|
||||
megamount 'foobarfs://janedoe:abc123@192.168.0.1/documents' "${__sysTmpDir}/mnt/documents"
|
Loading…
x
Reference in New Issue
Block a user