Skip to content

Commit

Permalink
feat: add setting page for updating keploy yaml (#71)
Browse files Browse the repository at this point in the history
Signed-off-by: Hermione Dadheech <[email protected]>
Signed-off-by: Shivam Sourav Jha <[email protected]>
Co-authored-by: Shivam Sourav Jha <[email protected]>
  • Loading branch information
Hermione2408 and shivamsouravjha authored Oct 7, 2024
1 parent bd0e690 commit 27edc34
Show file tree
Hide file tree
Showing 4 changed files with 300 additions and 8 deletions.
51 changes: 51 additions & 0 deletions src/Config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,56 @@
import * as vscode from 'vscode';
import { existsSync, readFileSync } from 'fs';
import * as path from 'path';
import * as yaml from 'js-yaml';
import { writeFileSync } from 'fs';
export interface PartialKeployConfig {
appName:string;
command:string;
containerName:string;
networkName: string;
test: {
delay: number;
apiTimeout: number;
mongoPassword: string;
};
}

// Function to update keploy.yaml
export async function updateKeployYaml(newConfig: PartialKeployConfig) {
const workspaceFolders = vscode.workspace.workspaceFolders;
if (!workspaceFolders) {
vscode.window.showErrorMessage('No workspace folder found');
return;
}

const keployFilePath = path.join(workspaceFolders[0].uri.fsPath, 'keploy.yml');

if (!existsSync(keployFilePath)) {
vscode.window.showErrorMessage('keploy.yaml file not found');
return;
}

try {
const fileContents = readFileSync(keployFilePath, 'utf8');
const config = yaml.load(fileContents) as any; // Load the YAML content into an object
console.log('config', config);
config.appName = newConfig.appName;
config.command = newConfig.command;
config.containerName = newConfig.containerName;
config.networkName = newConfig.networkName;
config.test.delay = newConfig.test.delay;
config.test.apiTimeout = newConfig.test.apiTimeout;
config.test.mongoPassword = newConfig.test.mongoPassword;

// Convert the updated config object back to YAML format
const newYamlContent = yaml.dump(config);
writeFileSync(keployFilePath, newYamlContent, 'utf8');

vscode.window.showInformationMessage('Keploy config updated successfully!');
} catch (error) {
vscode.window.showErrorMessage(`Failed to update keploy.yaml: ${error}`);
}
}

export async function handleOpenKeployConfigFile(webview: any) {
const folderPath = vscode.workspace.workspaceFolders?.[0].uri.fsPath;
Expand Down
51 changes: 50 additions & 1 deletion src/SidebarProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import { getNonce } from "./Utils";
import { startRecording, stopRecording } from "./Record";
import { startTesting, stopTesting, displayTestCases, displayPreviousTestResults } from "./Test";
import { existsSync } from "fs";
import { handleInitializeKeployConfigFile, handleOpenKeployConfigFile } from "./Config";
import { handleInitializeKeployConfigFile, handleOpenKeployConfigFile,updateKeployYaml,PartialKeployConfig } from "./Config";
import SignInWithGitHub from "./SignIn";
import oneClickInstall from './OneClickInstall';
import * as path from 'path';
import * as fs from 'fs';
import { workspace } from 'vscode';
const yaml = require('js-yaml');

function precheckFunction(): Promise<string> {
const workspacePath = workspace.workspaceFolders ? workspace.workspaceFolders[0].uri.fsPath : '';
Expand Down Expand Up @@ -152,6 +153,34 @@ export class SidebarProvider implements vscode.WebviewViewProvider {
});
webviewView.webview.onDidReceiveMessage(async (data) => {
switch (data.type) {
case "getKeployConfig":{
// Load the keploy.yml config file
const workspaceFolders = vscode.workspace.workspaceFolders;
if (!workspaceFolders) {
vscode.window.showErrorMessage('No workspace folder found');
return;
}

const keployFilePath = path.join(workspaceFolders[0].uri.fsPath, 'keploy.yml');

if (!fs.existsSync(keployFilePath)) {
vscode.window.showErrorMessage('keploy.yml file not found');
return;
}

try {
const fileContents = fs.readFileSync(keployFilePath, 'utf8');
const config = yaml.load(fileContents); // Parse YAML into JS object

// Send the config data back to the webview
webviewView.webview.postMessage({
type: 'keployConfig',
config: config,
});
} catch (err) {
vscode.window.showErrorMessage(`Error reading keploy.yml: ${err}`);
}
}
case "onInfo": {
if (!data.value) {
return;
Expand Down Expand Up @@ -542,10 +571,30 @@ export class SidebarProvider implements vscode.WebviewViewProvider {
}
break;
}

case "updateKeployConfig": {
// Collect the updated data from the UI
const newConfig: PartialKeployConfig = {
appName: data.config.appName,
command: data.config.command,
containerName: data.config.containerName,
networkName: data.config.networkName,
test: {
delay: data.config.test?.delay,
apiTimeout: data.config.test?.apiTimeout,
mongoPassword: data.config.test?.mongoPassword,
},
};

// Call the function to update the YAML config
await updateKeployYaml(newConfig);
break;
}

}

});

}
private _startApiResponseUpdates() {
this._interval = setInterval(() => {
Expand Down
2 changes: 1 addition & 1 deletion webviews/components/IntegrationTest.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,7 @@
display: flex;
flex-direction: row;
align-items: start;
padding: 4vw 5vw;
padding: 2.5vw 5vw;
width: 100%; /* Full width of the container */
justify-content: flex-end; /* Align icons to the left */
}
Expand Down
Loading

0 comments on commit 27edc34

Please sign in to comment.