Skip to content

Instantly share code, notes, and snippets.

@aricart
Last active March 3, 2023 12:54
Show Gist options
  • Save aricart/3ad8ed738531bca61de787eb2455034f to your computer and use it in GitHub Desktop.
Save aricart/3ad8ed738531bca61de787eb2455034f to your computer and use it in GitHub Desktop.
osls - tool for listing objectstores
import {
connect,
credsAuthenticator,
} from "https://raw.githubusercontent.com/nats-io/nats.deno/main/src/mod.ts";
import { cli } from "https://deno.land/x/cobra@v0.0.9/mod.ts";
const root = cli({
use:
"ls [--server host:port] [--creds /path/to/creds] [--user n] [--pass pw] --name osname",
short: "list objectstore assets",
run: async (cmd, args, flags): Promise<number> => {
flags.checkRequired();
const name = flags.value<string>("name") || undefined;
const servers = flags.value<string>("server") || undefined;
const user = flags.value<string>("user") || undefined;
const pass = flags.value<string>("pass") || undefined;
const fp = flags.value<string>("creds") || undefined;
let creds;
if (fp) {
const d = await Deno.readFile(fp);
creds = credsAuthenticator(d);
}
const nc = await connect({ servers, user, pass });
console.log(`connected to ${nc.getServer()}`);
const js = nc.jetstream();
if (!name) {
const jsm = await nc.jetstreamManager();
const list = jsm.streams.listObjectStores();
let printed = false;
for await (const i of list) {
printed = true;
console.log(i.bucket);
}
} else {
const os = await js.views.os(name);
const entries = await os.list();
const list = entries.map((v) => {
return {
name: v.name,
size: v.size,
last_modified: v.mtime,
deleted: v.deleted ? "Y" : "N",
};
});
console.table(list);
}
return 0;
},
});
root.addFlag({
name: "creds",
type: "string",
usage: "optional filepath to creds file",
});
root.addFlag({
name: "server",
type: "string",
usage: "optional server hostport",
});
root.addFlag({
name: "user",
type: "string",
usage: "optional user name",
default: undefined,
});
root.addFlag({
name: "pass",
type: "string",
usage: "optional user password",
});
root.addFlag({
name: "name",
type: "string",
usage: "name of the objectstore",
});
Deno.exit(await root.execute(Deno.args));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment