11# debounce
22
33<p align =" center " >
4- <img src =" logo.jpeg " />
4+ <img src =" logo.jpeg " alt = " debounce logo " />
55</p >
66
7-
87<!-- vim-markdown-toc GFM -->
98
10- * [ Introduction] ( #introduction )
11- * [ Examples] ( #examples )
12- * [ A command without arguments] ( #a-command-without-arguments )
13- * [ A command with arguments] ( #a-command-with-arguments )
14- * [ Using Shell Variables] ( #using-shell-variables )
15- * [ More Complex Commands] ( #more-complex-commands )
16- * [ Installation] ( #installation )
9+ - [ Introduction] ( #introduction )
10+ - [ Installation] ( #installation )
11+ - [ Go Install] ( #go-install )
12+ - [ Using Ubi] ( #using-ubi )
13+ - [ Download a Release] ( #download-a-release )
14+ - [ Examples] ( #examples )
15+ - [ A command without arguments] ( #a-command-without-arguments )
16+ - [ A command with arguments] ( #a-command-with-arguments )
17+ - [ Using Shell Variables] ( #using-shell-variables )
18+ - [ More Complex Commands] ( #more-complex-commands )
19+ - [ Available Flags] ( #available-flags )
20+ - [ --cache-dir] ( #--cache-dir )
21+ - [ --status] ( #--status )
22+ - [ Resetting the Cache] ( #resetting-the-cache )
23+ - [ --version] ( #--version )
24+ - [ --help] ( #--help )
25+ - [ Caveats] ( #caveats )
1726
1827<!-- vim-markdown-toc -->
1928
2029## Introduction
2130
22- debounce is a simple utility to limit the rate at which a command can fire.
31+ ` debounce ` is a simple utility to limit the rate at which a command can fire.
32+ This is useful in scenarios where you want to avoid repetitive command
33+ executions, like in automation scripts, cron jobs, or continuous integration
34+ pipelines.
2335
24- The arguments are :
36+ The command format is :
2537
2638``` bash
2739debounce < integer> < unit> < command>
2840```
2941
3042Available units are:
3143
32- * seconds (s)
33- * minutes (m)
34- * hours (h)
44+ - seconds (s)
45+ - minutes (m)
46+ - hours (h)
3547
3648The following are equivalent:
3749
@@ -53,10 +65,55 @@ debounce 1 hour date
5365debounce 1 hours date
5466```
5567
68+ ## Installation
69+
70+ Choose from the following options to install ` debounce ` .
71+
72+ ### Go Install
73+
74+ ``` bash
75+ go install github.com/oalders/debounce@latest
76+ ```
77+
78+ or for a specific version:
79+
80+ ``` bash
81+ go install github.com/oalders/debounce@v0.3.0
82+ ```
83+
84+ ### Using Ubi
85+
86+ You can use [ ubi] ( https://github.com/houseabsolute/ubi ) to install ` debounce ` .
87+
88+ ``` bash
89+ #! /usr/bin/env bash
90+
91+ set -eux -o pipefail
92+
93+ # Choose a directory in your $PATH
94+ dir=" $HOME /local/bin"
95+
96+ if [ ! " $( command -v ubi) " ]; then
97+ curl --silent --location \
98+ https://raw.githubusercontent.com/houseabsolute/ubi/master/bootstrap/bootstrap-ubi.sh |
99+ TARGET=$dir sh
100+ fi
101+
102+ ubi --project oalders/debounce --in " $dir "
103+ ```
104+
105+ ### Download a Release
106+
107+ You can download the latest release directly from
108+ [ here] ( https://github.com/oalders/debounce/releases ) .
109+
56110## Examples
57111
58112### A command without arguments
59113
114+ This runs the command without any parameters but prevents repeated execution
115+ within the set time window.
116+
60117``` bash
61118$ debounce 2 seconds date
62119Mon Aug 5 23:09:09 EDT 2024
@@ -66,9 +123,9 @@ $ debounce 2 seconds date
66123
67124### A command with arguments
68125
69- This command uses < https://github.com/houseabsolute/ubi > to install
70- < https://github.com/oalders/is > into the current directory. The command will
71- not be run more than once every 8 hours.
126+ This example uses [ ubi ] ( https://github.com/houseabsolute/ubi ) to install
127+ [ is ] ( https://github.com/oalders/is ) into the current directory. The command
128+ won't be executed more than once every 8 hours.
72129
73130``` bash
74131$ debounce 8 hours ubi --verbose --project oalders/is --in .
@@ -79,45 +136,119 @@ $ debounce 8 hours ubi --verbose --project oalders/is --in .
79136
80137### Using Shell Variables
81138
82- Remember to single quote variables which shouldn't be expanded until the
83- command is run.
139+ Remember to single quote variables which shouldn't be expanded until the command
140+ is run.
84141
85142``` bash
86143debounce 10 s zsh -c ' echo $PWD'
87144```
88145
89146### More Complex Commands
90147
91- You can use ` && ` and ` || ` in your commands. You'll want to quote your command
92- to ensure that the entire command is passed to ` debounce ` .
148+ You can use ` && ` and ` || ` in your commands. You'll want to quote your command to
149+ ensure that the entire command is passed to ` debounce ` .
93150
94151``` bash
95152debounce 2 s bash -c ' sleep 2 && date'
96153```
97154
98- ## Installation
155+ ## Available Flags
99156
100- Choose from the following options to install ` debounce ` .
157+ It's important to add ` debounce ` flags before other command arguments to avoid
158+ confusion between ` debounce ` flags and flags meant for your command.
159+
160+ Good: ✅
161+
162+ ``` shell
163+ debounce --debug 90 s curl https://www.olafalders.com
164+ ```
165+
166+ Bad: 💥
167+
168+ ``` shell
169+ $ debounce 90 s curl https://www.prettygoodping.com --debug
170+ 🚀 Running command: curl https://www.prettygoodping.com --debug
171+ curl: option --debug: is unknown
172+ ```
173+
174+ You could be explicit about this by using ` -- ` as a visual indicator that flag
175+ parsing has ended.
101176
102- 1 . [ Download a release] ( https://github.com/oalders/debounce/releases )
103- 1 . Use ` go install `
104- * ` go install github.com/oalders/debounce@latest `
105- * ` go install github.com/oalders/debounce@v0.2.0 `
106- 1 . Use [ ubi] ( https://github.com/houseabsolute/ubi )
177+ ``` shell
178+ debounce --debug 90 s -- curl https://www.prettygoodping.com
179+ ```
180+
181+ ### --cache-dir
182+
183+ Specify an alternate cache directory to use. The directory must already exist.
107184
108185``` bash
109- #! /usr/bin/env bash
186+ debounce --cache-dir /tmp 30 s date
187+ ```
110188
111- set -e -u -x -o pipefail
189+ ### --status
112190
113- # Or choose a different dir in your $PATH
114- dir=" $HOME /local/bin"
191+ Print debounce status information for a command.
115192
116- if [ ! " $( command -v ubi) " ]; then
117- curl --silent --location \
118- https://raw.githubusercontent.com/houseabsolute/ubi/master/bootstrap/bootstrap-ubi.sh |
119- TARGET=$dir sh
120- fi
193+ ``` bash
194+ debounce --status 30 s date
195+ 📁 cache location: /Users/olaf/.cache/debounce/0e87632cd46bd4907c516317eb6d81fe0f921a23c7643018f21292894b470681
196+ 🚧 cache last modified: Thu, 19 Sep 2024 08:28:20 EDT
197+ ⏲️ debounce interval: 00:00:30
198+ 🕰️ cache age: 00:00:12
199+ ⏳ time remaining: 00:00:17
200+ ```
121201
122- ubi --project oalders/debounce --in " $dir "
202+ #### Resetting the Cache
203+
204+ Since the cache is just a file, you can ` rm ` the cache location file whenever
205+ you'd like to start fresh.
206+
207+ ``` shell
208+ rm /Users/olaf/.cache/debounce/0e87632cd46bd4907c516317eb6d81fe0f921a23c7643018f21292894b470681
209+ ```
210+
211+ ### --version
212+
213+ Prints current version.
214+
215+ ``` bash
216+ debounce --version
217+ 0.3.0
218+ ```
219+
220+ ### --help
221+
222+ Displays usage instructions.
223+
224+ ``` text
225+ debounce --help
226+ Usage: debounce <quantity> <unit> <command> ... [flags]
227+
228+ limit the rate at which a command can fire
229+
230+ Arguments:
231+ <quantity> Quantity of time
232+ <unit> s,second,seconds,m,minute,minutes,h,hour,hours
233+ <command> ... Command to run
234+
235+ Flags:
236+ -h, --help Show context-sensitive help.
237+ --debug Print debugging info to screen
238+ --version Print version to screen
239+ --status Print cache information for a command without running it
240+ --cache-dir=STRING Override the default cache directory
123241```
242+
243+ ## Caveats
244+
245+ Under the hood, ` debounce ` creates or updates a cache file to track when a
246+ command was run successfully. This means that, under the right conditions, it's
247+ entirely possible to kick off two long-running tasks in parallel without
248+ ` debounce ` knowing about it.
249+
250+ Additionally, if a command fails, the cache file will not be created or updated.
251+
252+ I've created this tool in a way that meets my needs. I will consider pull
253+ requests for additional functionality to address issues like these. Please get
254+ in touch with me first to discuss your feature if you'd like to add something.
0 commit comments