@@ -18,8 +18,14 @@ import { drawGraph } from "./utils/dotUtils";
1818import { isDotInstalled } from "./utils/dotUtils" ;
1919import { selectWorkspaceFolder } from "./utils/workspaceUtils" ;
2020import { checkTerraformInstalled } from "./terraformUtils" ;
21+ import { TerraformCommand } from "./common" ;
22+ import * as helper from "./utils/helper" ;
23+ import { command } from "./commons/tencent/commands" ;
24+ import { promisify } from "util" ;
25+ import { ChildProcess } from "child_process" ;
26+ import * as cp from "child_process" ;
27+
2128// import stripAnsi from 'strip-ansi';
22- import stripAnsi from 'strip-ansi' ;
2329
2430export class IntegratedShell extends BaseShell {
2531 private static readonly GRAPH_FILE_NAME = "graph.png" ;
@@ -72,32 +78,29 @@ export class IntegratedShell extends BaseShell {
7278 const fileName = ( file === undefined ) ? params . resource . type + '.tf' : file ;
7379
7480 const defaultContents = `resource "${ params . resource . type } " "${ params . resource . name } " {}` ;
81+ const resAddress = `${ params . resource . type } .${ params . resource . name } ` ;
7582
7683 const tfFile : string = path . join ( cwd , fileName ) ;
7784
78- if ( ! fse . existsSync ( tfFile ) ) {
79- fse . writeFileSync ( tfFile , defaultContents ) ;
80- } else {
81- await fse . writeFile ( tfFile , defaultContents ) ;
82- }
85+ // reset file
86+ await this . resetFileContent ( tfFile , defaultContents ) ;
87+ // reset state
88+ await this . resetTFState ( resAddress ) ;
8389
8490 const importArgs = [ 'import ' , params . resource . type , '.' , params . resource . name , ' ' , params . resource . id ] . join ( '' ) ;
85- console . debug ( "[DEBUG]#### import cmd: args=[$s], defaultContents=[%s]" , importArgs , defaultContents ) ;
86-
87- // await this.deleteFile(cwd, fileName);
88- // const output: string = await executeCommand(
89- // "terraform",
90- // [args],
91- // {
92- // shell: true,
93- // cwd,
94- // },
95- // );
96-
97- await this . runTerraformCmd ( "terraform " + importArgs ) ;
98-
99- // const stripAnsiPromise = import('strip-ansi');
100- // const stripAnsi = (await stripAnsiPromise).default;
91+ console . debug ( "[DEBUG]#### import cmd: args=[%s], defaultContents=[%s]" , importArgs , defaultContents ) ;
92+
93+ const importRet : string = await executeCommand (
94+ "terraform" ,
95+ [ importArgs ] ,
96+ {
97+ shell : true ,
98+ cwd,
99+ } ,
100+ ) ;
101+
102+ // await this.runTerraformCmd("terraform " + importArgs);
103+
101104 const content : string =
102105 await executeCommand (
103106 "terraform" ,
@@ -107,10 +110,6 @@ export class IntegratedShell extends BaseShell {
107110 cwd,
108111 }
109112 ) ;
110- // const content = stripAnsi(output);
111-
112- const my = stripAnsi ( '\u001B]8;;https://github.com\u0007Click\u001B]8;;\u0007' ) ;
113- console . debug ( "my:%s" , my ) ;
114113
115114 console . debug ( "[DEBUG]#### import content:[%s]" , content ) ;
116115 fse . writeFileSync ( tfFile , content ) ;
@@ -119,10 +118,36 @@ export class IntegratedShell extends BaseShell {
119118 await commands . executeCommand ( "vscode.open" , Uri . file ( tfFile ) , ViewColumn . Active || ViewColumn . One ) ;
120119 }
121120
122- public async runTerraformCmd ( tfCommand : string ) {
121+ private async resetFileContent ( tfFile : string , defaultContents : string ) {
122+ if ( ! fse . existsSync ( tfFile ) ) {
123+ fse . writeFileSync ( tfFile , defaultContents ) ;
124+ } else {
125+ await fse . writeFile ( tfFile , defaultContents ) ;
126+ }
127+ }
128+
129+ private async resetTFState ( resAddress : string ) {
130+ // const runFunc = () => this.runTerraformCmdWithoutTerminal(TerraformCommand.State, ['rm', '-lock=false', resAddress]);
131+ // await helper.retryF(runFunc);
132+
133+ await this . runTerraformCmd ( TerraformCommand . State , [ 'rm' , '-lock=false' , resAddress ] ) ;
134+ }
135+
136+ public async runTerraformCmdWithoutTerminal ( tfCommand : string , args ?: string [ ] ) {
137+ const cmd = [ tfCommand , ...( args || [ ] ) ] . join ( ' ' ) ;
138+ const { stdout, stderr } = await promisify ( cp . exec ) ( cmd ) ;
139+ return { stdout, stderr } ;
140+ }
141+
142+ public async runTerraformCmd ( tfCommand : string , args ?: string [ ] ) {
123143 this . checkCreateTerminal ( ) ;
124144 this . terminal . show ( ) ;
125- this . terminal . sendText ( tfCommand ) ;
145+
146+ // const cmd= [tfCommand, args.values].join(' ');
147+ let tmp : string [ ] = [ tfCommand ] ;
148+ args . forEach ( ( arg ) => tmp . push ( arg ) ) ;
149+ const cmd = tmp . join ( ' ' ) ;
150+ this . terminal . sendText ( cmd ) ;
126151 }
127152
128153 public async runNormalCmd ( tfCommand : string , newLine = true ) {
0 commit comments