Skip to content

Commit 535cb2f

Browse files
authored
Merge pull request #3 from oalders/status
Add --status flag
2 parents c41ea3e + 987c447 commit 535cb2f

File tree

17 files changed

+454
-105
lines changed

17 files changed

+454
-105
lines changed

.github/workflows/test.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
---
22
name: test
33

4+
env:
5+
GITHUB_TOKEN: ${{ github.token }}
6+
47
on:
58
pull_request:
69
push:
@@ -32,7 +35,7 @@ jobs:
3235
run: npm install -g bats
3336

3437
- name: bats test
35-
run: echo $PWD && ls && bats test
38+
run: echo $PWD && ls && bats --verbose-run test
3639

3740
- name: Upload coverage to Codecov
3841
uses: codecov/codecov-action@v4
@@ -84,6 +87,8 @@ jobs:
8487
- run: env | sort
8588
- name: mkdir
8689
run: mkdir -p ~/.local/bin
90+
- name: npm i
91+
run: npm i
8792
- name: Install ubi
8893
run: curl --silent --location https://raw.githubusercontent.com/houseabsolute/ubi/master/bootstrap/bootstrap-ubi.sh | TARGET=~/.local/bin sh
8994
- name: Install omegasort

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ c.out
22
coverage.out
33
debounce
44
dist/
5+
node_modules/
56
bin/debounce

.golangci.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,10 @@ linters-settings:
107107
line-length: 100
108108
wrapcheck:
109109
ignorePackageGlobs:
110-
- github.com/oalders/is/*
110+
- github.com/oalders/is/*
111111
issues:
112112
exclude-rules:
113113
# disable funlen for test funcs
114-
- source: "^func Test"
114+
- source: '^func Test'
115115
linters:
116116
- funlen

.prettierrc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"printWidth": 80,
3+
"proseWrap": "always",
4+
"overrides": [
5+
{
6+
"files": "*.md",
7+
"options": {
8+
"parser": "markdown",
9+
"printWidth": 80,
10+
"proseWrap": "always"
11+
}
12+
}
13+
]
14+
}

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changes
22

3+
## 0.3.0 - 2024-09-25
4+
5+
- Add --status flag
6+
37
## 0.2.0 - 2024-09-13
48

59
- Obfuscate cache file names

README.md

Lines changed: 169 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,49 @@
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
2739
debounce <integer> <unit> <command>
2840
```
2941

3042
Available units are:
3143

32-
* seconds (s)
33-
* minutes (m)
34-
* hours (h)
44+
- seconds (s)
45+
- minutes (m)
46+
- hours (h)
3547

3648
The following are equivalent:
3749

@@ -53,10 +65,55 @@ debounce 1 hour date
5365
debounce 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
62119
Mon 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
86143
debounce 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
95152
debounce 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.

age/age.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func Compare(path, ageValue, ageUnit string) (bool, error) {
1515
if os.IsNotExist(err) {
1616
return false, nil
1717
}
18-
return false, errors.Join(errors.New("could not stat command"), err)
18+
return false, errors.Join(errors.New("could not stat cache file"), err)
1919
}
2020

2121
dur, err := age.StringToDuration(ageValue, ageUnit)

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/oalders/debounce
33
go 1.20
44

55
require (
6+
github.com/alecthomas/kong v1.2.1
67
github.com/oalders/is v0.5.4-0.20240716215244-33e0acf2ac19
78
github.com/stretchr/testify v1.9.0
89
)

go.sum

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1+
github.com/alecthomas/assert/v2 v2.10.0 h1:jjRCHsj6hBJhkmhznrCzoNpbA3zqy0fYiUcYZP/GkPY=
2+
github.com/alecthomas/kong v1.2.1 h1:E8jH4Tsgv6wCRX2nGrdPyHDUCSG83WH2qE4XLACD33Q=
3+
github.com/alecthomas/kong v1.2.1/go.mod h1:rKTSFhbdp3Ryefn8x5MOEprnRFQ7nlmMC01GKhehhBM=
4+
github.com/alecthomas/repr v0.4.0 h1:GhI2A8MACjfegCPVq9f1FLvIBS+DrQ2KQBFZP1iFzXc=
15
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
26
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
7+
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
38
github.com/oalders/is v0.5.4-0.20240716215244-33e0acf2ac19 h1:9BaO8F7ovJseyo119hfahMm1UDU98/cEQ2LN8m+TCmY=
49
github.com/oalders/is v0.5.4-0.20240716215244-33e0acf2ac19/go.mod h1:11gTLN1BHxrg8PYn+73Iuspq7kOsQVbl4ccOlj8rHj0=
510
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=

0 commit comments

Comments
 (0)