deployer: add settings

master
Yorick van Pelt 2023-04-23 12:50:31 +02:00
parent 036c5a7488
commit 9130e4602a
Signed by: yorick
GPG Key ID: A36E70F9DC014A15
1 changed files with 19 additions and 3 deletions

View File

@ -1,14 +1,18 @@
import { ssh, SSH } from './ssh.js'
import { Expression } from './nix.js'
type Dictionary<T> = Record<string, T>
type Command = () => Promise<void>
type Setter = (props: Dictionary<string | boolean | number>) => void
class Cmd {
registry: Record<string, Command> = {};
registry: Dictionary<Command> = {};
settings: Dictionary<Setter> = {};
register(obj: string, action: string, fn: Command) {
this.registry[`${obj}.${action}`] = fn
}
registerAll(obj: string, intf: Object & { _commands?: string[] }) {
registerAll(obj: string, intf: Object & { _commands?: string[], set?: Setter }) {
if (!intf._commands) return
if (intf.set) this.settings[obj] = intf.set.bind(intf)
const proto = Object.getPrototypeOf(intf)
for (const name of intf._commands) {
if (name != "constructor" && typeof proto[name] === "function") {
@ -18,6 +22,14 @@ class Cmd {
}
async run() {
const opt = argv._[0]
for (const k of Object.keys(argv)) {
if (k === "_") continue
if (this.settings[k]) {
this.settings[k](argv[k])
} else {
throw new Error("unknown object " + k)
}
}
if (opt == "__autocompletes") {
for (const k of Object.keys(this.registry)) {
console.log(k)
@ -62,7 +74,7 @@ class Machine {
return this.hostname
} else {
// todo: directify
return `${this.name}.vpn.yori.cc`
return `${this.name}.home.yori.cc`
}
}
async findIP(): Promise<typeof Machine.prototype.ssh> {
@ -106,6 +118,10 @@ class MachineInterface {
// hack:
delete this._commands
}
set(props: Dictionary<string | boolean | number>) {
console.log("setting", props)
Object.assign(this.machine, props)
}
@cmd
async ssh() {
(await this.machine.ssh()).interactive()