commit 4e9cb51f614a7fbffca15b7f2b64eb4ccf1ed8d8
parent 997d857a4ea14eb91620790e5322c00d1ded0054
Author: Jacob R. Edwards <n/a>
Date: Wed, 28 Dec 2022 13:12:17 -0800
Move some of the tag script to the new tags script
The stuff to find the tags directory was previously built-in to the
tag script, but now it's in the new tags script. This makes it
public for use in other programs and of course makes tag more
focused. The new script also provides the ability to list the tag
files.
Also, the tag -p[rint] flag was changed to -l, for load.
Diffstat:
2 files changed, 66 insertions(+), 32 deletions(-)
diff --git a/local/bin/bin/tag b/local/bin/bin/tag
@@ -2,23 +2,21 @@
# Copyright 2022 Jacob R. Edwards
# Filesystem tagging system
#
-# In write mode, each name from the standard input (made relative to
-# the current directory or base (set with -b)) is appended to the
-# given tagfiles if they don't contain them already. Tagfiles are
-# paths relative to the current directory unless a tag directory is
-# given (-t).
+# By default, each line from the standard input is made relative to
+# the base directory (set with -b or -a) unless in raw mode (-r)
+# and appended to the given tag files if they're missing. Tag files
+# are relative to the current directory unless the tag directory is
+# set (with either -t or -a).
#
-# Given the automatic flag (-a), the script attempts to resolve the
-# tags and base directories if they are not set. If base is defined,
-# tags is gotten using that--otherwise the 'tags' directory is searched
-# for in every parent directory of the current directory. The base
-# is generated by getting the dirname(1) of the tags directory.
+# To automatically resolve the tag and base directories, use the -a
+# flag. If the base directory is set, tags is resolved using that,
+# otherwise, the 'tags' directory is searched for in every parent
+# directory of the current directory.
#
-# In raw mode (-r), the tagged information is not altered in any way
-# and is suitable for tagging things other than files.
+# The base is generated by getting the dirname(1) of the tags directory.
#
-# In read mode (-p), names are not read from the standard input and
-# instead the given tagfiles are printed.
+# To read a tag file, use the -l flag which, unless in raw mode, roots
+# each name appearing in the tag files.
error() {
echo "error: $1" 1>&2
@@ -29,22 +27,11 @@ usage() {
{
test $# -gt 0 &&
echo "error: $1"
- echo 'usage: tag [-arp] [-b base] [-t tags] tagfile [...]'
+ echo 'usage: tag [-alr] [-b base] [-t tags] tagfile [...]'
} 1>&2
exit 1
}
-findtags() {
- dir="$1"
- while test "$dir" != '/' -a '!' -d "$dir"/tags
- do
- dir="$(dirname "$dir")"
- done
- ! test -d "$dir"/tags &&
- return 1
- echo "$dir"/tags
-}
-
base() (
test "$base" &&
cd "$base"
@@ -77,10 +64,10 @@ do
case "$1" in
(-a)
auto=true ;;
+ (-l)
+ read=true ;;
(-r)
raw=true ;;
- (-p)
- read=true ;;
(-b)
base="$2"
shift ;;
@@ -99,7 +86,7 @@ do
done
test $# -eq 0 &&
- usage 'No tag files given'
+ usage 'No tag files given'
if $auto
then
@@ -108,15 +95,16 @@ then
! test -d "$base"/tags &&
error "The 'tags' directory doesn't exist in base"
tags="$base"/tags
- elif ! tags="$(findtags "$PWD")"; then
+ elif ! tags="$(command tags find)"; then
error 'Unable to find tags directory'
fi
fi
! test "$base" &&
- base="$(dirname "$tags")"
+ base="$(command tags base "$tags")"
fi
-if $read; then
+if $read
+then
if $raw; then
tags cat "$@"
else
diff --git a/local/bin/bin/tags b/local/bin/bin/tags
@@ -0,0 +1,46 @@
+#!/bin/sh
+# Copyright 2022 Jacob R. Edwards
+# Tags resolver
+#
+# There are three commands: find, which finds the tag directory;
+# base, which finds the tag base directory; and list, which lists tag
+# files within a tag directory.
+
+usage() {
+ echo "${1:+error: $1
+}"'usage: tags find [from]
+ tags base [tags]
+ tags list [tags]' 1>&2
+ exit 1
+}
+
+findtags() (
+ test "$1" &&
+ cd "$1"
+ while test '!' -d tags -a "$PWD" '!=' /
+ do
+ cd ..
+ done
+ ! test -d tags &&
+ exit 1
+ echo "$PWD"/tags
+)
+
+set -e
+
+case "$1$#" in
+(find[12])
+ shift
+ findtags "$@"
+ ;;
+(base[12])
+ tags="${2:-"$(findtags)"}"
+ dirname "$tags"
+ ;;
+(list[12])
+ tags="${2:-"$(findtags)"}"
+ (cd "$tags" && find . -type f) | cut -c3-
+ ;;
+(*)
+ usage "$1: Invalid command"
+esac