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
59
cli.js
59
cli.js
|
|
@ -1,36 +1,65 @@
|
|||
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';
|
||||
|
||||
async function reader(path){
|
||||
async function reader(path) {
|
||||
try {
|
||||
const contents = await readFile(path, { encoding: 'utf8' });
|
||||
// console.log(contents)
|
||||
return contents;
|
||||
const preFlighCheck = await stat(path);
|
||||
const isUsable = preFlighCheck.isFile() && !preFlighCheck.isDirectory()
|
||||
if (!isUsable)
|
||||
return "";
|
||||
const contents = await readFile(path, { encoding: 'utf8' });
|
||||
return contents;
|
||||
} catch (err) {
|
||||
console.error(err.message);
|
||||
console.error(err.message);
|
||||
}
|
||||
}
|
||||
|
||||
async function writer(path, content){
|
||||
async function writer(path, content) {
|
||||
try {
|
||||
await writeFile(path, content, { encoding: 'utf8' });
|
||||
const preFlighCheck = await stat(path);
|
||||
const isUsable = preFlighCheck.isFile() && !preFlighCheck.isDirectory()
|
||||
if (!isUsable)
|
||||
return;
|
||||
await writeFile(path, content, { encoding: 'utf8' });
|
||||
} catch (err) {
|
||||
console.error(err.message);
|
||||
console.error(err.message);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function convertForConsole(convertFunction){
|
||||
return async function(filePath){
|
||||
const sourceFile = await reader("./systemd-unit.conf");
|
||||
function convertForConsole(convertFunction) {
|
||||
return async function (filePath) {
|
||||
const sourceFile = await reader(filePath);
|
||||
const converted = convertFunction(sourceFile);
|
||||
await writer("./init.conf", converted.result)
|
||||
}
|
||||
}
|
||||
|
||||
async function setup(){
|
||||
async function setup(input, output) {
|
||||
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