aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--README.md2
-rwxr-xr-xctmg.sh21
2 files changed, 21 insertions, 2 deletions
diff --git a/README.md b/README.md
index 322e72b..568a041 100644
--- a/README.md
+++ b/README.md
@@ -11,6 +11,8 @@
ctmg close container_path
ctmg list
+Calling `ctmg` with no arguments will call `list` if there are any containers open, and otherwise show the usage screen. Calling `ctmg` with a filename argument will call `open` if it is not already open and otherwise will call `close`.
+
### Examples
#### Create a 100MiB encrypted container called "example"
diff --git a/ctmg.sh b/ctmg.sh
index 6231672..9eaa79c 100755
--- a/ctmg.sh
+++ b/ctmg.sh
@@ -135,9 +135,26 @@ cmd_delete() {
cmd_list() {
[[ $# -ne 0 ]] && die "Usage: $PROGRAM list"
local mount_points="$(sed -n "s:^/dev/mapper/${CT_MAPPER_PREFIX}[^ ]* \\([^ ]\\+\\).*:\\1:p" /proc/mounts)"
- [[ -n $mount_points ]] && echo -e "$mount_points"
+ [[ -n $mount_points ]] && echo -e "$mount_points" && return 0
+ return 1
}
+cmd_auto() {
+ if [[ $# -eq 0 ]]; then
+ cmd_list "$@" || cmd_usage
+ elif [[ $# -eq 1 ]]; then
+ initialize_container "$1"
+ if [[ -e $mapper_path ]]; then
+ cmd_close "$@"
+ else
+ cmd_open "$@"
+ fi
+ else
+ cmd_usage "$@"
+ fi
+}
+
+
PROGRAM="$(basename "$0")"
[[ $UID != 0 ]] && exec sudo -p "$PROGRAM must be run as root. Please enter the password for %u to continue: " "$(readlink -f "$0")" "$@"
@@ -149,6 +166,6 @@ case "$1" in
c|close) shift; cmd_close "$@" ;;
l|list) shift; cmd_list "$@" ;;
o|open) shift; cmd_open "$@" ;;
- *) cmd_open "$@" ;;
+ *) cmd_auto "$@" ;;
esac
exit 0