inspec/www/tutorial/app/app.component.ts

151 lines
4.4 KiB
TypeScript
Raw Normal View History

2016-08-29 21:12:31 +00:00
import { Component, OnInit } from '@angular/core';
2016-08-24 12:58:40 +00:00
import { Http, Response } from '@angular/http';
import { Observable } from 'rxjs/Rx';
import { HTTP_PROVIDERS } from '@angular/http';
2016-08-29 21:12:31 +00:00
import { XtermTerminalComponent } from './xterm-terminal/xterm-terminal.component';
2016-08-24 12:58:40 +00:00
@Component({
selector: 'my-app',
templateUrl: 'app/app.component.html',
styleUrls: ['app/app.component.css'],
providers: [ HTTP_PROVIDERS ],
2016-08-29 21:12:31 +00:00
directives: [ XtermTerminalComponent ]
2016-08-24 12:58:40 +00:00
})
export class AppComponent implements OnInit {
instructions: any;
instructionsArray: any;
responsesArray: any;
counter: number = 0;
response: string;
shell: string;
2016-08-24 12:58:40 +00:00
constructor(private http: Http) { }
ngOnInit() {
this.getInstructions();
this.getResponses();
}
2016-08-29 21:12:31 +00:00
updateInstructions(step) {
let totalSteps = this.instructionsArray.length - 1;
2016-09-02 19:13:07 +00:00
let msg = Math.random();
2016-09-01 15:19:04 +00:00
if (step === 'next') {
2016-09-02 19:13:07 +00:00
if (this.counter < totalSteps) {
2016-09-01 15:19:04 +00:00
this.counter += 1;
}
2016-09-02 19:13:07 +00:00
this.response = ' Message: ' + msg;
2016-09-01 15:19:04 +00:00
} else if (step === 'prev') {
2016-09-02 19:13:07 +00:00
if (this.counter > 0) {
this.counter -= 1;
2016-09-01 15:19:04 +00:00
}
2016-09-02 19:13:07 +00:00
this.response = ' Message: ' + msg;
}
this.instructions = this.instructionsArray[this.counter]['_body'];
}
evalCommand(command) {
if (this.shell === 'inspec-shell') {
this.parseInspecShell(command);
}
2016-09-02 19:13:07 +00:00
else if (command.match(/^inspec\s*.*/)) {
this.parseInspecCommand(command);
}
else if (command.match(/^next\s*/)) {
this.updateInstructions('next');
}
else if (command.match(/^prev\s*/)) {
this.updateInstructions('prev');
}
else if (command.match(/^ls\s*/)) {
this.response = " " + this.responsesArray[1]['_body'];
}
else if (command.match(/^pwd\s*/)) {
this.response = " " + this.responsesArray[2]['_body'];
}
else if (command.match(/^cat\s*README.md\s*/i)) {
this.response = " Only a few commands are implemented in this terminal. Please follow the demo";
}
else if (command.match(/^less\s*README.md\s*/i)) {
this.response = " Only a few commands are implemented in this terminal. Please follow the demo.";
}
else {
2016-09-02 19:13:07 +00:00
this.response = " invalid command: " + command;
}
}
parseInspecCommand(command) {
let command_target = command.match(/^inspec\s*(.*)/);
if (command_target[1] === 'shell') {
this.shell = 'inspec-shell'
this.response = " " + 'Welcome to the InSpec Shell\r\n To find out how to use it, type: help\r\n To leave, type: exit';
}
else if (command_target[1].match(/^exec\s*.*/)) {
this.parseInspecExec(command);
}
else if (command_target[1].match(/^version\s*/)) {
this.response = " " + this.responsesArray[4]['_body'];
}
else {
let msg = Math.random();
this.response = " " + this.responsesArray[0]['_body'] + " " + msg;
}
}
parseInspecExec(command) {
let target = command.match(/^inspec exec\s*(.*)/);
if (target[1] === 'examples/profile') {
2016-09-02 19:13:07 +00:00
this.response = " " + this.responsesArray[3]['_body'];
} else {
2016-09-02 19:13:07 +00:00
this.response = "  Could not fetch inspec profile in '" + target[1] + "' ";
}
}
parseInspecShell(command) {
// exit inspec shell
if (command.match(/^exit\s*/)) {
this.shell = '';
this.response = '';
}
else if (command.match(/^ls\s*/)) {
2016-09-02 19:13:07 +00:00
this.response = " " + this.responsesArray[5]['_body'];
}
// TODO: functionality for inspec Shell
else {
2016-09-02 19:13:07 +00:00
this.response = " " + 'soon this will work, but not yet :) '
2016-08-29 21:12:31 +00:00
}
2016-08-24 12:58:40 +00:00
}
getInstructions() {
Observable.forkJoin(
this.http.get('/app/instructions/step0.txt'),
this.http.get('/app/instructions/step1.txt'),
this.http.get('/app/instructions/step2.txt')
).subscribe(
data => {
this.instructionsArray = data;
2016-08-29 21:12:31 +00:00
this.updateInstructions(0);
2016-08-24 12:58:40 +00:00
},
err => console.error(err)
);
}
getResponses() {
Observable.forkJoin(
this.http.get('/app/responses/help.txt'), // 0
this.http.get('/app/responses/ls.txt'), // 1
this.http.get('/app/responses/pwd.txt'), // 2
this.http.get('/app/responses/inspec_exec.txt'), // 3
this.http.get('/app/responses/inspec_version.txt'), // 4
this.http.get('/app/responses/pwd_inspec_shell.txt') // 5
2016-08-24 12:58:40 +00:00
).subscribe(
data => {
this.responsesArray = data;
},
err => console.error(err)
);
}
}