From 378b052412e22f112c72e5adb79f89e7db316ecd Mon Sep 17 00:00:00 2001 From: "Felix W. Dekker" Date: Sat, 26 Oct 2019 16:08:46 +0200 Subject: [PATCH] Add whoami Fixes #7 --- js/commands.ts | 14 ++++++++++++++ js/terminal.ts | 49 +++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 53 insertions(+), 10 deletions(-) diff --git a/js/commands.ts b/js/commands.ts index 81d5d1c..309a38e 100644 --- a/js/commands.ts +++ b/js/commands.ts @@ -129,6 +129,12 @@ export class Commands { `Update the access and modification times of each FILE to the current time. If a file does not exist, it is created.`.trimLines() + ), + whoami: new Command( + this.whoami, + `print short description of user`, + `whoami`, + `Print a description of the user associated with the current effective user ID.` ) }; } @@ -276,6 +282,14 @@ export class Commands { private touch(args: InputArgs): string { return this.fileSystem.createFiles(args.args); } + + private whoami(): string { + const user = this.terminal.currentUser; + if (user === undefined) + throw "Cannot execute `whoami` while not logged in."; + + return user.description; + } } diff --git a/js/terminal.ts b/js/terminal.ts index 77752d3..5d303a0 100644 --- a/js/terminal.ts +++ b/js/terminal.ts @@ -13,7 +13,8 @@ export class Terminal { private readonly fileSystem: FileSystem; private readonly commands: Commands; - private _currentUser: string | undefined; + private readonly users: User[]; + private _currentUser: User | undefined; private isLoggedIn: boolean; @@ -23,7 +24,13 @@ export class Terminal { this.output = output; this.prefixDiv = prefixDiv; - this._currentUser = "felix"; + this.users = [ + new User("felix", "password", "Why are you logged in on my account?"), + new User("root", "root", "Wait how did you get here?") + ]; + this._currentUser = this.users.find(it => it.name === "felix"); + if (this._currentUser === undefined) + throw "Could not find user `felix`."; this.isLoggedIn = true; this.inputHistory = new InputHistory(); @@ -64,7 +71,7 @@ export class Terminal { this.prefixDiv.innerHTML = prefixText; } - get currentUser(): string | undefined { + get currentUser(): User | undefined { return this._currentUser; } @@ -87,10 +94,13 @@ export class Terminal { if (this._currentUser === undefined) return "login as: "; else - return `Password for ${this._currentUser}@fwdekker.com: `; + return `Password for ${this._currentUser.name}@fwdekker.com: `; } - return `${this._currentUser}@fwdekker.com ${this.fileSystem.pwd}> `; + if (this._currentUser === undefined) + throw "User is logged in as undefined."; + + return `${this._currentUser.name}@fwdekker.com ${this.fileSystem.pwd}> `; } @@ -107,16 +117,22 @@ export class Terminal { private continueLogin(input: string) { - if (this._currentUser === undefined) { - this.outputText += `${this.prefixText}${input}\n`; + if (this.isLoggedIn) + throw "`continueLogin` is called while user is already logged in."; + + const user = this._currentUser; + if (user === undefined) { + this.outputText += `${this.prefixText}${input.trim()}\n`; + + this._currentUser = this.users.find(it => it.name === input.trim()); + if (this._currentUser === undefined) + this._currentUser = new User(input.trim(), "temp", "temp"); - this._currentUser = input.trim(); this.input.classList.add("terminalCurrentFocusInputHidden"); } else { this.outputText += `${this.prefixText}\n`; - if ((this._currentUser === "felix" && input === "password") - || (this._currentUser === "root" && input === "root")) { + if (this.users.find(it => it.name === user.name) && input === user.password) { this.isLoggedIn = true; this.outputText += Terminal.generateHeader(); } else { @@ -242,6 +258,19 @@ class InputHistory { } } +class User { + readonly name: string; + readonly password: string; + readonly description: string; + + + constructor(name: string, password: string, description: string) { + this.name = name; + this.password = password; + this.description = description; + } +} + export let terminal: Terminal;