Skip to content

Commit

Permalink
inspec shell multiline
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-rock committed Sep 21, 2016
1 parent 4105e7d commit 10154c1
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
42 changes: 37 additions & 5 deletions www/tutorial/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ export class AppComponent implements OnInit {
white: string = "";
black: string = "";

// inspec shell depth
shell_depth = 0;
shell_command = ''

constructor(private http: Http) { }

ngOnInit() {
Expand Down Expand Up @@ -130,11 +134,11 @@ export class AppComponent implements OnInit {
console.log('no match found')
if (command.match(/^inspec exec\s*.*/)) {
let target = command.match(/^inspec exec\s*(.*)/)
response = this.red + "Could not fetch inspec profile in '" + target[1] + "' " + this.black + msg;
response = this.red + "Could not fetch inspec profile in '" + target[1] + "' " + this.black;
} else {
response = this.red + 'command not found: ' + command;
}
this.printOnStdout(response)
this.printOnStdout(response + "\n\r")
}
}

Expand All @@ -143,6 +147,7 @@ export class AppComponent implements OnInit {
let self = this
// tutorial commands
var m = /^\s*(next|prev|first|last)\s*$/.exec(command)
let multiline = /\s*(describe|control|end)\s*/.exec(command)
if (m) {
// update the instructions widget
// we are not calling this from the ng event loop, therefore we need to
Expand All @@ -169,17 +174,43 @@ export class AppComponent implements OnInit {
this.shellMode = 'sh'
// switch prompt
this.prompt = SH_PROMPT;
this.printOnStdout('');
this.printOnStdout("\n\r");
}
// default commands
else {
else if (this.shellMode == 'inspec' && multiline) {
// count control + describe
let mstart = command.match(/describe|control/g)
if (mstart) {
this.shell_depth += mstart.length
}
// end
let mend = command.match(/end/g)
if (mend) {
this.shell_depth -= mend.length
}

this.shell_command += command + "\n"

if (mend && this.shell_depth == 0) {
command = this.shell_command
this.shell_depth = 0
this.shell_command = ''
this.execCommand(command, this.shellMode);
}
}
// default sh commands
else if ((this.shell_depth == 0) || this.shellMode == 'sh') {
this.execCommand(command, this.shellMode);
}
// store command in cache, must be an inspec shell command
else {
this.shell_command += command + "\n"
}
}

// submit stdout data to terminal
printOnStdout(data){
this.stdout.emit(data)
this.stdout.emit(this.prompt)
}

// load json file for instructions and save to instructionsArray
Expand Down Expand Up @@ -207,6 +238,7 @@ export class AppComponent implements OnInit {
// @see http://stackoverflow.com/questions/3446170/escape-string-for-use-in-javascript-regex
let cmd = commands[i].command.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
cmd = cmd.replace(/ /g,"\\s*")
cmd = cmd.replace(/\n/g,"\\s*")
let pattern = '^\\s*'+cmd+'\\s*$'
commands[i].regex = new RegExp(pattern, 'im')
}
Expand Down
4 changes: 1 addition & 3 deletions www/tutorial/app/xterm-terminal/xterm-terminal.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,12 @@ export class XtermTerminalComponent implements OnInit, OnChanges{
// print out stdout data
onStdout(data) {
let res = data
this.term.writeln(res);
this.writePrompt();
this.term.write(res);
}

// print out error
onStderr(response) {
this.term.writeln(response);
this.writePrompt();
}

// writes the command prompt, a prompt is not part of the input buffer
Expand Down

0 comments on commit 10154c1

Please sign in to comment.