Page Menu
Home
Sealhub
Search
Configure Global Search
Log In
Files
F10360142
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
6 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/bin/index.ts b/bin/index.ts
index e511aa8..9ddd23f 100644
--- a/bin/index.ts
+++ b/bin/index.ts
@@ -1,35 +1,14 @@
#!/usr/bin/env ts-node
import initConfig from "../lib/init";
-import { Context } from "../lib/classes/context";
-import { StdinQueryKeyStrategy } from "../lib/classes/stdin_query";
-import { StdinJsonStrategy } from "../lib/classes/stdin_json";
-import parseJSON from "../lib/helpers/parse_json";
-
-// TODO(itsromek): Create interface for possible fields for Conduit API
-async function fetchData(data: string) {
- const parsed = await parseJSON(data);
- const context = new Context();
- if (!parsed) {
- context.setStrategy(new StdinQueryKeyStrategy(data));
- } else {
- context.setStrategy(new StdinJsonStrategy(parsed));
- }
- return await context.execute();
-}
+import parseDataFromStdin from "../lib/parse_data_stdin";
(async function main() {
if (!process.stdin.isTTY) {
- let data = "";
- process.stdin.resume();
- process.stdin.setEncoding("utf-8");
- process.stdin.on("data", (chunk) => {
- data += chunk;
- });
- process.stdin.on("end", () => fetchData(data));
+ await parseDataFromStdin();
}
if (process.argv.includes("--init")) {
return await initConfig();
}
})();
diff --git a/lib/fetch_data.ts b/lib/fetch_data.ts
new file mode 100644
index 0000000..a9ac9d9
--- /dev/null
+++ b/lib/fetch_data.ts
@@ -0,0 +1,18 @@
+import { Context } from "./classes/context";
+import { StdinJsonStrategy } from "./classes/stdin_json";
+import { StdinQueryKeyStrategy } from "./classes/stdin_query";
+import parseJSON from "./helpers/parse_json";
+
+async function fetchData(data: string): Promise<void> {
+ const parsed = await parseJSON(data);
+ const context = new Context();
+ if (!parsed) {
+ // Basically, when JSON has not been parsed, we'll get tasks as we would get them with empty queryKey
+ context.setStrategy(new StdinQueryKeyStrategy(data));
+ } else {
+ context.setStrategy(new StdinJsonStrategy(parsed));
+ }
+ return await context.execute();
+}
+
+export default fetchData;
diff --git a/lib/helpers/resolve_api_token.ts b/lib/helpers/resolve_api_token.ts
index aeeb497..1de295c 100644
--- a/lib/helpers/resolve_api_token.ts
+++ b/lib/helpers/resolve_api_token.ts
@@ -1,23 +1,23 @@
import { resolve } from "path";
import { readFile } from "fs";
import { promisify } from "util";
const _readFile = promisify(readFile);
async function resolveAPIToken(
path: string,
api: string
): Promise<string | undefined> {
try {
const config = await _readFile(resolve(path));
const parsedConfig = JSON.parse(config.toString());
- return parsedConfig.hosts[api].token as string;
+ return parsedConfig.hosts[`${api}api/`].token as string;
} catch (e) {
if (e) {
console.error(e);
}
}
return undefined;
}
export default resolveAPIToken;
diff --git a/lib/init.ts b/lib/init.ts
index b90b63c..5d0909c 100644
--- a/lib/init.ts
+++ b/lib/init.ts
@@ -1,120 +1,120 @@
import * as prompts from "prompts";
import { stat, promises, constants, writeFile } from "fs";
import { homedir } from "os";
import { promisify } from "util";
import { URL } from "url";
import { PromptAnswers } from "./interfaces/prompt_answers";
import resolveAPIToken from "./helpers/resolve_api_token";
import { resolve } from "path";
import { exit } from "process";
const _stat = promisify(stat);
const _writeFile = promisify(writeFile);
function onCancel() {
console.log("\x1b[33m%s\x1b[0m", "Exiting..");
exit(1);
}
async function promptUser() {
return await prompts(
[
{
type: "text",
name: "arc",
message: "Where would I find .arcrc config?",
initial: `${homedir()}/.arcrc`,
validate: async (path) => {
try {
await promises
.access(path, constants.F_OK | constants.W_OK)
.catch(() => {
throw new Error("Cannot find specified file!");
});
const stats = await _stat(path);
if (stats.isDirectory()) {
throw new Error("Entered path indicates folder");
}
} catch (e) {
return `${e.message}`;
}
return true;
},
},
{
type: "text",
name: "uri",
- message: "Enter full Conduit API URI",
+ message: "Enter full Phabricator URI (https://example.com/)",
validate: (uri) => {
try {
new URL(uri);
} catch (_) {
return "Not a walid URI";
}
return true;
},
},
{
type: "select",
name: "defaultRequest",
message:
"Select default option for sending data to Conduit API",
choices: [
{
title: "User-defined query key",
value: "configQueryKey",
},
{
title: "User-defined query key from stdin",
value: "configQueryKeyStdin",
},
{ title: "JSON file", value: "configJson" },
{ title: "JSON from stdin", value: "configJsonStdin" },
],
},
{
type: (prev) =>
prev === "configJson" || prev === "configQueryKey"
? "text"
: null,
name: (prev) => prev,
message: (prev: "configJson" | "configQueryKey") => {
switch (prev) {
case "configJson": {
return "Enter path to JSON file";
}
case "configQueryKey": {
return "Enter query key";
}
}
},
},
],
{ onCancel }
);
}
async function initConfig(): Promise<void> {
const answer = (await promptUser()) as PromptAnswers;
console.log("\x1b[33m%s\x1b[0m", "Creating config file..");
const config = {
token: await resolveAPIToken(answer.arc, answer.uri),
...answer,
};
try {
await _writeFile(
resolve(homedir(), ".roficatorrc"),
JSON.stringify(config)
);
console.log(
"\x1b[33m%s\x1b[0m",
`Config file created: ${resolve(homedir(), ".roficatorrc")}`
);
} catch (e) {
if (e) {
console.error("Failed to create config file.");
}
}
}
export default initConfig;
diff --git a/lib/parse_data_stdin.ts b/lib/parse_data_stdin.ts
new file mode 100644
index 0000000..634e63f
--- /dev/null
+++ b/lib/parse_data_stdin.ts
@@ -0,0 +1,13 @@
+import fetchData from "./fetch_data";
+
+async function parseDataFromStdin(): Promise<void> {
+ let data = "";
+ process.stdin.resume();
+ process.stdin.setEncoding("utf-8");
+ process.stdin.on("data", (chunk) => {
+ data += chunk;
+ });
+ process.stdin.once("end", () => fetchData(data));
+}
+
+export default parseDataFromStdin;
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Sat, Nov 8, 04:45 (1 d, 4 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1034026
Default Alt Text
(6 KB)
Attached To
Mode
rROFI Roficator
Attached
Detach File
Event Timeline
Log In to Comment