Skip to content

Commit 95a4840

Browse files
committed
lib.ts: Fix prompt for interactive tty
1 parent 1955b7e commit 95a4840

File tree

2 files changed

+34
-7
lines changed

2 files changed

+34
-7
lines changed

cli/lib.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
// SPDX-FileCopyrightText: Tobias Knöppler <[email protected]>
22
// SPDX-License-Identifier: AGPL-3.0-or-later
33

4-
import readline from 'node:readline'
4+
//import readline from 'node:readline'
55
import process from 'process'
66

7+
// readline breaks in noninteractive environments, so we're building our own prompt function
8+
//
79
// const rl = readline.createInterface({
810
// input: process.stdin,
911
// output: process.stdout,
@@ -24,17 +26,31 @@ import process from 'process'
2426
// }
2527
// )
2628

29+
let promptBuf = ""
30+
2731
export async function prompt(query: string){
2832

2933
process.stderr.write(query)
3034

31-
let data = ""
35+
let inputs = promptBuf.split(/\r?\n/)
36+
if (inputs.length > 1) {
37+
let data = inputs.shift() as string;
38+
promptBuf = inputs.join("\n")
39+
return data
40+
}
41+
42+
let data = promptBuf
3243
for await (const chunk of process.stdin) {
44+
let inputs = ("" + chunk).split(/\r?\n/)
45+
if (inputs.length > 1) {
46+
data += inputs.shift() as string
47+
promptBuf = inputs.join("\n")
48+
break;
49+
}
3350
data += chunk;
3451
}
3552

36-
3753
// process all the data and write it back to stdout
3854

39-
return data.replace(/\n$/, "")
55+
return data.replace(/\r?\n$/, "")
4056
}

cli/secrets.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { CommandExecutionError } from './CommandExecutionError.ts'
1111
import { prompt } from './lib.ts'
1212
import process from "process";
1313
import * as url from "node:url";
14+
import * as https from "node:https";
1415

1516
const btoa = (str: string) => Buffer.from(str, 'binary').toString('base64')
1617
const atob = (str: string) => Buffer.from(str, 'base64').toString('binary')
@@ -91,7 +92,7 @@ export async function createSecret(ncUrl: string, ncUser: string, secretFile: st
9192

9293
const result = await new Promise<string>((resolve, reject) => {
9394

94-
const req = http.request(
95+
const req = http_client(ncUrl).request(
9596
`${ncUrl}/ocs/v2.php/apps/secrets/api/v1/secrets?format=json`,
9697
rOptions,
9798
(response) => {
@@ -201,7 +202,7 @@ export async function retrieveSecret(shareUrlStr: string, options: {
201202
}
202203

203204
const result = await new Promise<string>((resolve, reject) => {
204-
const req = http.request(
205+
const req = http_client(shareUrlStr).request(
205206
`${baseUrl}/ocs/v2.php/apps/secrets/api/v1/share?format=json`,
206207
reqOptions,
207208
(response) => {
@@ -238,13 +239,23 @@ export async function retrieveSecret(shareUrlStr: string, options: {
238239

239240
}
240241

242+
function http_client(url: string): any {
243+
244+
let http_client = http
245+
if (url.startsWith("https:")) {
246+
http_client = https
247+
}
248+
249+
return http_client
250+
}
251+
241252
/**
242253
* @param ncUrl
243254
*/
244255
async function getApiInfo(ncUrl: string): Promise<string> {
245256

246257
const result = await new Promise<string>((resolve, reject) => {
247-
const req = http.request(`${ncUrl}/ocs/v2.php/apps/secrets/version?format=json`,
258+
const req = http_client(ncUrl).request(`${ncUrl}/ocs/v2.php/apps/secrets/version?format=json`,
248259
{
249260
method: 'GET',
250261
headers: {

0 commit comments

Comments
 (0)