Skip to content

Commit b947107

Browse files
authored
Merge pull request #55 from geolonia/feature/issue-661-count-only
feat(cli): add --count-only option for entities list
2 parents b7d8a4b + 7ea5a4a commit b947107

2 files changed

Lines changed: 34 additions & 4 deletions

File tree

src/commands/entities.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
outputResponse,
77
} from "../helpers.js";
88
import { parseJsonInput } from "../input.js";
9-
import { printSuccess } from "../output.js";
9+
import { printCount, printSuccess } from "../output.js";
1010
import { registerAttrsSubcommand } from "./attrs.js";
1111
import { addExamples } from "./help.js";
1212

@@ -31,6 +31,7 @@ export function registerEntitiesCommand(program: Command): void {
3131
.option("--offset <n>", "Skip first N entities", parseInt)
3232
.option("--order-by <field>", "Order results by field")
3333
.option("--count", "Include total count in response")
34+
.option("--count-only", "Only show the total count without listing entities")
3435
.option("--key-values", "Request simplified key-value format")
3536
.action(
3637
withErrorHandler(async (opts: Record<string, unknown>, cmd: Command) => {
@@ -50,11 +51,16 @@ export function registerEntitiesCommand(program: Command): void {
5051
if (opts.limit !== undefined) params.limit = String(opts.limit);
5152
if (opts.offset !== undefined) params.offset = String(opts.offset);
5253
if (opts.orderBy) params.orderBy = String(opts.orderBy);
53-
if (opts.count) params.count = "true";
54+
if (opts.count || opts.countOnly) params.count = "true";
55+
if (opts.countOnly) params.limit = "0";
5456
if (opts.keyValues) params.options = "keyValues";
5557

5658
const response = await client.get("/entities", params);
57-
outputResponse(response, format, !!opts.count);
59+
if (opts.countOnly) {
60+
printCount(response.count ?? 0);
61+
} else {
62+
outputResponse(response, format, !!opts.count);
63+
}
5864
}),
5965
);
6066

@@ -117,6 +123,10 @@ export function registerEntitiesCommand(program: Command): void {
117123
description: "Get total count with results",
118124
command: "geonic entities list --type Sensor --count",
119125
},
126+
{
127+
description: "Get only the total count (no entity data)",
128+
command: "geonic entities list --type Sensor --count-only",
129+
},
120130
]);
121131

122132
// entities get

tests/entities.test.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ vi.mock("../src/commands/attrs.js", () => ({
3232

3333
import { createClient, getFormat, outputResponse } from "../src/helpers.js";
3434
import { parseJsonInput } from "../src/input.js";
35-
import { printSuccess } from "../src/output.js";
35+
import { printCount, printSuccess } from "../src/output.js";
3636
import { registerEntitiesCommand } from "../src/commands/entities.js";
3737

3838
describe("entities command", () => {
@@ -133,6 +133,26 @@ describe("entities command", () => {
133133
expect(outputResponse).toHaveBeenCalledWith(expect.anything(), "json", true);
134134
});
135135

136+
it("passes count-only option with limit=0 and only shows count", async () => {
137+
mockClient.get.mockResolvedValue(mockResponse([], 200, 42));
138+
await runCommand(program, ["entities", "list", "--count-only"]);
139+
140+
expect(mockClient.get).toHaveBeenCalledWith("/entities", expect.objectContaining({
141+
count: "true",
142+
limit: "0",
143+
}));
144+
expect(printCount).toHaveBeenCalledWith(42);
145+
expect(outputResponse).not.toHaveBeenCalled();
146+
});
147+
148+
it("count-only defaults to 0 when response has no count", async () => {
149+
mockClient.get.mockResolvedValue(mockResponse([]));
150+
await runCommand(program, ["entities", "list", "--count-only"]);
151+
152+
expect(printCount).toHaveBeenCalledWith(0);
153+
expect(outputResponse).not.toHaveBeenCalled();
154+
});
155+
136156
it("passes keyValues option as options=keyValues", async () => {
137157
mockClient.get.mockResolvedValue(mockResponse([]));
138158
await runCommand(program, ["entities", "list", "--key-values"]);

0 commit comments

Comments
 (0)