feat: more stuff
This commit is contained in:
parent
0a1b1517df
commit
f66e46850f
4 changed files with 69 additions and 7 deletions
12
src/components/network.ts
Normal file
12
src/components/network.ts
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
import { DefinitionsNetwork } from "../compose-schema";
|
||||||
|
|
||||||
|
export class Network {
|
||||||
|
constructor(
|
||||||
|
public readonly name: string,
|
||||||
|
private options: DefinitionsNetwork
|
||||||
|
) {}
|
||||||
|
|
||||||
|
toCompose(): DefinitionsNetwork {
|
||||||
|
return this.options;
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,5 +1,12 @@
|
||||||
import { DefinitionsService } from "../compose-schema.ts";
|
import { DefinitionsService } from "../compose-schema.ts";
|
||||||
|
|
||||||
export class Service {
|
export class Service {
|
||||||
constructor(public name: string, public options: DefinitionsService) {}
|
constructor(
|
||||||
|
public readonly name: string,
|
||||||
|
private options: DefinitionsService
|
||||||
|
) {}
|
||||||
|
|
||||||
|
toCompose(): DefinitionsService {
|
||||||
|
return this.options;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,26 @@
|
||||||
import { ComposeSpecification, DefinitionsService } from "../compose-schema.ts";
|
import {
|
||||||
|
ComposeSpecification,
|
||||||
|
DefinitionsNetwork,
|
||||||
|
DefinitionsService,
|
||||||
|
DefinitionsVolume,
|
||||||
|
} from "../compose-schema.ts";
|
||||||
import { registerDeployment } from "../deployment.ts";
|
import { registerDeployment } from "../deployment.ts";
|
||||||
|
import { Network } from "./network.ts";
|
||||||
import { Service } from "./service.ts";
|
import { Service } from "./service.ts";
|
||||||
|
import { Volume } from "./volume.ts";
|
||||||
|
|
||||||
|
function mapRecordToCompose<T, F extends { toCompose(): T }>(
|
||||||
|
obj: Record<string, F>
|
||||||
|
): Record<string, T> {
|
||||||
|
return Object.fromEntries(
|
||||||
|
Object.entries(obj).map(([k, v]) => [k, v.toCompose()])
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
export class Stack {
|
export class Stack {
|
||||||
private services: Record<string, Service> = {};
|
private services: Record<string, Service> = {};
|
||||||
|
private networks: Record<string, Network> = {};
|
||||||
|
private volumes: Record<string, Volume> = {};
|
||||||
|
|
||||||
constructor(public name: string) {
|
constructor(public name: string) {
|
||||||
registerDeployment(this);
|
registerDeployment(this);
|
||||||
|
@ -18,14 +35,28 @@ export class Stack {
|
||||||
return this.services[name];
|
return this.services[name];
|
||||||
}
|
}
|
||||||
|
|
||||||
public toComposeFile(): ComposeSpecification {
|
public addNetwork(name: string, network: DefinitionsNetwork): Network {
|
||||||
const services: Record<string, DefinitionsService> = {};
|
if (this.networks[name]) {
|
||||||
for (const [name, service] of Object.entries(this.services)) {
|
throw new Error(`Network ${name} already defined.`);
|
||||||
services[name] = service.options;
|
|
||||||
}
|
}
|
||||||
|
this.networks[name] = new Network(name, network);
|
||||||
|
return this.networks[name];
|
||||||
|
}
|
||||||
|
|
||||||
|
public addVolume(name: string, volume: DefinitionsVolume): Volume {
|
||||||
|
if (this.volumes[name]) {
|
||||||
|
throw new Error(`Volume ${name} already defined.`);
|
||||||
|
}
|
||||||
|
this.volumes[name] = new Volume(name, volume);
|
||||||
|
return this.volumes[name];
|
||||||
|
}
|
||||||
|
|
||||||
|
public toComposeFile(): ComposeSpecification {
|
||||||
return {
|
return {
|
||||||
version: "3.8",
|
version: "3.8",
|
||||||
services,
|
services: mapRecordToCompose(this.services),
|
||||||
|
networks: mapRecordToCompose(this.networks),
|
||||||
|
volumes: mapRecordToCompose(this.volumes),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
12
src/components/volume.ts
Normal file
12
src/components/volume.ts
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
import { DefinitionsVolume } from "../compose-schema";
|
||||||
|
|
||||||
|
export class Volume {
|
||||||
|
constructor(
|
||||||
|
public readonly name: string,
|
||||||
|
private options: DefinitionsVolume
|
||||||
|
) {}
|
||||||
|
|
||||||
|
toCompose(): DefinitionsVolume {
|
||||||
|
return this.options;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue