Skip to content

Commit

Permalink
feat(ctl): added isActive ctl function
Browse files Browse the repository at this point in the history
  • Loading branch information
chyzwar committed Sep 20, 2024
1 parent 6d24073 commit ec6e923
Showing 1 changed file with 72 additions and 61 deletions.
133 changes: 72 additions & 61 deletions packages/ctl/src/ctl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,76 @@ function getUnit(unitName: string, type: string = getType(unitName)): Unit | und
}
};

/**
* Create a unit file if it does not exist or if it is different from the current unit.
*/
export function create(unitName: string, unit: Unit) {
const name = getName(unitName);
const type = getType(unitName, unit);
const path = getPath (name, type);

const current = getUnit(name, type);

if (unit.equals(current)) {
writeFileSync(path, unit.toINIString());
}
}

export function reload(unitName: string, unit?: Unit) {
const type = getType(unitName, unit);
const name = getName(unitName);

execSync(`systemctl daemon-reload ${name}.${type}`);
}

export function enable(unitName: string, unit?: Unit) {
const type = getType(unitName, unit);
if (type === "container") {
throw new Error("Containers are not supported");
}
const name = getName(unitName);

execSync(`systemctl enable ${name}.${type}`);
}

export function disable(unitName: string, unit?: Unit) {
const type = getType(unitName, unit);
if (type === "container") {
throw new Error("Containers are not supported");
}
const name = getName(unitName);

execSync(`systemctl disable ${name}.${type}`);
}

export function start(unitName: string, unit?: Unit) {
const type = getType(unitName, unit);
const name = getName(unitName);

execSync(`systemctl start ${name}.${type}`);
}

export function stop(unitName: string, unit?: Unit) {
const type = getType(unitName, unit);
const name = getName(unitName);

execSync(`systemctl stop ${name}.${type}`);
}

export function restart(unitName: string, unit?: Unit) {
const type = getType(unitName, unit);
const name = getName(unitName);

execSync(`systemctl restart ${name}.${type}`);
}

export function isActive(unitName: string, unit?: Unit) {
const type = getType(unitName, unit);
const name = getName(unitName);

return execSync(`systemctl is-active ${name}.${type}`).toString().trim() === "active";
}

export class Ctl {
private readonly type: string;
private readonly name: string;
Expand Down Expand Up @@ -116,67 +186,8 @@ export class Ctl {
public reload() {
execSync(`systemctl daemon-reload ${this.name}.${this.type}`);
}
}

/**
* Create a unit file if it does not exist or if it is different from the current unit.
*/
export function create(unitName: string, unit: Unit) {
const name = getName(unitName);
const type = getType(unitName, unit);
const path = getPath (name, type);

const current = getUnit(name, type);

if (unit.equals(current)) {
writeFileSync(path, unit.toINIString());
}
}

export function reload(unitName: string, unit?: Unit) {
const type = getType(unitName, unit);
const name = getName(unitName);

execSync(`systemctl daemon-reload ${name}.${type}`);
}

export function enable(unitName: string, unit?: Unit) {
const type = getType(unitName, unit);
if (type === "container") {
throw new Error("Containers are not supported");
}
const name = getName(unitName);

execSync(`systemctl enable ${name}.${type}`);
}

export function disable(unitName: string, unit?: Unit) {
const type = getType(unitName, unit);
if (type === "container") {
throw new Error("Containers are not supported");
public isActive() {
return isActive(this.name, this.unit);
}
const name = getName(unitName);

execSync(`systemctl disable ${name}.${type}`);
}

export function start(unitName: string, unit?: Unit) {
const type = getType(unitName, unit);
const name = getName(unitName);

execSync(`systemctl start ${name}.${type}`);
}

export function stop(unitName: string, unit?: Unit) {
const type = getType(unitName, unit);
const name = getName(unitName);

execSync(`systemctl stop ${name}.${type}`);
}

export function restart(unitName: string, unit?: Unit) {
const type = getType(unitName, unit);
const name = getName(unitName);

execSync(`systemctl restart ${name}.${type}`);
}

0 comments on commit ec6e923

Please sign in to comment.