mirror of
https://github.com/lennyhans/systemd-to-openrc.git
synced 2026-03-10 04:41:09 +00:00
Add cli input handler
Also check for directories Also add a small help
This commit is contained in:
parent
28fa44e17c
commit
e46a47b54f
1 changed files with 44 additions and 15 deletions
41
cli.js
41
cli.js
|
|
@ -1,10 +1,16 @@
|
||||||
import { readFile, writeFile } from 'node:fs/promises';
|
#!/usr/bin/env node
|
||||||
|
|
||||||
|
import { readFile, writeFile, stat } from 'node:fs/promises';
|
||||||
|
import { argv, exit } from 'node:process';
|
||||||
import { convert } from './magic.js';
|
import { convert } from './magic.js';
|
||||||
|
|
||||||
async function reader(path) {
|
async function reader(path) {
|
||||||
try {
|
try {
|
||||||
|
const preFlighCheck = await stat(path);
|
||||||
|
const isUsable = preFlighCheck.isFile() && !preFlighCheck.isDirectory()
|
||||||
|
if (!isUsable)
|
||||||
|
return "";
|
||||||
const contents = await readFile(path, { encoding: 'utf8' });
|
const contents = await readFile(path, { encoding: 'utf8' });
|
||||||
// console.log(contents)
|
|
||||||
return contents;
|
return contents;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err.message);
|
console.error(err.message);
|
||||||
|
|
@ -13,6 +19,10 @@ async function reader(path){
|
||||||
|
|
||||||
async function writer(path, content) {
|
async function writer(path, content) {
|
||||||
try {
|
try {
|
||||||
|
const preFlighCheck = await stat(path);
|
||||||
|
const isUsable = preFlighCheck.isFile() && !preFlighCheck.isDirectory()
|
||||||
|
if (!isUsable)
|
||||||
|
return;
|
||||||
await writeFile(path, content, { encoding: 'utf8' });
|
await writeFile(path, content, { encoding: 'utf8' });
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err.message);
|
console.error(err.message);
|
||||||
|
|
@ -22,15 +32,34 @@ async function writer(path, content){
|
||||||
|
|
||||||
function convertForConsole(convertFunction) {
|
function convertForConsole(convertFunction) {
|
||||||
return async function (filePath) {
|
return async function (filePath) {
|
||||||
const sourceFile = await reader("./systemd-unit.conf");
|
const sourceFile = await reader(filePath);
|
||||||
const converted = convertFunction(sourceFile);
|
const converted = convertFunction(sourceFile);
|
||||||
await writer("./init.conf", converted.result)
|
await writer("./init.conf", converted.result)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function setup(){
|
async function setup(input, output) {
|
||||||
const convertFunction = convertForConsole(convert)
|
const convertFunction = convertForConsole(convert)
|
||||||
convertFunction();
|
convertFunction(input);
|
||||||
}
|
}
|
||||||
|
|
||||||
setup();
|
const [, , input, output] = argv;
|
||||||
|
|
||||||
|
// print process.argv
|
||||||
|
// argv.forEach((val, index) => {
|
||||||
|
// console.log(`${index}: ${val}`);
|
||||||
|
// });
|
||||||
|
// exit(0);
|
||||||
|
if (input === "-h" || input === '--help' || argv.length < 3) {
|
||||||
|
console.info(`
|
||||||
|
Convert systemd service unit to openrc
|
||||||
|
|
||||||
|
usage:
|
||||||
|
cli.js <input-systemd-unit> <output-init-file>
|
||||||
|
|
||||||
|
example:
|
||||||
|
cli.js systemd.unit init.conf
|
||||||
|
`)
|
||||||
|
exit();
|
||||||
|
}
|
||||||
|
setup(input, output);
|
||||||
Loading…
Add table
Add a link
Reference in a new issue