Skip to content

Commit

Permalink
feat(conf): added abstract unit class to handle common methods
Browse files Browse the repository at this point in the history
  • Loading branch information
chyzwar committed Sep 20, 2024
1 parent da31f32 commit 7f26ee9
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 9 deletions.
7 changes: 4 additions & 3 deletions packages/conf/src/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { UnitSectionBuilder, UnitSectionSchema } from "./unit.js";
import { implement } from "./utils.js";
import { INI } from "./ini.js";
import { ServiceSectionBuilder, ServiceSectionSchema, type ServiceSection } from "./service.js";
import type { AbstractUnit, Unit } from "./types.js";

/**
* @see https://docs.podman.io/en/latest/markdown/podman-systemd.unit.5.html
Expand Down Expand Up @@ -59,7 +60,7 @@ export class ContainerSectionBuilder {
}
}

export class Container {
export class Container implements AbstractUnit {
private readonly unitSection: UnitSectionBuilder;
private readonly containerSection: ContainerSectionBuilder;
private readonly installSection: InstallSectionBuilder;
Expand Down Expand Up @@ -169,7 +170,7 @@ export class Container {
/**
* Compare current container with another container
*/
public equals(container: Container) {
return JSON.stringify(this.toObject()) === JSON.stringify(container.toObject());
public equals(container?: Unit) {
return JSON.stringify(this.toObject()) === JSON.stringify(container?.toObject());
}
}
7 changes: 4 additions & 3 deletions packages/conf/src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type { KillSectionConfig } from "./kill.js";
import { KillSectionBuilder, KillSectionSchema } from "./kill.js";
import type { ResourceSectionConfig } from "./resource.js";
import { ResourceSectionBuilder, ResourceSectionConfigSchema } from "./resource.js";
import type { AbstractUnit, Unit } from "./types.js";

/**
* @see https://manpages.ubuntu.com/manpages/noble/en/man5/systemd.service.5.html
Expand Down Expand Up @@ -1602,7 +1603,7 @@ applyMixins(ServiceSectionBuilder, [
ResourceSectionBuilder,
]);

export class Service {
export class Service implements AbstractUnit {
private readonly unitSection: UnitSectionBuilder;
private readonly serviceSection: ServiceSectionBuilder;
private readonly installSection: InstallSectionBuilder;
Expand Down Expand Up @@ -1694,7 +1695,7 @@ export class Service {
/**
* Compare current service with another service
*/
public equals(service: Service) {
return JSON.stringify(this.toObject()) === JSON.stringify(service.toObject());
public equals(service?: Unit) {
return JSON.stringify(this.toObject()) === JSON.stringify(service?.toObject());
}
}
7 changes: 4 additions & 3 deletions packages/conf/src/timer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { InstallSectionBuilder, InstallSectionSchema } from "./install.js";
import type { UnitSection } from "./unit.js";
import { UnitSectionBuilder, UnitSectionSchema } from "./unit.js";
import { INI } from "./ini.js";
import type { AbstractUnit, Unit } from "./types.js";

/**
* Timer section of a systemd unit file.
Expand Down Expand Up @@ -508,7 +509,7 @@ applyMixins(TimerSectionBuilder, [
ExecSectionBuilder,
]);

export class Timer {
export class Timer implements AbstractUnit {
private readonly unitSection: UnitSectionBuilder;
private readonly timerSection: TimerSectionBuilder;
private readonly installSection: InstallSectionBuilder;
Expand Down Expand Up @@ -606,7 +607,7 @@ export class Timer {
/**
* Compare current timer with another timer
*/
public equals(timer: Timer) {
return JSON.stringify(this.toObject()) === JSON.stringify(timer.toObject());
public equals(timer?: Unit) {
return JSON.stringify(this.toObject()) === JSON.stringify(timer?.toObject());
}
}
6 changes: 6 additions & 0 deletions packages/conf/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,10 @@ import type { Container } from "./container.js";
import type { Service } from "./service.js";
import type { Timer } from "./timer.js";

export abstract class AbstractUnit {
public abstract toObject(): object;
public abstract toINIString(): string;
public abstract equals(unit?: Unit): boolean;
}

export type Unit = Container | Service | Timer;

0 comments on commit 7f26ee9

Please sign in to comment.