This repository has been archived by the owner on Sep 20, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
credits, dl, shutdown,ping fixed some missing type warnings added check for UUID args
- Loading branch information
Showing
10 changed files
with
189 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,64 @@ | ||
import { SafeHtml } from '@angular/platform-browser'; | ||
import {Device} from 'src/app/api/devices/device'; | ||
|
||
export interface TerminalAPI { | ||
/** | ||
* Outputs html to the terminal (followed by a line break) | ||
* @param html HTML string | ||
*/ | ||
output(html: string); | ||
output(html: string): void; | ||
|
||
/** | ||
* Outputs html without a line break to the terminal | ||
* @param html HTML string | ||
*/ | ||
outputRaw(html: string); | ||
outputRaw(html: string): void; | ||
|
||
/** | ||
* Outputs text to the terminal | ||
* @param text Raw text | ||
*/ | ||
outputText(text: string); | ||
outputText(text: string): void; | ||
|
||
/** | ||
* Outputs a html node to the terminal | ||
* @param node `Node` | ||
*/ | ||
outputNode(node: Node); | ||
outputNode(node: Node): void; | ||
|
||
/** | ||
* Closes the terminal window | ||
*/ | ||
closeTerminal(); | ||
closeTerminal(): void; | ||
|
||
/** | ||
* Clears the complete terminal | ||
*/ | ||
clear(); | ||
clear(): void; | ||
|
||
changePrompt(prompt: string | SafeHtml, trust?: boolean); | ||
changePrompt(prompt: string | SafeHtml, trust?: boolean): void; | ||
|
||
pushState(state: TerminalState); | ||
pushState(state: TerminalState): void; | ||
|
||
popState(): TerminalState; | ||
|
||
/** | ||
* Shutdowns the current device | ||
* | ||
* @returns: if the shutdown was successful | ||
*/ | ||
shutdown(): Promise<boolean>; | ||
|
||
getOwnerDevice(): Device; | ||
} | ||
|
||
|
||
export interface TerminalState { | ||
execute(command: string); | ||
execute(command: string): void; | ||
|
||
autocomplete(content: string): Promise<string>; | ||
|
||
getHistory(): string[]; | ||
|
||
refreshPrompt(); | ||
refreshPrompt(): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import {Command, IOHandler} from '../command'; | ||
import {ShellApi} from '../shellapi'; | ||
|
||
export class Credits extends Command { | ||
constructor(shellApi: ShellApi) { | ||
super('credits', shellApi); | ||
this.addDescription('list all contributors'); | ||
} | ||
|
||
async run(iohandler: IOHandler): Promise<number> { | ||
const data = await fetch('https://api.admin.staging.cryptic-game.net/website/team'); | ||
const members = JSON.parse(await data.text()).sort(() => Math.random() - 0.5); | ||
members.forEach((member: any) => { | ||
iohandler.stdout(member.name); | ||
}); | ||
return 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import {Command, IOHandler, ArgType} from '../command'; | ||
import {ShellApi} from '../shellapi'; | ||
import {Path} from 'src/app/api/files/path'; | ||
import {File} from 'src/app/api/files/file'; | ||
|
||
export class Dl extends Command { | ||
constructor(shellApi: ShellApi) { | ||
super('dl', shellApi); | ||
this.addDescription('download a file to your own device'); | ||
this.addPositionalArgument({name: 'source', argType: ArgType.FILE}); | ||
this.addPositionalArgument({name: 'destination'}); | ||
} | ||
|
||
async run(iohandler: IOHandler): Promise<number> { | ||
let srcFile: File; | ||
let dstPath: Path; | ||
const ownerUuid = this.shellApi.terminal.getOwnerDevice()['uuid']; | ||
try { | ||
const srcPath = Path.fromString(iohandler.positionalArgs[0], this.shellApi.working_dir); | ||
srcFile = await this.shellApi.fileService.getFromPath(this.shellApi.activeDevice['uuid'], srcPath).toPromise(); | ||
} catch { | ||
iohandler.stderr('The source file was not found'); | ||
return 1; | ||
} | ||
if (srcFile.is_directory) { | ||
iohandler.stderr('Cannot download a directory'); | ||
return 1; | ||
} | ||
try { | ||
dstPath = Path.fromString(iohandler.positionalArgs[1], this.shellApi.working_dir); | ||
} catch { | ||
iohandler.stderr('The specified destination path is not valid'); | ||
return 1; | ||
} | ||
|
||
try { | ||
await this.shellApi.fileService.getFromPath(ownerUuid, dstPath).toPromise(); | ||
iohandler.stderr('That file already exists'); | ||
return 1; | ||
} catch {} | ||
|
||
const dstFileName = dstPath.path[dstPath.path.length - 1]; | ||
try { | ||
await this.shellApi.fileService.createFile(ownerUuid, dstFileName, srcFile.content, dstPath.parentUUID).toPromise(); | ||
return 0; | ||
} catch { | ||
iohandler.stderr('Could not create file'); | ||
return 1; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import {Command, IOHandler, ArgType} from '../command'; | ||
import {ShellApi} from '../shellapi'; | ||
|
||
export class Ping extends Command { | ||
constructor(shellApi: ShellApi) { | ||
super('ping', shellApi); | ||
this.addDescription('ping a device'); | ||
this.addPositionalArgument({name: 'uuid', argType: ArgType.UUID}); | ||
} | ||
|
||
async run(iohandler: IOHandler): Promise<number> { | ||
const uuid = iohandler.positionalArgs[0]; | ||
try { | ||
const status = await this.shellApi.deviceService.getDeviceState(uuid).toPromise(); | ||
iohandler.stdout(`Device is ${status.online ? '' : 'not '}online`); | ||
return 0; | ||
} catch { | ||
iohandler.stderr('Device not found'); | ||
return 1; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import {Command, IOHandler} from '../command'; | ||
import {ShellApi} from '../shellapi'; | ||
|
||
export class Shutdown extends Command { | ||
constructor(shellApi: ShellApi) { | ||
super('shutdown', shellApi); | ||
this.addDescription('shutdown your own device'); | ||
} | ||
|
||
async run(_: IOHandler): Promise<number> { | ||
return await this.shellApi.terminal.shutdown() ? 0 : 1; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters