Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

uploadToK6Cloud #16

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@

Enhances Visual Studio Code with the ability to run k6 tests both on your local machine and in the Grafana Cloud.

## Commands

![VS Code k6 Commands](vscode-commands.png)

- Run current file
- Run current file in k6 cloud
- Upload current file to k6 cloud
- Open Settings

### For more information

- [k6.io](https://k6.io)
Expand Down
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@
"command": "k6.runWithK6Cloud",
"title": "k6: Run current file in k6 cloud"
},
{
"command": "k6.uploadToK6Cloud",
"title": "k6: Upload current file to k6 cloud"
},
{
"command": "k6.openSettings",
"title": "k6: Open Settings"
Expand Down
61 changes: 61 additions & 0 deletions src/commands/upload/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import * as vscode from 'vscode';
import { spawn } from 'child_process';
import openSettings from '../settings';

export default async () => {
const currentFile = vscode.window.activeTextEditor?.document.uri;
if (currentFile?.scheme !== 'file') {
vscode.window.showErrorMessage(
'The k6 extension currently only works with files saved on disk'
);
return;
}

if (
!vscode.workspace.getConfiguration('k6').get('cloudToken') &&
!process.env.K6_CLOUD_TOKEN
) {
const openSettingsButton = { title: 'Open k6 settings' };
const action = await vscode.window.showErrorMessage(
'This option requires you to either have a cloud token added in your environment variables or in the k6 extension settings',
{},
openSettingsButton
);
if (action === openSettingsButton) {
openSettings('k6.cloudToken');
}
return;
}

const channel = vscode.window.createOutputChannel('k6');
channel.show();
channel.appendLine('Starting k6...');

const k6 = spawn('k6', ['cloud', '--upload-only', currentFile.fsPath], {
env: {
...process.env,
K6_CLOUD_TOKEN:
vscode.workspace.getConfiguration('k6').get('cloudToken') ??
process.env.K6_CLOUD_TOKEN,
},
});

k6.stdout.on('data', (data) => channel.append(data.toString()));
k6.stderr.on('data', (data) => channel.append(data.toString()));

k6.on('error', (e) => {
if (e.message.includes('ENOENT')) {
channel.appendLine(
"Looks like you've not installed k6 into your PATH. Aborting."
);
} else {
channel.appendLine(
'Something went wrong while launching k6. See the error below for details'
);
channel.appendLine(JSON.stringify(e));
}
});
k6.on('exit', (code) => {
channel.appendLine('k6 exited with exit code ' + code);
});
};
2 changes: 2 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import * as vscode from 'vscode';

import runWithK6 from './commands/run';
import uploadToK6Cloud from './commands/upload';
import detect from './commands/detect';
import openSettings from './commands/settings';

Expand All @@ -29,6 +30,7 @@ export function activate(context: vscode.ExtensionContext) {

addEditorCommand(context, 'k6.runWithK6', () => runWithK6(false));
addEditorCommand(context, 'k6.runWithK6Cloud', () => runWithK6(true));
addEditorCommand(context, 'k6.uploadToK6Cloud', () => uploadToK6Cloud());
addCommand(context, 'k6.openSettings', () => openSettings());
}

Expand Down