aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKylie McClain <kylie@somas.is>2020-07-13 01:15:10 -0400
committerKylie McClain <kylie@somas.is>2020-07-13 01:15:10 -0400
commit5ae56470188a365550a2ff8592bc3fbee4be6f6c (patch)
tree0912b56afcd87b2bdc9b1fd4806e06cdb4df14e6
parentcd19113bce5ab372ddfe73da720c5218e3d5d362 (diff)
downloadidioms-5ae56470188a365550a2ff8592bc3fbee4be6f6c.tar.gz
idioms-5ae56470188a365550a2ff8592bc3fbee4be6f6c.tar.xz
idioms-5ae56470188a365550a2ff8592bc3fbee4be6f6c.zip
ratelimit: make -e *add* strings to result, not overwrite generated nameHEADmain
-rw-r--r--ratelimit.19
-rw-r--r--ratelimit.1.adoc6
-rw-r--r--ratelimit.in4
-rw-r--r--spec/ratelimit_spec.sh7
4 files changed, 16 insertions, 10 deletions
diff --git a/ratelimit.1 b/ratelimit.1
index 3fce994..aebabd5 100644
--- a/ratelimit.1
+++ b/ratelimit.1
@@ -35,19 +35,20 @@ ratelimit \- limit how often a command can be ran
.SH "DESCRIPTION"
.sp
\fBratelimit\fP creates a lock file, runs \fICOMMAND\fP as a child, waits the \fITIME\fP given to elapse, and
-then removes the lock file.
+then removes the lock file. \fITIME\fP is simply passed to sleep(1), so it supports whatever format
+sleep(1) uses.
.sp
If, during the wait period, \fBratelimit\fP is ran again with the same arguments, it will not run the
command and will instead exit 100.
.sp
-If \fB\-e\fP \fIENTROPY\fP is not passed, the lock file is a sum (generated with cksum(1)) of the \fICOMMAND\fP.
+The lock file is a sum (generated with cksum(1)) of any \fB\-e\fP arguments given, and the \fICOMMAND\fP.
.SH "OPTIONS"
.sp
\fB\-e\fP \fIENTROPY\fP
.RS 4
-Add \fIENTROPY\fP as a string used to calculate the filename used for the lock file; this function
+Add \fIENTROPY\fP to the list of strings used to calculate the name of the lock file; this function
is useful for running multiple rate\-limited instances of a command and preventing them from
-clashing. If this is specified, \fICOMMAND\fP is not used in creating a filename.
+clashing.
.RE
.SH "CONTRIBUTING"
.sp
diff --git a/ratelimit.1.adoc b/ratelimit.1.adoc
index 8a586a8..524e5f4 100644
--- a/ratelimit.1.adoc
+++ b/ratelimit.1.adoc
@@ -17,13 +17,13 @@ sleep(1) uses.
If, during the wait period, *ratelimit* is ran again with the same arguments, it will not run the
command and will instead exit 100.
-If *-e* _ENTROPY_ is not passed, the lock file is a sum (generated with cksum(1)) of the _COMMAND_.
+The lock file is a sum (generated with cksum(1)) of any *-e* arguments given, and the _COMMAND_.
== Options
*-e* _ENTROPY_::
- Add _ENTROPY_ as a string used to calculate the filename used for the lock file; this function
+ Add _ENTROPY_ to the list of strings used to calculate the name of the lock file; this function
is useful for running multiple rate-limited instances of a command and preventing them from
- clashing. If this is specified, _COMMAND_ is not used in creating a filename.
+ clashing.
include::footer.adoc[]
diff --git a/ratelimit.in b/ratelimit.in
index 04ad897..5cbbe4f 100644
--- a/ratelimit.in
+++ b/ratelimit.in
@@ -10,7 +10,7 @@ mkdir -p "${XDG_RUNTIME_DIR}"/ratelimit
while getopts :e: arg; do
case "${arg}" in
e)
- entropy=$(printf "%s\n" "${entropy}" "${OPTARG}")
+ entropy=$(printf "%s\n" ${entropy:+"${entropy}"} "${OPTARG}")
;;
?)
error "unknown argument -- ${OPTARG}"
@@ -25,7 +25,7 @@ shift $(( OPTIND - 1 ))
time="$1"; shift
-entropy=$(printf '%s\n' "${entropy:-$@}" | cksum | cut -d' ' -f1)
+entropy=$(printf '%s\n' ${entropy:+"${entropy}"} "$@" | cksum | cut -d' ' -f1)
temp="${XDG_RUNTIME_DIR}"/ratelimit/"${entropy}".lock
[ -f "${temp}" ] && exit 100
diff --git a/spec/ratelimit_spec.sh b/spec/ratelimit_spec.sh
index 48b92be..65dd0d9 100644
--- a/spec/ratelimit_spec.sh
+++ b/spec/ratelimit_spec.sh
@@ -1,7 +1,7 @@
XDG_RUNTIME_DIR="${TMPDIR:-/tmp}"
print_temp() {
- ratelimit ${@:+"$@"} 3 true &
+ ratelimit ${@:+"$@"} 2 true &
(sleep 1 && echo "${XDG_RUNTIME_DIR}"/ratelimit/*.lock)
}
@@ -10,4 +10,9 @@ Describe "ratelimit"
When run print_temp
The stdout should equal "${XDG_RUNTIME_DIR}/ratelimit/2469621148.lock"
End
+
+ It "allows for adding entropy to the lock file's name"
+ When run print_temp -e "entropy"
+ The stdout should equal "${XDG_RUNTIME_DIR}/ratelimit/4069469580.lock"
+ End
End