Skip to content

Commit

Permalink
CAP v1.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
SpideyZac committed Jan 15, 2023
1 parent 885a2a9 commit 42af3fa
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 30 deletions.
4 changes: 2 additions & 2 deletions bin/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ node_modules/
dist/

# Build
bin/
bin.zip
cactive-bin/
cactive-bin.zip
cactive-bin-linux
cactive-bin-macos
cactive-bin-win.exe
22 changes: 22 additions & 0 deletions bin/LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# CactiveNetwork Licence Version 1.0

## Last Updated:
14th November 2020

## Licensor Information:
Copyright ©️ 2022 CactiveNetwork [[email protected]](mailto://[email protected])

### Definitions

- **"Software"** (or **"Work"**) shall mean the source code, algorithms, processes or related material used in this work.
- **"Licence"** shall mean the terms of conditions for re-production, use and additional distribution.
- **"Licensor"** shall mean the copyright owner, or other entity authorised by the copyright owner granting this Licence.
- **"You"** (or **"Your"**) shall mean an individual exercising permissions granted by this License.

### Terms of this licence

Permission is granted, free of charge, to any person obtaining a copy of this "Software", to the rights to, use, copy, modify, merge, publish, distribute, and/or sell copies of the Software without restriction, this holds true as long as the software is not used to intentionally hurt people, and/or groups or communities mentally or physically.

Unless required by applicable law or agreed to in writing, the Licensor provides this work on an "as-is" basis without warranties or conditions of any kind. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License.

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the "Software" without modification.
53 changes: 53 additions & 0 deletions bin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
### What is this?
This is CAP or Cactive's Assembled Packages. In short, it is a compiled NodeJS script that allows you to use Cactive's products from any coding language or without one at all!

### How do I set it up?
First, install the cactive-bin.zip file from the most recent release

Extract that zip file

Now in the same directory as the cactive-bin extracted folder, open cmd

Now execute the correct file and add a -h (help) flag. For example, on Windows:
```bash
.\cactive-bin\cactive-bin-win.exe -h
```

### cactive.json File
The Cactive JSON file's location is specified using the -f flag and defaults to "./cactive.json"

The file contains special commands that hold which module, function, and args to pass. For example:
```json
{
"retrieve": {
"module": "ip",
"func": "retrieve",
"args": "user"
},
"google": {
"module": "ip",
"func": "retrieve",
"args": {
"ip": "8.8.8.8"
}
}
}
```

Here we define two commands, "retrieve" and "google". The "module" specifies that we will run a command from Cactive's IP Module. "func" specifies that we will run the "retrieve" function from Cactive's IP Module. Args specifies two different things, it specifies for "retrieve", we will let the user decide the args to be passed. For "google", it specifies
the arguments passed to the function will always be {"ip": "8.8.8.8"}.

To use this file we can run our cactive-bin program and we can do something like this where the text after ">>> " is stringified JSON
```bash
>>> {"command":"google"}
[output here]
>>> {"command":"retrieve","args":{"ip":"8.8.8.8"}}
```

The data after ">>> " is just stringified JSON that matches this style:
```json
{
"command": "command to execute from cactive.json",
"args?": "args to pass to the function defined in cactive.json"
}
```
4 changes: 3 additions & 1 deletion bin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"main": "./src/index.ts",
"bin": "./dist/index.js",
"scripts": {
"build": "tsc && pkg -C GZip --ouput cactive-bin ."
"build": "tsc && pkg -C GZip --out-path cactive-bin/ .",
"build-nodeploy": "tsc"
},
"pkg": {
"assets": [
Expand All @@ -27,6 +28,7 @@
},
"dependencies": {
"phin": "^3.7.0",
"readline": "^1.3.0",
"ts-command-line-args": "^2.3.1"
}
}
80 changes: 56 additions & 24 deletions bin/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { parse } from "ts-command-line-args";
import { readFileSync, existsSync } from "fs";
import { createInterface } from "readline";

import Arguments from "./types/arguments";
import Cactive from "./types/cactive";
import Input from "./types/input";

import { self, retrieve } from "./ip";

function test(a: string, b: string) {
console.log(a, b)
}

const year = new Date().getFullYear();
const valid_modules: string[] = [
"ip",
Expand All @@ -26,36 +26,68 @@ const modules_functions_mapping: {[k: string]: {[k: string]: (...args: any[]) =>
};
const args = parse<Arguments>(
{
module: { type: String, alias: "m", description: "The Cactive module to use (ip, etc.)" },
func: { type: String, alias: "f", description: "The function to use from that module (retrieve, etc.)" },
args: { type: String, optional: true, alias: "a", description: "The args to pass to that function seperated by ',' ('8.8.8.8, hi')" },
file: { type: String, alias: "f", defaultValue: "cactive.json", description: "The file location of you cactive.json file" },
help: { type: Boolean, optional: true, alias: "h", description: "Prints the usage guide" },
},
{
helpArg: "help",
headerContentSections: [{ header: "Cactive Bin Config", content: "By using Cactive Bin, you agree to our CactiveNetwork Licence Version 1.0" }],
footerContentSections: [{ header: `Copyright © ${year} Luke Matison, Cactive.`, content: "All Rights Reserved." }],
footerContentSections: [{ header: `Copyright © ${year} Cactive.`, content: "All Rights Reserved." }],
},
);

if (!args.help) {
if (!valid_modules.includes(args.module)) throw Error("That is not a valid module!");
if (!valid_functions[args.module].includes(args.func)) throw Error("That is not a valid function for that module!");

// Load Module's Function With Args
let pass_args: string[];
if (args.args) {
pass_args = args.args.replace(/\[|\]/g, "").split(",");
} else {
pass_args = [];
if (!existsSync(args.file)) throw Error("Your file has to exist!");

const rl = createInterface({
input: process.stdin,
output: process.stdout,
});
const question = (questionText: string) => new Promise<string>(resolve => rl.question(questionText, resolve));

let cactiveFile: Cactive;

try {
cactiveFile = JSON.parse(readFileSync(args.file, 'utf-8'));
} catch (e) {
throw Error("Your file has to be JSON parsable!");
}

const arg_length = modules_functions_mapping[args.module][args.func].length;
if (pass_args.length < arg_length || pass_args.length > arg_length) throw Error("User passed too little or too many arguments for that function");
(async() => {
while (true) {
let input: Input;
try {
input = JSON.parse(await question(">>> "));
} catch (e) {
throw Error("Input must be JSON parsable!");
}

(async () => {
await modules_functions_mapping[args.module][args.func](...pass_args);
})().catch(e => {
console.log(e);
});
if (!cactiveFile[input.command]) throw Error(`Command ${input.command} was not found in your cactive.json!`);

const command = cactiveFile[input.command];
let args: any[];
let module = command.module;

if (!valid_modules.includes(module)) throw Error(`Module ${module} is not a valid module!`);
if (!valid_functions[module].includes(command.func)) throw Error(`Function ${command.func} is not a valid function for module ${command.module}!`);

if (command.args === "user") {
if (!input.args) throw Error("Arguments are required on a user args command!");

args = Object.values(input.args);
} else {
args = Object.values(command.args);
}

const func = modules_functions_mapping[command.module][command.func];

if (args.length < func.length || args.length > func.length) throw Error("You passed too little or too many arguments for that function!");

try {
await func(...args);
} catch (e) {
console.log(e);
}
}
})();
}
4 changes: 1 addition & 3 deletions bin/src/types/arguments.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
export default interface Arguments {
module: string
func: string
args?: string
file: string
help?: boolean
}
9 changes: 9 additions & 0 deletions bin/src/types/cactive.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
type args = "user";

export default interface Cactive {
[k: string]: {
module: string
func: string
args: args | {[k: string]: any}
}
}
4 changes: 4 additions & 0 deletions bin/src/types/input.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default interface Input {
command: string
args?: {[k: string]: any}
}
5 changes: 5 additions & 0 deletions bin/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -814,6 +814,11 @@ readable-stream@^3.1.1, readable-stream@^3.4.0, readable-stream@^3.6.0:
string_decoder "^1.1.1"
util-deprecate "^1.0.1"

readline@^1.3.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/readline/-/readline-1.3.0.tgz#c580d77ef2cfc8752b132498060dc9793a7ac01c"
integrity sha512-k2d6ACCkiNYz222Fs/iNze30rRJ1iIicW7JuX/7/cozvih6YCkFZH+J6mAFDVgv0dRBaAyr4jDqC95R2y4IADg==

reduce-flatten@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/reduce-flatten/-/reduce-flatten-2.0.0.tgz#734fd84e65f375d7ca4465c69798c25c9d10ae27"
Expand Down

0 comments on commit 42af3fa

Please sign in to comment.