@apet97/clockify-cli-115 6.0.0 → 7.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +167 -21
- package/dist/client.js +30 -7
- package/dist/commands/api.js +44 -76
- package/dist/commands/approvals.js +2 -5
- package/dist/commands/auth.js +229 -0
- package/dist/commands/batch.js +226 -0
- package/dist/commands/clients.js +11 -65
- package/dist/commands/doctor.js +65 -66
- package/dist/commands/expenses.js +21 -24
- package/dist/commands/helpers.js +46 -2
- package/dist/commands/leaf-command.js +7 -0
- package/dist/commands/manifest.js +63 -0
- package/dist/commands/operations.js +271 -0
- package/dist/commands/projects.js +10 -23
- package/dist/commands/reports.js +22 -7
- package/dist/commands/shared-report-body.js +3 -81
- package/dist/commands/sharedReports.js +1 -21
- package/dist/commands/skill.js +85 -0
- package/dist/commands/status.js +18 -12
- package/dist/commands/stop.js +3 -12
- package/dist/commands/tags.js +5 -7
- package/dist/commands/tasks.js +19 -120
- package/dist/commands/users.js +4 -14
- package/dist/commands/webhook-delivery.js +331 -0
- package/dist/commands/webhooks.js +31 -97
- package/dist/completions.js +2 -1
- package/dist/config.js +166 -63
- package/dist/credentials.js +164 -0
- package/dist/failure.js +4 -9
- package/dist/flags.js +14 -4
- package/dist/generated/version.js +1 -1
- package/dist/index.js +38 -14
- package/dist/interrupts.js +28 -0
- package/dist/output.js +137 -48
- package/dist/receipt.js +2 -10
- package/dist/stdin.js +6 -0
- package/dist/stdout.js +38 -0
- package/dist/write-outcome.js +1 -1
- package/package.json +5 -3
- package/plugin/.claude-plugin/plugin.json +16 -0
- package/plugin/skills/clockify-cli/SKILL.md +88 -0
- package/plugin/skills/clockify-mcp/SKILL.md +48 -0
- package/plugin/skills/clockify-sdk/SKILL.md +85 -0
- package/dist/error-codes.js +0 -300
|
@@ -1,4 +1,7 @@
|
|
|
1
|
+
import { webhooksOf } from "clockify-sdk-ts-115/lookup";
|
|
1
2
|
import { requestOptions } from "clockify-sdk-ts-115/request-options";
|
|
3
|
+
import { WEBHOOK_TRIGGER_SOURCE_TYPES, webhookTriggerSource } from "clockify-sdk-ts-115/updates";
|
|
4
|
+
import { CLOCKIFY_WEBHOOK_EVENT_NAMES } from "clockify-sdk-ts-115/webhook-events";
|
|
2
5
|
import { assertSafeWebhookUrl } from "clockify-sdk-ts-115/webhooks";
|
|
3
6
|
import { UsageError } from "../failure.js";
|
|
4
7
|
import { printRecords } from "../output.js";
|
|
@@ -6,98 +9,37 @@ import { printReceipt } from "../receipt.js";
|
|
|
6
9
|
import { dispatchedWrite } from "../write-outcome.js";
|
|
7
10
|
import { resolveContext, splitList } from "./helpers.js";
|
|
8
11
|
import { leafCommand } from "./leaf-command.js";
|
|
12
|
+
import { registerWebhookDeliveryCommands } from "./webhook-delivery.js";
|
|
9
13
|
const WEBHOOK_LIST_TYPES = ["USER_CREATED", "SYSTEM", "ADDON"];
|
|
10
|
-
const WEBHOOK_TRIGGER_SOURCE_TYPES = [
|
|
11
|
-
"PROJECT_ID",
|
|
12
|
-
"USER_ID",
|
|
13
|
-
"TAG_ID",
|
|
14
|
-
"TASK_ID",
|
|
15
|
-
"WORKSPACE_ID",
|
|
16
|
-
"ASSIGNMENT_ID",
|
|
17
|
-
"EXPENSE_ID",
|
|
18
|
-
];
|
|
19
|
-
const WEBHOOK_EVENT_TYPES = [
|
|
20
|
-
"NEW_TIME_ENTRY",
|
|
21
|
-
"TIME_ENTRY_UPDATED",
|
|
22
|
-
"TIME_ENTRY_DELETED",
|
|
23
|
-
"TIME_ENTRY_SPLIT",
|
|
24
|
-
"TIME_ENTRY_RESTORED",
|
|
25
|
-
"NEW_TIMER_STARTED",
|
|
26
|
-
"TIMER_STOPPED",
|
|
27
|
-
"NEW_PROJECT",
|
|
28
|
-
"PROJECT_UPDATED",
|
|
29
|
-
"PROJECT_DELETED",
|
|
30
|
-
"NEW_TASK",
|
|
31
|
-
"TASK_UPDATED",
|
|
32
|
-
"TASK_DELETED",
|
|
33
|
-
"NEW_CLIENT",
|
|
34
|
-
"CLIENT_UPDATED",
|
|
35
|
-
"CLIENT_DELETED",
|
|
36
|
-
"NEW_TAG",
|
|
37
|
-
"TAG_UPDATED",
|
|
38
|
-
"TAG_DELETED",
|
|
39
|
-
"USER_JOINED_WORKSPACE",
|
|
40
|
-
"USER_DELETED_FROM_WORKSPACE",
|
|
41
|
-
"USER_DEACTIVATED_ON_WORKSPACE",
|
|
42
|
-
"USER_ACTIVATED_ON_WORKSPACE",
|
|
43
|
-
"USER_EMAIL_CHANGED",
|
|
44
|
-
"USER_UPDATED",
|
|
45
|
-
"USERS_INVITED_TO_WORKSPACE",
|
|
46
|
-
"LIMITED_USERS_ADDED_TO_WORKSPACE",
|
|
47
|
-
"NEW_INVOICE",
|
|
48
|
-
"INVOICE_UPDATED",
|
|
49
|
-
"NEW_APPROVAL_REQUEST",
|
|
50
|
-
"APPROVAL_REQUEST_STATUS_UPDATED",
|
|
51
|
-
"TIME_OFF_REQUESTED",
|
|
52
|
-
"TIME_OFF_REQUEST_UPDATED",
|
|
53
|
-
"TIME_OFF_REQUEST_APPROVED",
|
|
54
|
-
"TIME_OFF_REQUEST_REJECTED",
|
|
55
|
-
"TIME_OFF_REQUEST_STARTED",
|
|
56
|
-
"TIME_OFF_REQUEST_WITHDRAWN",
|
|
57
|
-
"BALANCE_UPDATED",
|
|
58
|
-
"EXPENSE_CREATED",
|
|
59
|
-
"EXPENSE_UPDATED",
|
|
60
|
-
"EXPENSE_DELETED",
|
|
61
|
-
"EXPENSE_RESTORED",
|
|
62
|
-
"ASSIGNMENT_CREATED",
|
|
63
|
-
"ASSIGNMENT_UPDATED",
|
|
64
|
-
"ASSIGNMENT_DELETED",
|
|
65
|
-
"ASSIGNMENT_PUBLISHED",
|
|
66
|
-
"USER_GROUP_CREATED",
|
|
67
|
-
"USER_GROUP_UPDATED",
|
|
68
|
-
"USER_GROUP_DELETED",
|
|
69
|
-
"COST_RATE_UPDATED",
|
|
70
|
-
"BILLABLE_RATE_UPDATED",
|
|
71
|
-
];
|
|
72
|
-
const USER_ID_TRIGGER_EVENTS = new Set([
|
|
73
|
-
"USER_EMAIL_CHANGED",
|
|
74
|
-
"USER_UPDATED",
|
|
75
|
-
]);
|
|
76
|
-
const webhookEventsExhaustive = true;
|
|
77
|
-
void webhookEventsExhaustive;
|
|
78
14
|
function nonEmptyString(value, field) {
|
|
79
15
|
if (typeof value !== "string" || value.trim() === "") {
|
|
80
16
|
throw new UsageError(`webhooks.create: ${field} must be a non-empty string.`);
|
|
81
17
|
}
|
|
82
18
|
return value;
|
|
83
19
|
}
|
|
20
|
+
function usage(check) {
|
|
21
|
+
try {
|
|
22
|
+
return check();
|
|
23
|
+
}
|
|
24
|
+
catch (error) {
|
|
25
|
+
throw new UsageError(`webhooks.create: ${error instanceof Error ? error.message : String(error)}`);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
84
28
|
function webhookEvent(value) {
|
|
85
29
|
const event = String(value).toUpperCase();
|
|
86
|
-
|
|
30
|
+
const match = CLOCKIFY_WEBHOOK_EVENT_NAMES.find((name) => name === event);
|
|
31
|
+
if (match === undefined) {
|
|
87
32
|
throw new UsageError(`Unknown webhook event: ${String(value)}. Provide a Clockify webhook event type such as NEW_PROJECT or NEW_TIME_ENTRY.`);
|
|
88
33
|
}
|
|
89
|
-
return
|
|
34
|
+
return match;
|
|
90
35
|
}
|
|
91
|
-
function webhookTriggerSourceType(value) {
|
|
92
|
-
const
|
|
93
|
-
|
|
94
|
-
|
|
36
|
+
function webhookTriggerSourceType(value = "WORKSPACE_ID") {
|
|
37
|
+
const type = value.toUpperCase();
|
|
38
|
+
const match = WEBHOOK_TRIGGER_SOURCE_TYPES.find((candidate) => candidate === type);
|
|
39
|
+
if (match === undefined) {
|
|
40
|
+
throw new UsageError(`Unknown trigger source type: ${value}. Provide one of: ${WEBHOOK_TRIGGER_SOURCE_TYPES.join(", ")}.`);
|
|
95
41
|
}
|
|
96
|
-
|
|
97
|
-
if (!WEBHOOK_TRIGGER_SOURCE_TYPES.includes(type)) {
|
|
98
|
-
throw new UsageError(`Unknown trigger source type: ${raw}. Provide one of: PROJECT_ID, USER_ID, TAG_ID, TASK_ID, WORKSPACE_ID, ASSIGNMENT_ID, EXPENSE_ID.`);
|
|
99
|
-
}
|
|
100
|
-
return type;
|
|
42
|
+
return match;
|
|
101
43
|
}
|
|
102
44
|
export const registerWebhooksCommand = (program, services) => {
|
|
103
45
|
const webhooks = program.command("webhooks").description("Manage outbound webhooks.");
|
|
@@ -118,8 +60,7 @@ export const registerWebhooksCommand = (program, services) => {
|
|
|
118
60
|
}
|
|
119
61
|
const { client, workspaceId, output } = await resolveContext(this, services);
|
|
120
62
|
const options = type ? requestOptions({ queryParams: { type } }) : undefined;
|
|
121
|
-
const
|
|
122
|
-
const items = Array.isArray(response) ? response : (response.webhooks ?? []);
|
|
63
|
+
const items = webhooksOf(await client.webhooks.list({ workspaceId }, options));
|
|
123
64
|
const rows = items.map((raw) => {
|
|
124
65
|
const w = raw;
|
|
125
66
|
return {
|
|
@@ -146,26 +87,18 @@ export const registerWebhooksCommand = (program, services) => {
|
|
|
146
87
|
.action(async function (opts) {
|
|
147
88
|
const name = nonEmptyString(opts.name, "name");
|
|
148
89
|
const url = nonEmptyString(opts.url, "url");
|
|
149
|
-
|
|
150
|
-
assertSafeWebhookUrl(url);
|
|
151
|
-
}
|
|
152
|
-
catch (err) {
|
|
153
|
-
throw new UsageError(`webhooks.create: ${err instanceof Error ? err.message : String(err)}`);
|
|
154
|
-
}
|
|
90
|
+
usage(() => assertSafeWebhookUrl(url));
|
|
155
91
|
const event = webhookEvent(opts.event);
|
|
156
92
|
const triggerSourceType = webhookTriggerSourceType(opts.triggerSourceType);
|
|
157
|
-
if (USER_ID_TRIGGER_EVENTS.has(event) && triggerSourceType !== "USER_ID") {
|
|
158
|
-
throw new UsageError(`webhooks.create: ${event} requires USER_ID trigger sources.`);
|
|
159
|
-
}
|
|
160
93
|
const { client, workspaceId, output } = await resolveContext(this, services);
|
|
161
|
-
const triggerSource =
|
|
162
|
-
|
|
163
|
-
:
|
|
94
|
+
const triggerSource = usage(() => webhookTriggerSource({
|
|
95
|
+
workspaceId,
|
|
96
|
+
webhookEvent: event,
|
|
97
|
+
triggerSourceType,
|
|
98
|
+
triggerSource: triggerSourceType !== "WORKSPACE_ID" && opts.triggerSource
|
|
164
99
|
? splitList(String(opts.triggerSource))
|
|
165
|
-
: []
|
|
166
|
-
|
|
167
|
-
throw new UsageError(`webhooks.create: trigger source is required for ${triggerSourceType}.`);
|
|
168
|
-
}
|
|
100
|
+
: [],
|
|
101
|
+
}));
|
|
169
102
|
const body = {
|
|
170
103
|
name,
|
|
171
104
|
url,
|
|
@@ -219,4 +152,5 @@ export const registerWebhooksCommand = (program, services) => {
|
|
|
219
152
|
],
|
|
220
153
|
}, output);
|
|
221
154
|
});
|
|
155
|
+
registerWebhookDeliveryCommands(webhooks, services);
|
|
222
156
|
};
|
package/dist/completions.js
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { InvalidArgumentError } from "commander";
|
|
2
2
|
import { KNOWN_REGIONS } from "./client.js";
|
|
3
3
|
import { TIME_VIEW_MODES } from "./commands/helpers.js";
|
|
4
|
+
import { OUTPUT_MODES } from "./flags.js";
|
|
4
5
|
export const COMPLETION_SHELLS = ["zsh", "bash", "fish"];
|
|
5
6
|
const BINARIES = ["clk115", "clockify115"];
|
|
6
7
|
const FLAG_VALUES = {
|
|
7
|
-
"--output":
|
|
8
|
+
"--output": OUTPUT_MODES,
|
|
8
9
|
"--region": KNOWN_REGIONS,
|
|
9
10
|
"--time-view-mode": TIME_VIEW_MODES,
|
|
10
11
|
};
|
package/dist/config.js
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { existsSync, mkdirSync, readFileSync, realpathSync, renameSync, rmSync, writeFileSync } from "node:fs";
|
|
2
3
|
import { homedir } from "node:os";
|
|
3
|
-
import { join } from "node:path";
|
|
4
|
-
|
|
4
|
+
import { dirname, join } from "node:path";
|
|
5
|
+
import { UsageError } from "./failure.js";
|
|
6
|
+
const ROUTING_KEYS = ["workspaceId", "baseUrl", "region", "subdomain"];
|
|
7
|
+
const KNOWN_RC_FILE_KEYS = ["workspaceId", "baseUrl", "region", "subdomain", "profile", "profiles"];
|
|
8
|
+
const PROFILE_NAME = /^[A-Za-z0-9][A-Za-z0-9_-]{0,63}$/;
|
|
5
9
|
const SECRET_RC_FILE_KEY = /api[-_]?key|token|secret|password|credential/i;
|
|
6
10
|
function editDistance(left, right) {
|
|
7
11
|
let previous = Array.from({ length: right.length + 1 }, (_, index) => index);
|
|
@@ -15,42 +19,113 @@ function editDistance(left, right) {
|
|
|
15
19
|
}
|
|
16
20
|
return previous[right.length];
|
|
17
21
|
}
|
|
18
|
-
function
|
|
19
|
-
const
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
if (
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
}
|
|
22
|
+
function nearestKey(input, known) {
|
|
23
|
+
const distance = (candidate) => editDistance(input.toLowerCase(), candidate.toLowerCase());
|
|
24
|
+
return known.reduce((nearest, candidate) => (distance(candidate) < distance(nearest) ? candidate : nearest));
|
|
25
|
+
}
|
|
26
|
+
function warnUnknownKeys(value, known) {
|
|
27
|
+
for (const key of Object.keys(value)) {
|
|
28
|
+
if (known.includes(key))
|
|
29
|
+
continue;
|
|
30
|
+
process.stderr.write(`WARN clk115: ignoring unknown rc-file key ${JSON.stringify(key)}. Did you mean ${JSON.stringify(nearestKey(key, known))}?\n`);
|
|
28
31
|
}
|
|
29
|
-
|
|
32
|
+
}
|
|
33
|
+
function routed(flags, env, file) {
|
|
34
|
+
const named = firstPresent(flags.baseUrl, env.baseUrl, flags.region, env.region) !== undefined;
|
|
35
|
+
const out = {};
|
|
36
|
+
for (const key of ROUTING_KEYS) {
|
|
37
|
+
const value = firstPresent(flags[key], env[key], named && key !== "workspaceId" ? undefined : file[key]);
|
|
38
|
+
if (value !== undefined)
|
|
39
|
+
out[key] = value;
|
|
40
|
+
}
|
|
41
|
+
return out;
|
|
42
|
+
}
|
|
43
|
+
function routingFlags({ workspace, baseUrl, region, subdomain }) {
|
|
44
|
+
return {
|
|
45
|
+
...(workspace !== undefined && { workspaceId: workspace }),
|
|
46
|
+
...(baseUrl !== undefined && { baseUrl }),
|
|
47
|
+
...(region !== undefined && { region }),
|
|
48
|
+
...(subdomain !== undefined && { subdomain }),
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
function routingEnv(env) {
|
|
52
|
+
return {
|
|
53
|
+
...(env.CLOCKIFY_WORKSPACE_ID !== undefined && { workspaceId: env.CLOCKIFY_WORKSPACE_ID }),
|
|
54
|
+
...(env.CLOCKIFY_BASE_URL !== undefined && { baseUrl: env.CLOCKIFY_BASE_URL }),
|
|
55
|
+
...(env.CLOCKIFY_REGION !== undefined && { region: env.CLOCKIFY_REGION }),
|
|
56
|
+
...(env.CLOCKIFY_SUBDOMAIN !== undefined && { subdomain: env.CLOCKIFY_SUBDOMAIN }),
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
export function selectedProfile(flags, current, env = process.env) {
|
|
60
|
+
return firstPresent(flags.profile, env.CLOCKIFY_PROFILE, current);
|
|
30
61
|
}
|
|
31
62
|
export function loadConfig(flags = {}, env = process.env) {
|
|
32
63
|
const file = loadRcFile(env);
|
|
33
64
|
const apiKey = firstPresent(env.CLOCKIFY_API_KEY);
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
65
|
+
if (flags.profile === undefined && apiKey !== undefined) {
|
|
66
|
+
if (selectedProfile(flags, undefined, env) !== undefined) {
|
|
67
|
+
throw new Error("CLOCKIFY_API_KEY and CLOCKIFY_PROFILE are both set; provide only one of them.");
|
|
68
|
+
}
|
|
69
|
+
return { apiKey, ...routed(routingFlags(flags), routingEnv(env), file) };
|
|
70
|
+
}
|
|
71
|
+
const profile = selectedProfile(flags, file.profile, env);
|
|
72
|
+
if (profile === undefined)
|
|
73
|
+
return routed(routingFlags(flags), routingEnv(env), file);
|
|
74
|
+
const ambient = flags.profile === undefined ? routingEnv(env) : {};
|
|
75
|
+
return { profile, ...routed(routingFlags(flags), ambient, requireProfile(file.profiles, profile)) };
|
|
76
|
+
}
|
|
77
|
+
export function requireProfile(profiles, name) {
|
|
78
|
+
if (Object.hasOwn(profiles, name))
|
|
79
|
+
return profiles[name];
|
|
80
|
+
const known = Object.keys(profiles);
|
|
81
|
+
throw new UsageError(`Unknown profile "${name}". ` +
|
|
82
|
+
(known.length > 0 ? `Provide one of: ${known.join(", ")}.` : "Run `clk115 auth login` to create one."));
|
|
83
|
+
}
|
|
84
|
+
export function loginConfig(profile, flags, profiles) {
|
|
85
|
+
return { profile, ...routed(routingFlags(flags), {}, profiles[profile] ?? {}) };
|
|
86
|
+
}
|
|
87
|
+
export function loadProfiles(env = process.env) {
|
|
88
|
+
const { profile, profiles } = loadRcFile(env);
|
|
89
|
+
return { current: profile, profiles };
|
|
90
|
+
}
|
|
91
|
+
export function requireProfileName(name) {
|
|
92
|
+
if (!PROFILE_NAME.test(name)) {
|
|
93
|
+
throw new UsageError(`"${name}" is not a profile name; provide letters, digits, - or _ (64 at most).`);
|
|
94
|
+
}
|
|
95
|
+
return name;
|
|
96
|
+
}
|
|
97
|
+
export function saveProfiles({ current, profiles }, env = process.env) {
|
|
98
|
+
const path = rcFilePath(env);
|
|
99
|
+
const { profile: _profile, profiles: _profiles, ...rest } = existsSync(path) ? readRcObject(path) : {};
|
|
100
|
+
const next = {
|
|
101
|
+
...rest,
|
|
102
|
+
...(current !== undefined && { profile: current }),
|
|
103
|
+
...(Object.keys(profiles).length > 0 && { profiles }),
|
|
44
104
|
};
|
|
105
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
106
|
+
const target = existsSync(path) ? realpathSync(path) : path;
|
|
107
|
+
const temporary = `${target}.${randomUUID()}.tmp`;
|
|
108
|
+
try {
|
|
109
|
+
writeFileSync(temporary, `${JSON.stringify(next, null, 2)}\n`, { mode: 0o600, flag: "wx" });
|
|
110
|
+
renameSync(temporary, target);
|
|
111
|
+
}
|
|
112
|
+
finally {
|
|
113
|
+
rmSync(temporary, { force: true });
|
|
114
|
+
}
|
|
115
|
+
return path;
|
|
116
|
+
}
|
|
117
|
+
export function saveProfile(name, routing) {
|
|
118
|
+
const { current = name, profiles } = loadProfiles();
|
|
119
|
+
saveProfiles({ current, profiles: { ...profiles, [name]: routing } });
|
|
120
|
+
return { current, previous: Object.hasOwn(profiles, name) ? profiles[name] : undefined };
|
|
45
121
|
}
|
|
46
122
|
function firstPresent(...values) {
|
|
47
123
|
return values.find((value) => typeof value === "string" && value.trim().length > 0);
|
|
48
124
|
}
|
|
49
|
-
export function
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
return config.apiKey;
|
|
125
|
+
export function missingApiKey({ profile }) {
|
|
126
|
+
return new Error(profile === undefined
|
|
127
|
+
? "Clockify API key not set. Set CLOCKIFY_API_KEY in the process environment, or run `clk115 auth login`."
|
|
128
|
+
: `Clockify API key not set: profile "${profile}" has no stored key for this host. Run \`clk115 auth login --profile ${profile}\`.`);
|
|
54
129
|
}
|
|
55
130
|
export function requireWorkspaceId(config) {
|
|
56
131
|
if (!config.workspaceId) {
|
|
@@ -58,41 +133,69 @@ export function requireWorkspaceId(config) {
|
|
|
58
133
|
}
|
|
59
134
|
return config.workspaceId;
|
|
60
135
|
}
|
|
61
|
-
function
|
|
136
|
+
function rcFilePath(env) {
|
|
62
137
|
const root = firstPresent(env.CLOCKIFY_HOME) ?? homedir();
|
|
63
138
|
const candidates = [join(root, "clockifyrc.json"), join(root, ".clockifyrc.json")];
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
throw new Error(`failed to read Clockify rc file ${path}: ${reason}`);
|
|
75
|
-
}
|
|
76
|
-
if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed)) {
|
|
77
|
-
throw new Error(`Clockify rc file ${path} must contain a JSON object such as {"workspaceId":"..."}; fix or remove the file`);
|
|
78
|
-
}
|
|
79
|
-
const secretKeys = Object.keys(parsed).filter((key) => SECRET_RC_FILE_KEY.test(key));
|
|
80
|
-
if (secretKeys.length > 0) {
|
|
81
|
-
throw new Error(`legacy rc-file secret detected: remove ${secretKeys.join(", ")} from the rc file and set CLOCKIFY_API_KEY in the process environment`);
|
|
82
|
-
}
|
|
83
|
-
for (const key of Object.keys(parsed)) {
|
|
84
|
-
if (KNOWN_RC_FILE_KEYS.includes(key))
|
|
85
|
-
continue;
|
|
86
|
-
const suggestion = nearestRcFileKey(key);
|
|
87
|
-
process.stderr.write(`WARN clk115: ignoring unknown rc-file key ${JSON.stringify(key)}. Did you mean ${JSON.stringify(suggestion)}?\n`);
|
|
88
|
-
}
|
|
89
|
-
const out = {};
|
|
90
|
-
for (const key of KNOWN_RC_FILE_KEYS) {
|
|
91
|
-
const value = parsed[key];
|
|
92
|
-
if (typeof value === "string")
|
|
93
|
-
out[key] = value;
|
|
94
|
-
}
|
|
95
|
-
return out;
|
|
139
|
+
return candidates.find((path) => existsSync(path)) ?? candidates[1];
|
|
140
|
+
}
|
|
141
|
+
function readRcObject(path) {
|
|
142
|
+
let parsed;
|
|
143
|
+
try {
|
|
144
|
+
parsed = JSON.parse(readFileSync(path, "utf8"));
|
|
145
|
+
}
|
|
146
|
+
catch (err) {
|
|
147
|
+
const reason = err instanceof Error ? err.message : String(err);
|
|
148
|
+
throw new Error(`failed to read Clockify rc file ${path}: ${reason}`);
|
|
96
149
|
}
|
|
97
|
-
|
|
150
|
+
if (!isObject(parsed)) {
|
|
151
|
+
throw new Error(`Clockify rc file ${path} must contain a JSON object such as {"workspaceId":"..."}; fix or remove the file`);
|
|
152
|
+
}
|
|
153
|
+
return parsed;
|
|
154
|
+
}
|
|
155
|
+
function isObject(value) {
|
|
156
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
157
|
+
}
|
|
158
|
+
function refuseSecrets(value) {
|
|
159
|
+
const secretKeys = Object.keys(value).filter((key) => SECRET_RC_FILE_KEY.test(key));
|
|
160
|
+
if (secretKeys.length > 0) {
|
|
161
|
+
throw new Error(`legacy rc-file secret detected: remove ${secretKeys.join(", ")} from the rc file and set CLOCKIFY_API_KEY in the process environment`);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
function readRouting(value) {
|
|
165
|
+
const out = {};
|
|
166
|
+
for (const key of ROUTING_KEYS) {
|
|
167
|
+
const text = value[key];
|
|
168
|
+
if (typeof text === "string")
|
|
169
|
+
out[key] = text;
|
|
170
|
+
}
|
|
171
|
+
return out;
|
|
172
|
+
}
|
|
173
|
+
function readProfiles(value) {
|
|
174
|
+
if (value === undefined)
|
|
175
|
+
return {};
|
|
176
|
+
if (!isObject(value))
|
|
177
|
+
throw new Error("rc file: `profiles` must be an object of named profiles");
|
|
178
|
+
const profiles = {};
|
|
179
|
+
for (const [name, profile] of Object.entries(value)) {
|
|
180
|
+
requireProfileName(name);
|
|
181
|
+
if (!isObject(profile))
|
|
182
|
+
throw new Error(`rc file: profile "${name}" must be an object`);
|
|
183
|
+
refuseSecrets(profile);
|
|
184
|
+
warnUnknownKeys(profile, ROUTING_KEYS);
|
|
185
|
+
profiles[name] = readRouting(profile);
|
|
186
|
+
}
|
|
187
|
+
return profiles;
|
|
188
|
+
}
|
|
189
|
+
function loadRcFile(env) {
|
|
190
|
+
const path = rcFilePath(env);
|
|
191
|
+
if (!existsSync(path))
|
|
192
|
+
return { profiles: {} };
|
|
193
|
+
const parsed = readRcObject(path);
|
|
194
|
+
refuseSecrets(parsed);
|
|
195
|
+
warnUnknownKeys(parsed, KNOWN_RC_FILE_KEYS);
|
|
196
|
+
return {
|
|
197
|
+
...readRouting(parsed),
|
|
198
|
+
...(typeof parsed.profile === "string" && parsed.profile !== "" && { profile: parsed.profile }),
|
|
199
|
+
profiles: readProfiles(parsed.profiles),
|
|
200
|
+
};
|
|
98
201
|
}
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import { createInterface } from "node:readline";
|
|
3
|
+
import { Writable } from "node:stream";
|
|
4
|
+
import { UsageError } from "./failure.js";
|
|
5
|
+
import { signalExitCode } from "./interrupts.js";
|
|
6
|
+
import { AlreadyReportedError } from "./output.js";
|
|
7
|
+
import { readStdin } from "./stdin.js";
|
|
8
|
+
const SERVICE = "clockify115-cli";
|
|
9
|
+
const ITEM_NOT_FOUND = 44;
|
|
10
|
+
const PROMPT_TIMEOUT_MS = 120_000;
|
|
11
|
+
const NOTHING_READ = "No API key was read; provide it on stdin or at the prompt.";
|
|
12
|
+
const ACCOUNT = /^[A-Za-z0-9_-]+@(?:https?:\/\/)?[A-Za-z0-9.:[\]-]+$/;
|
|
13
|
+
export function credentialHost({ baseUrl, region, subdomain }) {
|
|
14
|
+
if (baseUrl !== undefined)
|
|
15
|
+
return URL.canParse(baseUrl) ? new URL(baseUrl).origin : "invalid-base-url";
|
|
16
|
+
return subdomain === undefined ? (region ?? "global") : `${subdomain}.${region ?? "global"}`;
|
|
17
|
+
}
|
|
18
|
+
export function credentialAccount(config) {
|
|
19
|
+
const account = `${config.profile ?? ""}@${credentialHost(config)}`;
|
|
20
|
+
return ACCOUNT.test(account) ? account : undefined;
|
|
21
|
+
}
|
|
22
|
+
async function run(command, args, input = "") {
|
|
23
|
+
const { CLOCKIFY_API_KEY: _key, CLOCKIFY_ADDON_TOKEN: _token, ...env } = process.env;
|
|
24
|
+
const child = spawn(command, args, { env, stdio: ["pipe", "pipe", "pipe"] });
|
|
25
|
+
const timer = setTimeout(() => child.kill(), PROMPT_TIMEOUT_MS);
|
|
26
|
+
let stdout = "";
|
|
27
|
+
let stderr = "";
|
|
28
|
+
child.stdout.setEncoding("utf8").on("data", (text) => (stdout += text));
|
|
29
|
+
child.stderr.setEncoding("utf8").on("data", (text) => (stderr += text));
|
|
30
|
+
child.stdin.on("error", () => undefined);
|
|
31
|
+
child.stdin.end(input);
|
|
32
|
+
try {
|
|
33
|
+
return await new Promise((resolve, reject) => {
|
|
34
|
+
child.on("error", (err) => {
|
|
35
|
+
reject(new Error(`Could not run ${command}: ${err.message}. Set CLOCKIFY_API_KEY instead.`));
|
|
36
|
+
});
|
|
37
|
+
child.on("close", (code) => {
|
|
38
|
+
resolve({ code, stdout, stderr });
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
finally {
|
|
43
|
+
clearTimeout(timer);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
function failed(store, action, { code, stderr }) {
|
|
47
|
+
const reason = stderr.trim().slice(0, 300) || `exit ${code ?? "by signal"}`;
|
|
48
|
+
return new Error(`Could not ${action} the API key in ${store}: ${reason}`);
|
|
49
|
+
}
|
|
50
|
+
export function macosKeychain(command = "/usr/bin/security", keychain = []) {
|
|
51
|
+
const name = "the macOS keychain";
|
|
52
|
+
const item = (account) => ["-s", SERVICE, "-a", account];
|
|
53
|
+
return {
|
|
54
|
+
name,
|
|
55
|
+
async get(account) {
|
|
56
|
+
const result = await run(command, ["find-generic-password", ...item(account), "-w", ...keychain]);
|
|
57
|
+
if (result.code === ITEM_NOT_FOUND)
|
|
58
|
+
return undefined;
|
|
59
|
+
if (result.code !== 0)
|
|
60
|
+
throw failed(name, "read", result);
|
|
61
|
+
return result.stdout.replace(/\n$/, "");
|
|
62
|
+
},
|
|
63
|
+
async set(account, secret) {
|
|
64
|
+
const words = ["add-generic-password", "-U", ...item(account), "-w", secret, ...keychain];
|
|
65
|
+
const result = await run(command, ["-i"], `${words.map((word) => `"${word}"`).join(" ")}\n`);
|
|
66
|
+
if (result.code !== 0)
|
|
67
|
+
throw failed(name, "store", result);
|
|
68
|
+
},
|
|
69
|
+
async delete(account) {
|
|
70
|
+
const result = await run(command, ["delete-generic-password", ...item(account), ...keychain]);
|
|
71
|
+
if (result.code === ITEM_NOT_FOUND)
|
|
72
|
+
return false;
|
|
73
|
+
if (result.code !== 0)
|
|
74
|
+
throw failed(name, "delete", result);
|
|
75
|
+
return true;
|
|
76
|
+
},
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
export function secretService(command = "secret-tool") {
|
|
80
|
+
const name = "the Secret Service keyring";
|
|
81
|
+
const item = (account) => ["service", SERVICE, "account", account];
|
|
82
|
+
const get = async (account) => {
|
|
83
|
+
const result = await run(command, ["lookup", ...item(account)]);
|
|
84
|
+
if (result.code === 0 && result.stdout !== "")
|
|
85
|
+
return result.stdout;
|
|
86
|
+
if (result.code === 1 && result.stderr.trim() === "")
|
|
87
|
+
return undefined;
|
|
88
|
+
throw failed(name, "read", result);
|
|
89
|
+
};
|
|
90
|
+
return {
|
|
91
|
+
name,
|
|
92
|
+
get,
|
|
93
|
+
async set(account, secret) {
|
|
94
|
+
const result = await run(command, ["store", "--label", `clk115 ${account}`, ...item(account)], secret);
|
|
95
|
+
if (result.code !== 0)
|
|
96
|
+
throw failed(name, "store", result);
|
|
97
|
+
},
|
|
98
|
+
async delete(account) {
|
|
99
|
+
if ((await get(account)) === undefined)
|
|
100
|
+
return false;
|
|
101
|
+
const result = await run(command, ["clear", ...item(account)]);
|
|
102
|
+
if (result.code !== 0)
|
|
103
|
+
throw failed(name, "delete", result);
|
|
104
|
+
return true;
|
|
105
|
+
},
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
export function systemCredentialStore(platform = process.platform) {
|
|
109
|
+
if (platform === "darwin")
|
|
110
|
+
return macosKeychain();
|
|
111
|
+
if (platform === "linux")
|
|
112
|
+
return secretService();
|
|
113
|
+
throw new UsageError(`clk115 has no credential store on ${platform}; set CLOCKIFY_API_KEY in the environment.`);
|
|
114
|
+
}
|
|
115
|
+
export function requireStorableKey(text) {
|
|
116
|
+
const key = text.trim();
|
|
117
|
+
if (key === "")
|
|
118
|
+
throw new UsageError(NOTHING_READ);
|
|
119
|
+
if (!/^[A-Za-z0-9+/=_.-]{8,512}$/.test(key)) {
|
|
120
|
+
throw new UsageError("That does not look like a Clockify API key; provide the key exactly as Clockify shows it.");
|
|
121
|
+
}
|
|
122
|
+
return key;
|
|
123
|
+
}
|
|
124
|
+
export async function storeVerified(store, account, key) {
|
|
125
|
+
const previous = await store.get(account);
|
|
126
|
+
const undo = async () => {
|
|
127
|
+
await (previous === undefined ? store.delete(account) : store.set(account, previous)).catch(() => undefined);
|
|
128
|
+
};
|
|
129
|
+
await store.set(account, key);
|
|
130
|
+
if ((await store.get(account)) === key)
|
|
131
|
+
return undo;
|
|
132
|
+
await undo();
|
|
133
|
+
throw new Error(`Could not read the API key back from ${store.name}, so it was not kept. Set CLOCKIFY_API_KEY instead.`);
|
|
134
|
+
}
|
|
135
|
+
export async function storedApiKey(config, store) {
|
|
136
|
+
const account = credentialAccount(config);
|
|
137
|
+
return account === undefined ? undefined : await store().get(account);
|
|
138
|
+
}
|
|
139
|
+
export async function readSecret() {
|
|
140
|
+
if (!process.stdin.isTTY)
|
|
141
|
+
return await readStdin();
|
|
142
|
+
const silent = new Writable({
|
|
143
|
+
write(_chunk, _encoding, done) {
|
|
144
|
+
done();
|
|
145
|
+
},
|
|
146
|
+
});
|
|
147
|
+
const reader = createInterface({ input: process.stdin, output: silent, terminal: true });
|
|
148
|
+
process.stderr.write("Clockify API key (hidden): ");
|
|
149
|
+
try {
|
|
150
|
+
return await new Promise((resolve, reject) => {
|
|
151
|
+
reader.once("line", resolve);
|
|
152
|
+
reader.once("SIGINT", () => {
|
|
153
|
+
reject(new AlreadyReportedError(signalExitCode("SIGINT")));
|
|
154
|
+
});
|
|
155
|
+
reader.once("close", () => {
|
|
156
|
+
reject(new UsageError(NOTHING_READ));
|
|
157
|
+
});
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
finally {
|
|
161
|
+
reader.close();
|
|
162
|
+
process.stderr.write("\n");
|
|
163
|
+
}
|
|
164
|
+
}
|
package/dist/failure.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { recoveryForCode } from "
|
|
2
|
-
import { AlreadyReportedError, printError, } from "./output.js";
|
|
1
|
+
import { recoveryForCode } from "clockify-sdk-ts-115/error-codes";
|
|
2
|
+
import { AlreadyReportedError, printError, UsageError, } from "./output.js";
|
|
3
3
|
import { failureStatus, writeFailure } from "./write-outcome.js";
|
|
4
|
+
export { UsageError } from "./output.js";
|
|
4
5
|
export class LookupError extends Error {
|
|
5
6
|
lookupState;
|
|
6
7
|
constructor(lookupState, message) {
|
|
@@ -8,12 +9,6 @@ export class LookupError extends Error {
|
|
|
8
9
|
this.lookupState = lookupState;
|
|
9
10
|
}
|
|
10
11
|
}
|
|
11
|
-
export class UsageError extends Error {
|
|
12
|
-
constructor(message) {
|
|
13
|
-
super(message);
|
|
14
|
-
this.name = "UsageError";
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
12
|
export class PartialDeleteError extends Error {
|
|
18
13
|
completedStage;
|
|
19
14
|
next;
|
|
@@ -63,7 +58,7 @@ export async function reportFailure(err, output, failedCommand) {
|
|
|
63
58
|
return 2;
|
|
64
59
|
}
|
|
65
60
|
if (err instanceof AlreadyReportedError)
|
|
66
|
-
return
|
|
61
|
+
return err.exitCode;
|
|
67
62
|
if (err instanceof UsageError && (!write || write.stage === "before_dispatch")) {
|
|
68
63
|
printError(err.message, output, undefined, usage(failedCommand), undefined, write);
|
|
69
64
|
return 2;
|