-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
127 lines (116 loc) · 4.43 KB
/
index.js
File metadata and controls
127 lines (116 loc) · 4.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/bin/env node
const { Command } = require('commander');
const path = require('path');
const { loadEnv } = require('./src/config');
const { createLoadForgeClient } = require('./src/api');
const { pullCommand } = require('./src/commands/pull');
const { pushCommand } = require('./src/commands/push');
const { startRunBySlug } = require('./src/commands/start');
const { waitForResult } = require('./src/commands/wait');
const { createTestCommand } = require('./src/commands/create');
async function main() {
loadEnv();
const program = new Command();
program
.name('lf-cli')
.description('CLI helper for LoadForge')
.version('1.0.0');
program
.command('pull')
.description('Pull all LoadForge test scripts (locustfiles)')
.option('-o, --out <dir>', 'Output directory', 'tests')
.action(async (opts) => {
const apiKey = process.env.API_KEY;
if (!apiKey) {
console.error('Missing API_KEY in environment (.env)');
process.exit(1);
}
const client = createLoadForgeClient({ apiKey });
const outDir = path.resolve(process.cwd(), opts.out || '.');
await pullCommand({ client, outDir });
});
program
.command('push')
.description('Push local test folders to LoadForge by unique name')
.option('--dir <dir>', 'Directory to scan', 'tests')
.option('--dry-run', 'Show plan only', false)
.option('--allow-create', 'Create tests that do not exist remotely', false)
.option('--allow-delete', 'Delete remote tests not present locally', false)
.option('--try-extended', 'Try to send extended fields (fallback on 400)', true)
.option('--verbose', 'Enable verbose debug logging', false)
.action(async (opts) => {
const apiKey = process.env.API_KEY;
if (!apiKey) {
console.error('Missing API_KEY in environment (.env)');
process.exit(1);
}
const client = createLoadForgeClient({ apiKey });
const rootDir = require('path').resolve(process.cwd(), opts.dir || '.');
await pushCommand({
client,
rootDir,
dryRun: !!opts.dryRun,
allowCreate: !!opts.allowCreate,
allowDelete: !!opts.allowDelete,
tryExtended: opts.tryExtended !== false,
verbose: !!opts.verbose,
});
});
program
.command('start <slug>')
.description('Start a run by test slug (name). Prints run_id to stdout')
.option('-d, --duration <mins>', 'Duration in minutes (2-720)', '5')
.option('--verbose', 'Enable verbose debug logging', false)
.action(async (slug, opts) => {
const apiKey = process.env.API_KEY;
if (!apiKey) {
console.error('Missing API_KEY in environment (.env)');
process.exit(1);
}
const client = createLoadForgeClient({ apiKey });
const duration = Math.max(2, Math.min(720, Number(opts.duration || 5)));
await startRunBySlug({ client, slug, duration, verbose: !!opts.verbose });
});
program
.command('wait <id>')
.description('Wait for a run/result to finish; exits 0 on success, 1 on failure')
.option('-i, --interval <seconds>', 'Polling interval seconds', '5')
.option('--verbose', 'Enable verbose debug logging', false)
.action(async (id, opts) => {
const apiKey = process.env.API_KEY;
if (!apiKey) {
console.error('Missing API_KEY in environment (.env)');
process.exit(1);
}
const client = createLoadForgeClient({ apiKey });
const intervalMs = Math.max(1, Number(opts.interval || '5')) * 1000;
await waitForResult({ client, id, intervalMs, verbose: !!opts.verbose });
});
program
.command('create')
.description('Create a new test folder under tests/ with config and locustfile')
.option('-n, --name <slug>', 'Test name (slug)')
.option('-u, --users <num>', 'Users (number)')
.option('--host <host>', 'Host as protocol://url:port')
.option('-o, --out <dir>', 'Output directory', 'tests')
.action(async (opts) => {
const apiKey = process.env.API_KEY;
if (!apiKey) {
console.error('Missing API_KEY in environment (.env)');
process.exit(1);
}
const client = createLoadForgeClient({ apiKey });
await createTestCommand({
client,
outDir: opts.out,
name: opts.name,
users: opts.users,
host: opts.host,
});
});
await program.parseAsync(process.argv);
}
main().catch((err) => {
console.error(err?.message || err);
process.exit(1);
});