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