From 98806725a3ed7563db6a1ea7ce75b2adaed1aa48 Mon Sep 17 00:00:00 2001 From: William Perron Date: Mon, 21 Oct 2024 09:52:56 -0400 Subject: [PATCH] initial commit --- LICENSE | 22 ++++++++++++++++++++++ README.md | 17 +++++++++++++++++ incr | 25 +++++++++++++++++++++++++ 3 files changed, 64 insertions(+) create mode 100644 LICENSE create mode 100644 README.md create mode 100755 incr diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..ea739fd --- /dev/null +++ b/LICENSE @@ -0,0 +1,22 @@ +The MIT License (MIT) + +Copyright (c) 2024 William Perron + + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..20be5f9 --- /dev/null +++ b/README.md @@ -0,0 +1,17 @@ +# Global Increment + +This program creates a persisted sequence that increments every time it is +called, useful when you need to call the same command over and over and need to +pass an ever-increasing number to it. Takes an optional positional argument that +acts as a key, allowing multiple sequences to be used. + +## Usage + +```bash +incr [KEY] +``` + +## Installation + +Copy the `incr` file somewhere in your `PATH`. Needs [Deno][https://deno.com] +installed. diff --git a/incr b/incr new file mode 100755 index 0000000..574a8c3 --- /dev/null +++ b/incr @@ -0,0 +1,25 @@ +#!/usr/bin/env deno run --unstable-kv --allow-read --allow-write +// deno-lint-ignore-file prefer-const +import { parseArgs } from "jsr:@std/cli"; + +let db = await Deno.openKv("incrementer"); + +if (import.meta.main) { + let args = parseArgs(Deno.args); + let ns = args["_"][0] ?? "_global"; + + let old = await db.get([ns]); + + let nextVal = old.value + 1; + let n = await db.atomic() + .check(old) + .set([ns], nextVal) + .commit(); + + if (n.ok === false) { + console.error("failed to commit result to Deno KV"); + Deno.exit(1); + } + + console.log(nextVal); +}