@ably/cli
Version:
Ably CLI for Pub/Sub, Chat and Spaces
96 lines (95 loc) • 4.08 kB
JavaScript
import { Args } from "@oclif/core";
import chalk from "chalk";
import { SpacesBaseCommand } from "../../../spaces-base-command.js";
// interface SpacesClients { // Remove interface
// realtimeClient: any;
// spacesClient: any;
// }
export default class SpacesLocksGet extends SpacesBaseCommand {
static args = {
space: Args.string({
description: "Space to get lock from",
required: true,
}),
lockId: Args.string({
description: "Lock ID to get",
required: true,
}),
};
static description = "Get a lock in a space";
static examples = [
"$ ably spaces locks get my-space my-lock",
"$ ably spaces locks get my-space my-lock --json",
"$ ably spaces locks get my-space my-lock --pretty-json",
];
static flags = {
...SpacesBaseCommand.globalFlags,
};
// Declare class properties
realtimeClient = null;
spacesClient = null;
space = null;
async run() {
const { args, flags } = await this.parse(SpacesLocksGet);
// let clients: SpacesClients | null = null // Remove local variable
const { space: spaceName } = args; // Get spaceName earlier
const { lockId } = args;
try {
// Create Spaces client using setupSpacesClient
// clients = await this.createSpacesClient(flags) // Replace with setupSpacesClient
const setupResult = await this.setupSpacesClient(flags, spaceName);
this.realtimeClient = setupResult.realtimeClient;
this.spacesClient = setupResult.spacesClient;
this.space = setupResult.space;
// if (!clients) return // Check properties
if (!this.realtimeClient || !this.spacesClient || !this.space) {
this.error("Failed to initialize clients or space");
return;
}
// const { spacesClient } = clients // Remove deconstruction
// const {spaceName} = args // Moved earlier
// const {lockId} = args // Moved earlier
// Get the space
// const space = await spacesClient.get(spaceName) // Already got this.space
// Enter the space first
// await space.enter() // Use this.space
await this.space.enter();
this.log(`${chalk.green("Successfully entered space:")} ${chalk.cyan(spaceName)}`);
// Try to get the lock
try {
// const lock = await space.locks.get(lockId) // Use this.space
const lock = await this.space.locks.get(lockId);
if (!lock) {
if (this.shouldOutputJson(flags)) {
this.log(this.formatJsonOutput({ error: "Lock not found", lockId }, flags));
}
else {
this.log(chalk.yellow(`Lock '${lockId}' not found in space '${spaceName}'`));
}
return;
}
if (this.shouldOutputJson(flags)) {
// Use structuredClone or similar for formatJsonOutput
this.log(this.formatJsonOutput(structuredClone(lock), flags));
}
else {
// Use structuredClone or similar for formatJsonOutput
this.log(`${chalk.dim("Lock details:")} ${this.formatJsonOutput(structuredClone(lock), flags)}`);
}
}
catch (error) {
this.error(`Failed to get lock: ${error instanceof Error ? error.message : String(error)}`);
}
}
catch (error) {
this.error(`Error: ${error instanceof Error ? error.message : String(error)}`);
}
finally {
// if (clients?.realtimeClient) { // Use this.realtimeClient
if (this.realtimeClient) {
// clients.realtimeClient.close() // Use this.realtimeClient
this.realtimeClient.close();
}
}
}
}