kwallet and nocodb

This commit is contained in:
Jan Andrle 2024-03-21 10:35:30 +01:00
parent 4f578e0bb0
commit 9737f6cdab
Signed by: jaandrle
GPG Key ID: B3A25AED155AFFAB
2 changed files with 34 additions and 18 deletions

View File

@ -1,5 +1,6 @@
#!/usr/bin/env nodejsscript #!/usr/bin/env nodejsscript
/* jshint esversion: 11,-W097, -W040, module: true, node: true, expr: true, undef: true *//* global echo, $, pipe, s, fetch, cyclicLoop */ /* jshint esversion: 11,-W097, -W040, module: true, node: true, expr: true, undef: true *//* global echo, $, pipe, s, fetch, cyclicLoop */
import { kwallet } from "./§kwallet.mjs";
const url_api= "https://nocodb.jaandrle.cz/api/v1/db/data/v1/Linky/Linky"; const url_api= "https://nocodb.jaandrle.cz/api/v1/db/data/v1/Linky/Linky";
$.api() $.api()
.command("add-md") .command("add-md")
@ -9,7 +10,7 @@ $.api()
const res= await fetch(url_api, { const res= await fetch(url_api, {
body: JSON.stringify({ Popis: title.slice(1), Url: url.slice(0, -1) }), body: JSON.stringify({ Popis: title.slice(1), Url: url.slice(0, -1) }),
headers: { "xc-token": "Js7Qu0oT_wTmGJDFKlIZEBhwk87WF0L1wLmQO01F", headers: { "xc-token": kwallet([ "readPassword", "Passwords", "nocodb-linky" ])[0].toString(),
"accept": "application/json", "Content-Type": "application/json" "accept": "application/json", "Content-Type": "application/json"
}, },
method: "post" method: "post"

View File

@ -6,6 +6,7 @@ const css= echo.css`
.code::before, .code::after{ content: "\`"; } .code::before, .code::after{ content: "\`"; }
.li{ padding-left: 2ch; } .li{ padding-left: 2ch; }
`; `;
const default_wallet= "kdewallet";
const cmd= "qdbus"; const cmd= "qdbus";
if(!s.which(cmd)) if(!s.which(cmd))
$.exit(1, echo([ $.exit(1, echo([
@ -16,18 +17,35 @@ if(!s.which(cmd))
import { EventEmitter } from "node:events"; import { EventEmitter } from "node:events";
const events= new EventEmitter(); const events= new EventEmitter();
const exit_event= "exit"; const exit_event= "exit";
events.on(exit_event, $.exit);
$.api("", true) if($.isMain(import.meta)){
.version("2024-03-05") events.on(exit_event, $.exit);
.describe([
$.api("", true)
.version("2024-03-05")
.describe([
`KWallet CLI using ${cmd}.`, `KWallet CLI using ${cmd}.`,
"Call with no arguments to list all methods.", "Call with no arguments to list all methods.",
]) ])
.example("--wallet kdewallet folderList") .example("--wallet kdewallet folderList")
.example("--wallet kdewallet readPassword folder entry") .example("--wallet kdewallet readPassword folder entry")
.option("--wallet, -w", "Wallet name", "kdewallet") .option("--wallet, -w", "Wallet name", default_wallet)
.action(function({ _, wallet }){ .action(function main({ _, wallet }){
const [ results, is_ls ]= kwallet(_, { wallet });
if(is_ls) echoLs(results);
else echo(results);
events.emit(exit_event);
})
.parse();
}
/**
* @param {string[]} _ Query/arguments for qdbus
* @param {object} [options]
* @param {string} [options.wallet] Wallet name
* @returns {[ string[], true ]|[ string, false ]}
* */
export function kwallet(_, { wallet= default_wallet }= {}){
const qdbus= qdbusGenerator({ const qdbus= qdbusGenerator({
service: "org.kde.kwalletd6", service: "org.kde.kwalletd6",
methods: "/modules/kwalletd6", methods: "/modules/kwalletd6",
@ -35,16 +53,13 @@ $.api("", true)
exit_event exit_event
}); });
if(!_.length) if(!_.length)
echoLs(qdbus()); return [ qdbus(), true ];
else{ else{
//TODO: Map (`--literal` in qdbus and decode the result `echoMap`) //TODO: Map (`--literal` in qdbus and decode the result `echoMap`)
const result= qdbus(_); const result= qdbus(_);
echo(result); return [ result, false ];
} }
events.emit(exit_event); }
})
.parse();
function qdbusGenerator({ service, methods, wallet, exit_event }){ function qdbusGenerator({ service, methods, wallet, exit_event }){
const name= getScriptName(); const name= getScriptName();
const no_id= [ const no_id= [