@lightdash-tools/cli 0.1.1 → 0.2.1

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.
Files changed (48) hide show
  1. package/README.md +49 -1
  2. package/dist/commands/ai-agents.js +8 -6
  3. package/dist/commands/charts.js +8 -6
  4. package/dist/commands/content.d.ts +8 -0
  5. package/dist/commands/content.js +48 -0
  6. package/dist/commands/dashboards.js +4 -2
  7. package/dist/commands/explores.js +34 -4
  8. package/dist/commands/groups.js +6 -4
  9. package/dist/commands/metrics.d.ts +8 -0
  10. package/dist/commands/metrics.js +59 -0
  11. package/dist/commands/organization-roles.js +10 -8
  12. package/dist/commands/organization.js +4 -2
  13. package/dist/commands/project-access.js +8 -6
  14. package/dist/commands/project-role-assignments.js +12 -10
  15. package/dist/commands/projects.js +38 -4
  16. package/dist/commands/query.js +4 -2
  17. package/dist/commands/schedulers.d.ts +8 -0
  18. package/dist/commands/schedulers.js +59 -0
  19. package/dist/commands/spaces.js +6 -4
  20. package/dist/commands/tags.d.ts +8 -0
  21. package/dist/commands/tags.js +52 -0
  22. package/dist/commands/users.js +6 -4
  23. package/dist/index.js +13 -1
  24. package/dist/utils/safety.d.ts +11 -0
  25. package/dist/utils/safety.js +39 -0
  26. package/dist/utils/safety.test.d.ts +1 -0
  27. package/dist/utils/safety.test.js +50 -0
  28. package/package.json +3 -3
  29. package/src/commands/ai-agents.ts +53 -45
  30. package/src/commands/charts.ts +59 -47
  31. package/src/commands/content.ts +55 -0
  32. package/src/commands/dashboards.ts +17 -13
  33. package/src/commands/explores.ts +77 -26
  34. package/src/commands/groups.ts +59 -53
  35. package/src/commands/metrics.ts +69 -0
  36. package/src/commands/organization-roles.ts +78 -68
  37. package/src/commands/organization.ts +17 -13
  38. package/src/commands/project-access.ts +50 -39
  39. package/src/commands/project-role-assignments.ts +87 -75
  40. package/src/commands/projects.ts +77 -28
  41. package/src/commands/query.ts +30 -23
  42. package/src/commands/schedulers.ts +66 -0
  43. package/src/commands/spaces.ts +32 -26
  44. package/src/commands/tags.ts +53 -0
  45. package/src/commands/users.ts +51 -45
  46. package/src/index.ts +17 -1
  47. package/src/utils/safety.test.ts +53 -0
  48. package/src/utils/safety.ts +33 -0
@@ -3,7 +3,9 @@
3
3
  */
4
4
 
5
5
  import type { Command } from 'commander';
6
+ import { READ_ONLY_DEFAULT, WRITE_IDEMPOTENT } from '@lightdash-tools/common';
6
7
  import { getClient } from '../utils/client';
8
+ import { wrapAction } from '../utils/safety';
7
9
 
8
10
  /**
9
11
  * Registers the projects command and its subcommands.
@@ -15,38 +17,85 @@ export function registerProjectsCommand(program: Command): void {
15
17
  .command('get')
16
18
  .description('Get a project by UUID')
17
19
  .argument('<projectUuid>', 'Project UUID')
18
- .action(async (projectUuid: string) => {
19
- try {
20
- if (!projectUuid) {
21
- console.error('Error: projectUuid is required');
20
+ .action(
21
+ wrapAction(READ_ONLY_DEFAULT, async (projectUuid: string) => {
22
+ try {
23
+ if (!projectUuid) {
24
+ console.error('Error: projectUuid is required');
25
+ process.exit(1);
26
+ }
27
+ const client = getClient();
28
+ const project = await client.v1.projects.getProject(projectUuid);
29
+ console.log(JSON.stringify(project, null, 2));
30
+ } catch (error) {
31
+ console.error(
32
+ 'Error fetching project:',
33
+ error instanceof Error ? error.message : String(error),
34
+ );
22
35
  process.exit(1);
23
36
  }
24
- const client = getClient();
25
- const project = await client.v1.projects.getProject(projectUuid);
26
- console.log(JSON.stringify(project, null, 2));
27
- } catch (error) {
28
- console.error(
29
- 'Error fetching project:',
30
- error instanceof Error ? error.message : String(error),
31
- );
32
- process.exit(1);
33
- }
34
- });
37
+ }),
38
+ );
35
39
 
36
40
  projectsCmd
37
41
  .command('list')
38
42
  .description('List all projects in the current organization')
39
- .action(async () => {
40
- try {
41
- const client = getClient();
42
- const projects = await client.v1.projects.listProjects();
43
- console.log(JSON.stringify(projects, null, 2));
44
- } catch (error) {
45
- console.error(
46
- 'Error listing projects:',
47
- error instanceof Error ? error.message : String(error),
48
- );
49
- process.exit(1);
50
- }
51
- });
43
+ .action(
44
+ wrapAction(READ_ONLY_DEFAULT, async () => {
45
+ try {
46
+ const client = getClient();
47
+ const projects = await client.v1.projects.listProjects();
48
+ console.log(JSON.stringify(projects, null, 2));
49
+ } catch (error) {
50
+ console.error(
51
+ 'Error listing projects:',
52
+ error instanceof Error ? error.message : String(error),
53
+ );
54
+ process.exit(1);
55
+ }
56
+ }),
57
+ );
58
+
59
+ const validateCmd = projectsCmd.command('validate').description('Project validation');
60
+
61
+ validateCmd
62
+ .command('run <projectUuid>')
63
+ .description('Trigger project validation and get job ID')
64
+ .action(
65
+ wrapAction(WRITE_IDEMPOTENT, async (projectUuid: string) => {
66
+ try {
67
+ const client = getClient();
68
+ const result = await client.v1.validation.validateProject(projectUuid);
69
+ console.log(JSON.stringify(result, null, 2));
70
+ } catch (error) {
71
+ console.error(
72
+ 'Error triggering validation:',
73
+ error instanceof Error ? error.message : String(error),
74
+ );
75
+ process.exit(1);
76
+ }
77
+ }),
78
+ );
79
+
80
+ validateCmd
81
+ .command('results <projectUuid>')
82
+ .description('Get latest validation results for a project')
83
+ .option('--job-id <id>', 'Specific validation job ID')
84
+ .action(
85
+ wrapAction(READ_ONLY_DEFAULT, async (projectUuid: string, options: { jobId?: string }) => {
86
+ try {
87
+ const client = getClient();
88
+ const result = await client.v1.validation.getValidationResults(projectUuid, {
89
+ jobId: options.jobId,
90
+ });
91
+ console.log(JSON.stringify(result, null, 2));
92
+ } catch (error) {
93
+ console.error(
94
+ 'Error fetching validation results:',
95
+ error instanceof Error ? error.message : String(error),
96
+ );
97
+ process.exit(1);
98
+ }
99
+ }),
100
+ );
52
101
  }
@@ -4,7 +4,9 @@
4
4
 
5
5
  import type { Command } from 'commander';
6
6
  import { readFileSync } from 'fs';
7
+ import { READ_ONLY_DEFAULT } from '@lightdash-tools/common';
7
8
  import { getClient } from '../utils/client';
9
+ import { wrapAction } from '../utils/safety';
8
10
  import type { CompileQueryRequest } from '@lightdash-tools/common';
9
11
 
10
12
  /**
@@ -55,29 +57,34 @@ export function registerQueryCommand(program: Command): void {
55
57
  .argument('<projectUuid>', 'Project UUID')
56
58
  .argument('<exploreId>', 'Explore ID')
57
59
  .option('--file <path>', 'Read metric query JSON from file (default: read from stdin)')
58
- .action(async (projectUuid: string, exploreId: string, options: { file?: string }) => {
59
- try {
60
- if (!projectUuid) {
61
- console.error('Error: projectUuid is required');
62
- process.exit(1);
63
- }
64
- if (!exploreId) {
65
- console.error('Error: exploreId is required');
66
- process.exit(1);
67
- }
60
+ .action(
61
+ wrapAction(
62
+ READ_ONLY_DEFAULT,
63
+ async (projectUuid: string, exploreId: string, options: { file?: string }) => {
64
+ try {
65
+ if (!projectUuid) {
66
+ console.error('Error: projectUuid is required');
67
+ process.exit(1);
68
+ }
69
+ if (!exploreId) {
70
+ console.error('Error: exploreId is required');
71
+ process.exit(1);
72
+ }
68
73
 
69
- // Read JSON input
70
- const body = (await readJsonInput(options.file)) as CompileQueryRequest;
74
+ // Read JSON input
75
+ const body = (await readJsonInput(options.file)) as CompileQueryRequest;
71
76
 
72
- const client = getClient();
73
- const result = await client.v1.query.compileQuery(projectUuid, exploreId, body);
74
- console.log(JSON.stringify(result, null, 2));
75
- } catch (error) {
76
- console.error(
77
- 'Error compiling query:',
78
- error instanceof Error ? error.message : String(error),
79
- );
80
- process.exit(1);
81
- }
82
- });
77
+ const client = getClient();
78
+ const result = await client.v1.query.compileQuery(projectUuid, exploreId, body);
79
+ console.log(JSON.stringify(result, null, 2));
80
+ } catch (error) {
81
+ console.error(
82
+ 'Error compiling query:',
83
+ error instanceof Error ? error.message : String(error),
84
+ );
85
+ process.exit(1);
86
+ }
87
+ },
88
+ ),
89
+ );
83
90
  }
@@ -0,0 +1,66 @@
1
+ /**
2
+ * Schedulers command implementation.
3
+ */
4
+
5
+ import type { Command } from 'commander';
6
+ import { READ_ONLY_DEFAULT } from '@lightdash-tools/common';
7
+ import { getClient } from '../utils/client';
8
+ import { wrapAction } from '../utils/safety';
9
+
10
+ /**
11
+ * Registers the schedulers command and its subcommands.
12
+ */
13
+ export function registerSchedulersCommand(program: Command): void {
14
+ const schedulersCmd = program.command('schedulers').description('Manage scheduled deliveries');
15
+
16
+ schedulersCmd
17
+ .command('list <projectUuid>')
18
+ .description('List schedulers in a project')
19
+ .option('--search <query>', 'Search query')
20
+ .option('--page <number>', 'Page number', (v) => parseInt(v, 10))
21
+ .option('--page-size <number>', 'Page size', (v) => parseInt(v, 10))
22
+ .action(
23
+ wrapAction(
24
+ READ_ONLY_DEFAULT,
25
+ async (
26
+ projectUuid: string,
27
+ options: { search?: string; page?: number; pageSize?: number },
28
+ ) => {
29
+ try {
30
+ const client = getClient();
31
+ const result = await client.v1.schedulers.listSchedulers(projectUuid, {
32
+ searchQuery: options.search,
33
+ page: options.page,
34
+ pageSize: options.pageSize,
35
+ });
36
+ console.log(JSON.stringify(result, null, 2));
37
+ } catch (error) {
38
+ console.error(
39
+ 'Error listing schedulers:',
40
+ error instanceof Error ? error.message : String(error),
41
+ );
42
+ process.exit(1);
43
+ }
44
+ },
45
+ ),
46
+ );
47
+
48
+ schedulersCmd
49
+ .command('get <schedulerUuid>')
50
+ .description('Get a scheduler by UUID')
51
+ .action(
52
+ wrapAction(READ_ONLY_DEFAULT, async (schedulerUuid: string) => {
53
+ try {
54
+ const client = getClient();
55
+ const result = await client.v1.schedulers.getScheduler(schedulerUuid);
56
+ console.log(JSON.stringify(result, null, 2));
57
+ } catch (error) {
58
+ console.error(
59
+ 'Error fetching scheduler:',
60
+ error instanceof Error ? error.message : String(error),
61
+ );
62
+ process.exit(1);
63
+ }
64
+ }),
65
+ );
66
+ }
@@ -3,7 +3,9 @@
3
3
  */
4
4
 
5
5
  import type { Command } from 'commander';
6
+ import { READ_ONLY_DEFAULT } from '@lightdash-tools/common';
6
7
  import { getClient } from '../utils/client';
8
+ import { wrapAction } from '../utils/safety';
7
9
 
8
10
  /**
9
11
  * Registers the projects spaces subcommands under the existing projects command.
@@ -19,34 +21,38 @@ export function registerSpacesCommand(program: Command): void {
19
21
  spacesCmd
20
22
  .command('list <projectUuid>')
21
23
  .description('List spaces in a project')
22
- .action(async (projectUuid: string) => {
23
- try {
24
- const client = getClient();
25
- const result = await client.v1.spaces.listSpacesInProject(projectUuid);
26
- console.log(JSON.stringify(result, null, 2));
27
- } catch (error) {
28
- console.error(
29
- 'Error listing spaces:',
30
- error instanceof Error ? error.message : String(error),
31
- );
32
- process.exit(1);
33
- }
34
- });
24
+ .action(
25
+ wrapAction(READ_ONLY_DEFAULT, async (projectUuid: string) => {
26
+ try {
27
+ const client = getClient();
28
+ const result = await client.v1.spaces.listSpacesInProject(projectUuid);
29
+ console.log(JSON.stringify(result, null, 2));
30
+ } catch (error) {
31
+ console.error(
32
+ 'Error listing spaces:',
33
+ error instanceof Error ? error.message : String(error),
34
+ );
35
+ process.exit(1);
36
+ }
37
+ }),
38
+ );
35
39
 
36
40
  spacesCmd
37
41
  .command('get <projectUuid> <spaceUuid>')
38
42
  .description('Get a space by UUID')
39
- .action(async (projectUuid: string, spaceUuid: string) => {
40
- try {
41
- const client = getClient();
42
- const result = await client.v1.spaces.getSpace(projectUuid, spaceUuid);
43
- console.log(JSON.stringify(result, null, 2));
44
- } catch (error) {
45
- console.error(
46
- 'Error fetching space:',
47
- error instanceof Error ? error.message : String(error),
48
- );
49
- process.exit(1);
50
- }
51
- });
43
+ .action(
44
+ wrapAction(READ_ONLY_DEFAULT, async (projectUuid: string, spaceUuid: string) => {
45
+ try {
46
+ const client = getClient();
47
+ const result = await client.v1.spaces.getSpace(projectUuid, spaceUuid);
48
+ console.log(JSON.stringify(result, null, 2));
49
+ } catch (error) {
50
+ console.error(
51
+ 'Error fetching space:',
52
+ error instanceof Error ? error.message : String(error),
53
+ );
54
+ process.exit(1);
55
+ }
56
+ }),
57
+ );
52
58
  }
@@ -0,0 +1,53 @@
1
+ /**
2
+ * Tags command implementation.
3
+ */
4
+
5
+ import type { Command } from 'commander';
6
+ import { READ_ONLY_DEFAULT } from '@lightdash-tools/common';
7
+ import { getClient } from '../utils/client';
8
+ import { wrapAction } from '../utils/safety';
9
+
10
+ /**
11
+ * Registers the tags command and its subcommands.
12
+ */
13
+ export function registerTagsCommand(program: Command): void {
14
+ const tagsCmd = program.command('tags').description('Manage tags');
15
+
16
+ tagsCmd
17
+ .command('list <projectUuid>')
18
+ .description('List tags in a project')
19
+ .action(
20
+ wrapAction(READ_ONLY_DEFAULT, async (projectUuid: string) => {
21
+ try {
22
+ const client = getClient();
23
+ const result = await client.v1.tags.listTags(projectUuid);
24
+ console.log(JSON.stringify(result, null, 2));
25
+ } catch (error) {
26
+ console.error(
27
+ 'Error listing tags:',
28
+ error instanceof Error ? error.message : String(error),
29
+ );
30
+ process.exit(1);
31
+ }
32
+ }),
33
+ );
34
+
35
+ tagsCmd
36
+ .command('get <projectUuid> <tagUuid>')
37
+ .description('Get a tag by UUID')
38
+ .action(
39
+ wrapAction(READ_ONLY_DEFAULT, async (projectUuid: string, tagUuid: string) => {
40
+ try {
41
+ const client = getClient();
42
+ const result = await client.v1.tags.getTag(projectUuid, tagUuid);
43
+ console.log(JSON.stringify(result, null, 2));
44
+ } catch (error) {
45
+ console.error(
46
+ 'Error fetching tag:',
47
+ error instanceof Error ? error.message : String(error),
48
+ );
49
+ process.exit(1);
50
+ }
51
+ }),
52
+ );
53
+ }
@@ -3,7 +3,9 @@
3
3
  */
4
4
 
5
5
  import type { Command } from 'commander';
6
+ import { READ_ONLY_DEFAULT } from '@lightdash-tools/common';
6
7
  import { getClient } from '../utils/client';
8
+ import { wrapAction } from '../utils/safety';
7
9
 
8
10
  /**
9
11
  * Registers the users command and its subcommands.
@@ -19,55 +21,59 @@ export function registerUsersCommand(program: Command): void {
19
21
  .option('--page <number>', 'Page number (1-based)', (v: string) => parseInt(v, 10))
20
22
  .option('--page-size <number>', 'Page size', (v: string) => parseInt(v, 10))
21
23
  .option('--search <query>', 'Search query')
22
- .action(async function (this: Command) {
23
- const options = this.opts() as {
24
- all?: boolean;
25
- page?: number;
26
- pageSize?: number;
27
- search?: string;
28
- };
29
- try {
30
- const client = getClient();
31
- if (options.all) {
32
- const listParams: { searchQuery?: string } = {};
33
- if (options.search != null) listParams.searchQuery = options.search;
34
- const list = await client.v1.users.listAllMembers(
35
- Object.keys(listParams).length > 0 ? listParams : undefined,
24
+ .action(
25
+ wrapAction(READ_ONLY_DEFAULT, async function (this: Command) {
26
+ const options = this.opts() as {
27
+ all?: boolean;
28
+ page?: number;
29
+ pageSize?: number;
30
+ search?: string;
31
+ };
32
+ try {
33
+ const client = getClient();
34
+ if (options.all) {
35
+ const listParams: { searchQuery?: string } = {};
36
+ if (options.search != null) listParams.searchQuery = options.search;
37
+ const list = await client.v1.users.listAllMembers(
38
+ Object.keys(listParams).length > 0 ? listParams : undefined,
39
+ );
40
+ console.log(JSON.stringify(list, null, 2));
41
+ } else {
42
+ const params: { page?: number; pageSize?: number; searchQuery?: string } = {};
43
+ if (options.page != null) params.page = options.page;
44
+ if (options.pageSize != null) params.pageSize = options.pageSize;
45
+ if (options.search != null) params.searchQuery = options.search;
46
+ const result = await client.v1.users.listMembers(
47
+ Object.keys(params).length > 0 ? params : undefined,
48
+ );
49
+ console.log(JSON.stringify(result, null, 2));
50
+ }
51
+ } catch (error) {
52
+ console.error(
53
+ 'Error listing users:',
54
+ error instanceof Error ? error.message : String(error),
36
55
  );
37
- console.log(JSON.stringify(list, null, 2));
38
- } else {
39
- const params: { page?: number; pageSize?: number; searchQuery?: string } = {};
40
- if (options.page != null) params.page = options.page;
41
- if (options.pageSize != null) params.pageSize = options.pageSize;
42
- if (options.search != null) params.searchQuery = options.search;
43
- const result = await client.v1.users.listMembers(
44
- Object.keys(params).length > 0 ? params : undefined,
45
- );
46
- console.log(JSON.stringify(result, null, 2));
56
+ process.exit(1);
47
57
  }
48
- } catch (error) {
49
- console.error(
50
- 'Error listing users:',
51
- error instanceof Error ? error.message : String(error),
52
- );
53
- process.exit(1);
54
- }
55
- });
58
+ }),
59
+ );
56
60
 
57
61
  usersCmd
58
62
  .command('get <userUuid>')
59
63
  .description('Get a user by UUID')
60
- .action(async (userUuid: string) => {
61
- try {
62
- const client = getClient();
63
- const member = await client.v1.users.getMemberByUuid(userUuid);
64
- console.log(JSON.stringify(member, null, 2));
65
- } catch (error) {
66
- console.error(
67
- 'Error fetching user:',
68
- error instanceof Error ? error.message : String(error),
69
- );
70
- process.exit(1);
71
- }
72
- });
64
+ .action(
65
+ wrapAction(READ_ONLY_DEFAULT, async (userUuid: string) => {
66
+ try {
67
+ const client = getClient();
68
+ const member = await client.v1.users.getMemberByUuid(userUuid);
69
+ console.log(JSON.stringify(member, null, 2));
70
+ } catch (error) {
71
+ console.error(
72
+ 'Error fetching user:',
73
+ error instanceof Error ? error.message : String(error),
74
+ );
75
+ process.exit(1);
76
+ }
77
+ }),
78
+ );
73
79
  }
package/src/index.ts CHANGED
@@ -17,10 +17,22 @@ import { registerGroupsCommand } from './commands/groups';
17
17
  import { registerUsersCommand } from './commands/users';
18
18
  import { registerQueryCommand } from './commands/query';
19
19
  import { registerExploresCommand } from './commands/explores';
20
+ import { registerMetricsCommand } from './commands/metrics';
21
+ import { registerSchedulersCommand } from './commands/schedulers';
22
+ import { registerTagsCommand } from './commands/tags';
23
+ import { registerContentCommand } from './commands/content';
20
24
 
21
25
  const program = new Command();
22
26
 
23
- program.name('lightdash-ai').description('CLI for Lightdash AI').version('1.0.0');
27
+ program
28
+ .name('lightdash-ai')
29
+ .description('CLI for Lightdash AI')
30
+ .version('1.0.0')
31
+ .option(
32
+ '--mode <mode>',
33
+ 'Safety mode (read-only, write-idempotent, write-destructive)',
34
+ 'write-destructive',
35
+ );
24
36
 
25
37
  // Register all commands (organization and projects first so subcommands can attach)
26
38
  registerOrganizationCommand(program);
@@ -36,6 +48,10 @@ registerGroupsCommand(program);
36
48
  registerUsersCommand(program);
37
49
  registerQueryCommand(program);
38
50
  registerExploresCommand(program);
51
+ registerMetricsCommand(program);
52
+ registerSchedulersCommand(program);
53
+ registerTagsCommand(program);
54
+ registerContentCommand(program);
39
55
 
40
56
  // Parse command line arguments
41
57
  program.parse(process.argv);
@@ -0,0 +1,53 @@
1
+ import { describe, it, expect, vi, beforeEach } from 'vitest';
2
+ import { wrapAction } from './safety';
3
+ import { READ_ONLY_DEFAULT, WRITE_DESTRUCTIVE, SafetyMode } from '@lightdash-tools/common';
4
+ import { Command } from 'commander';
5
+
6
+ describe('CLI wrapAction', () => {
7
+ const mockAction = vi.fn().mockResolvedValue(undefined);
8
+ const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
9
+ const processExitSpy = vi.spyOn(process, 'exit').mockImplementation(() => {
10
+ throw new Error('exit');
11
+ });
12
+
13
+ beforeEach(() => {
14
+ vi.clearAllMocks();
15
+ });
16
+
17
+ it('should allow read-only action in read-only mode', async () => {
18
+ const cmd = new Command();
19
+ cmd.setOptionValueWithSource('mode', SafetyMode.READ_ONLY, 'cli');
20
+
21
+ const wrapped = wrapAction(READ_ONLY_DEFAULT, mockAction);
22
+ await wrapped.call(cmd, 'arg1');
23
+
24
+ expect(mockAction).toHaveBeenCalledWith('arg1');
25
+ expect(processExitSpy).not.toHaveBeenCalled();
26
+ });
27
+
28
+ it('should block destructive action in read-only mode', async () => {
29
+ const cmd = new Command();
30
+ cmd.setOptionValueWithSource('mode', SafetyMode.READ_ONLY, 'cli');
31
+
32
+ const wrapped = wrapAction(WRITE_DESTRUCTIVE, mockAction);
33
+
34
+ await expect(wrapped.call(cmd, 'arg1')).rejects.toThrow('exit');
35
+
36
+ expect(mockAction).not.toHaveBeenCalled();
37
+ expect(consoleErrorSpy).toHaveBeenCalledWith(
38
+ expect.stringContaining('disabled in read-only mode'),
39
+ );
40
+ expect(processExitSpy).toHaveBeenCalledWith(1);
41
+ });
42
+
43
+ it('should allow destructive action in write-destructive mode', async () => {
44
+ const cmd = new Command();
45
+ cmd.setOptionValueWithSource('mode', SafetyMode.WRITE_DESTRUCTIVE, 'cli');
46
+
47
+ const wrapped = wrapAction(WRITE_DESTRUCTIVE, mockAction);
48
+ await wrapped.call(cmd, 'arg1');
49
+
50
+ expect(mockAction).toHaveBeenCalledWith('arg1');
51
+ expect(processExitSpy).not.toHaveBeenCalled();
52
+ });
53
+ });
@@ -0,0 +1,33 @@
1
+ import { isAllowed, getSafetyModeFromEnv, SafetyMode } from '@lightdash-tools/common';
2
+ import type { ToolAnnotations } from '@lightdash-tools/common';
3
+ import type { Command } from 'commander';
4
+
5
+ /**
6
+ * Resolves the safety mode from the command line options or environment variables.
7
+ */
8
+ export function getSafetyMode(cmd: Command): SafetyMode {
9
+ const options = cmd.optsWithGlobals() as { mode?: string };
10
+ if (options.mode && Object.values(SafetyMode).includes(options.mode as SafetyMode)) {
11
+ return options.mode as SafetyMode;
12
+ }
13
+ return getSafetyModeFromEnv();
14
+ }
15
+
16
+ /**
17
+ * Wraps a CLI action with a safety check.
18
+ */
19
+ export function wrapAction<T extends unknown[]>(
20
+ annotations: ToolAnnotations,
21
+ action: (...args: T) => Promise<void> | void,
22
+ ) {
23
+ return async function (this: Command, ...args: T) {
24
+ const mode = getSafetyMode(this);
25
+ if (!isAllowed(mode, annotations)) {
26
+ console.error(
27
+ `Error: This command is disabled in ${mode} mode. To enable it, use --mode or set LIGHTDASH_AI_MODE.`,
28
+ );
29
+ process.exit(1);
30
+ }
31
+ return action.apply(this, args);
32
+ };
33
+ }