Skip to content

Commit 08ba4f2

Browse files
committed
enable git
1 parent 265c3ed commit 08ba4f2

File tree

4 files changed

+86
-40
lines changed

4 files changed

+86
-40
lines changed

config/snippets/tiat-examples.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1242,7 +1242,7 @@
12421242
"example": "resource \"tencentcloud_image\" \"image_snap\" {\n image_name = \"image-snapshot-keep\"\n snapshot_ids = [\"snap-nbp3xy1d\", \"snap-nvzu3dmh\"]\n force_poweroff = true\n image_description = \"create image with snapshot\"\n}\n"
12431243
},
12441244
"tencentcloud_instance": {
1245-
"example": "data \"tencentcloud_images\" \"my_favorite_image\" {\n image_type = [\"PUBLIC_IMAGE\"]\n image_name_regex = \"Final\"\n}\n\ndata \"tencentcloud_instance_types\" \"my_favorite_instance_types\" {\n filter {\n name = \"instance-family\"\n values = [\"S1\", \"S2\", \"S3\", \"S4\", \"S5\"]\n }\n\n cpu_core_count = 2\n exclude_sold_out = true\n}\n\ndata \"tencentcloud_availability_zones\" \"my_favorite_zones\" {\n}\n\n// Create VPC resource\nresource \"tencentcloud_vpc\" \"app\" {\n cidr_block = \"10.0.0.0/16\"\n name = \"awesome_app_vpc\"\n}\n\nresource \"tencentcloud_subnet\" \"app\" {\n vpc_id = tencentcloud_vpc.app.id\n availability_zone = data.tencentcloud_availability_zones.my_favorite_zones.zones.0.name\n name = \"awesome_app_subnet\"\n cidr_block = \"10.0.1.0/24\"\n}\n\n// Create a POSTPAID_BY_HOUR CVM instance\nresource \"tencentcloud_instance\" \"cvm_postpaid\" {\n instance_name = \"cvm_postpaid\"\n availability_zone = data.tencentcloud_availability_zones.my_favorite_zones.zones.0.name\n image_id = data.tencentcloud_images.my_favorite_image.images.0.image_id\n instance_type = data.tencentcloud_instance_types.my_favorite_instance_types.instance_types.0.instance_type\n system_disk_type = \"CLOUD_PREMIUM\"\n system_disk_size = 50\n hostname = \"user\"\n project_id = 0\n vpc_id = tencentcloud_vpc.app.id\n subnet_id = tencentcloud_subnet.app.id\n\n data_disks {\n data_disk_type = \"CLOUD_PREMIUM\"\n data_disk_size = 50\n encrypt = false\n }\n\n tags = {\n tagKey = \"tagValue\"\n }\n}\n\n// Create a PREPAID CVM instance\nresource \"tencentcloud_instance\" \"cvm_prepaid\" {\n timeouts {\n create = \"30m\"\n }\n instance_name = \"cvm_prepaid\"\n availability_zone = data.tencentcloud_availability_zones.my_favorite_zones.zones.0.name\n image_id = data.tencentcloud_images.my_favorite_image.images.0.image_id\n instance_type = data.tencentcloud_instance_types.my_favorite_instance_types.instance_types.0.instance_type\n system_disk_type = \"CLOUD_PREMIUM\"\n system_disk_size = 50\n hostname = \"user\"\n project_id = 0\n vpc_id = tencentcloud_vpc.app.id\n subnet_id = tencentcloud_subnet.app.id\n instance_charge_type = \"PREPAID\"\n instance_charge_type_prepaid_period = 1\n instance_charge_type_prepaid_renew_flag = \"NOTIFY_AND_MANUAL_RENEW\"\n data_disks {\n data_disk_type = \"CLOUD_PREMIUM\"\n data_disk_size = 50\n encrypt = false\n }\n force_delete = true\n tags = {\n tagKey = \"tagValue\"\n }\n}\n"
1245+
"example": "data \"tencentcloud_images\" \"my_favorite_image\" {\n image_type = [\"PUBLIC_IMAGE\"]\n image_name_regex = \"Final\"\n}\n\ndata \"tencentcloud_instance_types\" \"my_favorite_instance_types\" {\n filter {\n name = \"instance-family\"\n values = [\"S5\"]\n }\n\n cpu_core_count = 2\n exclude_sold_out = true\n}\n\ndata \"tencentcloud_availability_zones_by_product\" \"my_favorite_zones\" {\n product = \"cvm\"\n}\n\n// Create VPC resource\nresource \"tencentcloud_vpc\" \"app\" {\n cidr_block = \"10.0.0.0/16\"\n name = \"awesome_app_vpc\"\n}\n\nresource \"tencentcloud_subnet\" \"app\" {\n vpc_id = tencentcloud_vpc.app.id\n availability_zone = data.tencentcloud_availability_zones_by_product.my_favorite_zones.zones.0.name\n name = \"awesome_app_subnet\"\n cidr_block = \"10.0.1.0/24\"\n}\n\n// Create a POSTPAID_BY_HOUR CVM instance\nresource \"tencentcloud_instance\" \"cvm_postpaid\" {\n instance_name = \"cvm_postpaid\"\n availability_zone = data.tencentcloud_availability_zones_by_product.my_favorite_zones.zones.0.name\n image_id = data.tencentcloud_images.my_favorite_image.images.0.image_id\n instance_type = data.tencentcloud_instance_types.my_favorite_instance_types.instance_types.0.instance_type\n system_disk_type = \"CLOUD_PREMIUM\"\n system_disk_size = 50\n hostname = \"user\"\n project_id = 0\n vpc_id = tencentcloud_vpc.app.id\n subnet_id = tencentcloud_subnet.app.id\n\n data_disks {\n data_disk_type = \"CLOUD_PREMIUM\"\n data_disk_size = 50\n encrypt = false\n }\n\n tags = {\n tagKey = \"tagValue\"\n }\n}\n"
12461246
},
12471247
"tencentcloud_instance_set": {
12481248
"example": "data \"tencentcloud_images\" \"my_favorite_image\" {\n image_type = [\"PUBLIC_IMAGE\"]\n os_name = \"Tencent Linux release 3.2 (Final)\"\n}\n\ndata \"tencentcloud_instance_types\" \"my_favorite_instance_types\" {\n filter {\n name = \"instance-family\"\n values = [\"S3\"]\n }\n\n cpu_core_count = 1\n memory_size = 1\n}\n\ndata \"tencentcloud_availability_zones\" \"my_favorite_zones\" {\n}\n\n// Create VPC resource\nresource \"tencentcloud_vpc\" \"app\" {\n cidr_block = \"10.0.0.0/16\"\n name = \"awesome_app_vpc\"\n}\n\nresource \"tencentcloud_subnet\" \"app\" {\n vpc_id = tencentcloud_vpc.app.id\n availability_zone = data.tencentcloud_availability_zones.my_favorite_zones.zones.0.name\n name = \"awesome_app_subnet\"\n cidr_block = \"10.0.1.0/24\"\n}\n\n// Create 10 CVM instances to host awesome_app\nresource \"tencentcloud_instance_set\" \"my_awesome_app\" {\n timeouts {\n create = \"5m\"\n read = \"20s\"\n delete = \"1h\"\n }\n\n instance_count = 10\n instance_name = \"awesome_app\"\n availability_zone = data.tencentcloud_availability_zones.my_favorite_zones.zones.0.name\n image_id = data.tencentcloud_images.my_favorite_image.images.0.image_id\n instance_type = data.tencentcloud_instance_types.my_favorite_instance_types.instance_types.0.instance_type\n system_disk_type = \"CLOUD_PREMIUM\"\n system_disk_size = 50\n hostname = \"user\"\n project_id = 0\n vpc_id = tencentcloud_vpc.app.id\n subnet_id = tencentcloud_subnet.app.id\n}\n"

src/extension.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ export async function activate(context: vscode.ExtensionContext) {
4242
terraformShellManager.getShell().runTerraformCmd(TerraformCommand.Destroy);
4343
}));
4444

45+
// git operations
46+
context.subscriptions.push(vscode.commands.registerCommand('tcTerraform.git.push', async () => {
47+
if (_.isEmpty(vscode.workspace.workspaceFolders)) {
48+
vscode.window.showInformationMessage("Please open a workspace in VS Code first.");
49+
return;
50+
}
51+
await GitUtils.getInstance().submitToGit();
52+
}));
53+
4554
// terraformer cmd
4655
let disposableTferImport = vscode.commands.registerCommand('tcTerraformer.import', async () => {
4756
terraformShellManager.getShell().runTerraformCmd(TerraformerCommand.Import);

src/utils/gitUtils.ts

Lines changed: 59 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as vscode from 'vscode';
22
import { exec } from 'child_process';
33
import path from 'path';
44
import * as fs from 'fs';
5+
import * as workspaceUtils from "./workspaceUtils";
56

67
export class GitUtils {
78
private static instance: GitUtils;
@@ -15,44 +16,36 @@ export class GitUtils {
1516

1617
public async submitToGit(): Promise<any> {
1718
console.debug("[DEBUG]#### GitUtils submitToGit begin.");
18-
const gitRootPath = vscode.workspace.rootPath;
19+
const activeDocumentPath = workspaceUtils.getActiveEditorPath();
20+
const gitRootPath = path.dirname(activeDocumentPath);
1921
if (!gitRootPath) {
2022
vscode.window.showErrorMessage('Please open a workspace folder first!');
2123
return;
2224
}
2325

24-
while (true) {
25-
if (fs.existsSync(path.join(gitRootPath, '.git'))) {
26-
vscode.window.showInformationMessage('Trying to fetch from Git, please wait...');
27-
await this.fetchFromGit();
28-
break;
29-
} else {
30-
vscode.window.showInformationMessage(`You are not in a git repository yet, trying to clone from your Git repo...`);
31-
await this.cloneFromGit();
32-
// retry
33-
}
26+
if (fs.existsSync(path.join(gitRootPath, '.git'))) {
27+
vscode.window.showInformationMessage('Trying to fetch from Git, please wait...');
28+
await this.fetchFromGit(gitRootPath);
29+
} else {
30+
vscode.window.showInformationMessage(`You are not in a git repository yet, trying to clone from your Git repo...`);
31+
await this.cloneFromGit(gitRootPath);
3432
}
3533

36-
exec('git add . && git commit -m "committed by VS Code" && git push', { cwd: gitRootPath }, (error, stdout, stderr) => {
37-
if (error) {
38-
vscode.window.showErrorMessage(`Failed to submit code: ${error.message}`);
39-
return;
40-
}
41-
if (stderr) {
42-
vscode.window.showErrorMessage(`Failed to submit code: ${stderr}`);
43-
return;
44-
}
45-
vscode.window.showInformationMessage('Code has been successfully submitted to Git repo!');
46-
});
47-
return;
34+
console.debug("[DEBUG]#### GitUtils pushToGit begin.");
35+
try {
36+
const output = await this.gitPush(gitRootPath);
37+
vscode.window.showInformationMessage(`Code has been successfully submitted to Git repo!\nDetail:[${output}]`);
38+
} catch (error) {
39+
vscode.window.showErrorMessage(`Failed to submit code: ${error.message}`);
40+
}
4841
}
4942

50-
private async cloneFromGit(): Promise<any> {
43+
private async cloneFromGit(rootPath: string): Promise<any> {
5144
console.debug("[DEBUG]#### GitUtils cloneFromGit begin.");
52-
await vscode.window.showInputBox({ prompt: 'Please enter the Git repository URL:' }).then((url) => {
45+
await vscode.window.showInputBox({ prompt: 'Please enter the Git repository URL:' }).then(async (url) => {
5346
if (url) {
5447
const folderName = url.split('/').pop()?.replace('.git', '');
55-
const clonePath = vscode.workspace.rootPath ? `${vscode.workspace.rootPath}/${folderName}` : folderName || '';
48+
const clonePath = rootPath ? `${rootPath}/${folderName}` : folderName || '';
5649
if (fs.existsSync(clonePath)) {
5750
console.debug("[DEBUG]#### GitUtils cloneFromGit: clean and remove the clonePath before cloning the repository.");
5851
fs.rm(clonePath, { recursive: true }, () => { });
@@ -72,23 +65,50 @@ export class GitUtils {
7265
});
7366
}
7467
});
75-
return;
7668
}
7769

78-
private async fetchFromGit(): Promise<any> {
70+
private gitPush(rootPath: string): Promise<string> {
71+
return new Promise((resolve, reject) => {
72+
exec('git add . && git commit -m "committed by VS Code" && git push', { cwd: rootPath }, (error, stdout, stderr) => {
73+
if (error) {
74+
reject(error);
75+
return;
76+
}
77+
if (stdout) {
78+
resolve(stdout);
79+
return;
80+
}
81+
if (stderr) {
82+
reject(new Error(stderr));
83+
}
84+
});
85+
});
86+
}
87+
88+
private gitPull(rootPath: string): Promise<void> {
89+
return new Promise((resolve, reject) => {
90+
exec('git pull', { cwd: rootPath }, (error, stdout, stderr) => {
91+
if (error) {
92+
reject(error);
93+
return;
94+
}
95+
if (stderr) {
96+
reject(new Error(stderr));
97+
return;
98+
}
99+
resolve();
100+
});
101+
});
102+
}
103+
104+
private async fetchFromGit(rootPath: string): Promise<any> {
79105
console.debug("[DEBUG]#### GitUtils fetchFromGit begin.");
80-
const gitRootPath = vscode.workspace.rootPath;
81-
exec('git pull', { cwd: gitRootPath }, (error, stdout, stderr) => {
82-
if (error) {
83-
vscode.window.showErrorMessage(`Failed to fetch code: ${error.message}`);
84-
return;
85-
}
86-
if (stderr) {
87-
vscode.window.showErrorMessage(`Failed to fetch code: ${stderr}`);
88-
return;
89-
}
106+
try {
107+
await this.gitPull(rootPath);
90108
vscode.window.showInformationMessage('Code has been successfully fetched from Git repo!');
91-
});
109+
} catch (error) {
110+
vscode.window.showErrorMessage(`Failed to fetch code: ${error.message}`);
111+
}
92112
}
93113

94114
}

src/utils/workspaceUtils.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,20 @@ export async function selectWorkspaceFolder(): Promise<string | undefined> {
4040
}
4141
return folder ? folder.uri.fsPath : undefined;
4242
}
43+
44+
export function getActiveEditorPath(): string {
45+
const activeEditor = vscode.window.activeTextEditor;
46+
if (!activeEditor) {
47+
vscode.window.showInformationMessage('No active editor found.');
48+
return "";
49+
}
50+
51+
const activeDocument = activeEditor.document;
52+
if (!activeDocument) {
53+
vscode.window.showInformationMessage('No active document found.');
54+
return "";
55+
}
56+
57+
const activeDocumentPath = activeDocument.uri.fsPath;
58+
return activeDocumentPath;
59+
}

0 commit comments

Comments
 (0)