Compare commits

...

6 Commits

Author SHA1 Message Date
Thomas Hobson 5a05537a7f
cli: switch to axios 2021-03-13 16:44:33 +13:00
Thomas Hobson a1536ffe56
cli: merge with api-client 2021-03-13 16:08:35 +13:00
Thomas Hobson e5000917e8
dev(nix-shell): add yarn 2021-03-13 15:58:30 +13:00
Thomas Hobson 4e2be29461
Merge branch 'v3' of https://github.com/engineer-man/piston into v3 2021-03-13 14:31:44 +13:00
Thomas Hobson 69c013fa7a
dev: nix shell file 2021-03-13 14:31:40 +13:00
Thomas Hobson f615e26493
cli: change shebang to use env 2021-03-13 13:19:05 +13:00
10 changed files with 57 additions and 124 deletions

View File

@ -1 +0,0 @@
node_modules

View File

@ -1,70 +0,0 @@
const fetch = require('node-fetch')
function url_join(base, endpoint){
return base + endpoint
//return new URL(endpoint, base).href;
}
class APIWrapper {
#base;
constructor(base_url){
this.#base = base_url.toString()
}
async query(endpoint, options={}){
const url = url_join(this.#base, endpoint);
return await fetch(url, options)
.then(res=>res.json())
.then(res=>{if(res.data)return res.data; throw new Error(res.message)});
}
get(endpoint){
return this.query(endpoint);
}
post(endpoint, body={}){
return this.query(endpoint, {
method: 'post',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify(body)
})
}
delete(endpoint, body={}){
return this.query(endpoint, {
method: 'delete',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify(body)
})
}
get url_base(){
return this.#base
}
}
class PistonEngine extends APIWrapper {
constructor(base_url = 'http://127.0.0.1:6969'){
super(base_url);
}
run_job({language, version, files, main, args, stdin, compile_timeout, run_timeout}){
return this.post(`/jobs`, {language, version, files, main, args, stdin, compile_timeout, run_timeout})
}
list_packages(){
return this.get('/packages').then(x=>x.packages)
}
install_package({language, version}){
return this.post(`/packages/${language}/${version}`);
}
uninstall_package({language, version}){
return this.post(`/packages/${language}/${version}`);
}
}
module.exports = {PistonEngine}

View File

@ -1,11 +0,0 @@
{
"name": "piston-api-client",
"version": "1.0.0",
"description": "Wraps API of Piston Engine API",
"main": "index.cjs",
"author": "Thomas Hobson <thomas@hexf.me>",
"license": "MIT",
"dependencies": {
"node-fetch": "^2.6.1"
}
}

View File

@ -1,4 +1,4 @@
const {PistonEngine} = require('piston-api-client');
//const fetch = require('node-fetch');
const fs = require('fs');
const path = require('path');
const chalk = require('chalk');
@ -34,8 +34,6 @@ exports.builder = {
exports.handler = async function(argv){
const api = new PistonEngine(argv['piston-url']);
const files = [...(argv.files || []),argv.file]
.map(file_path => ({
name: path.basename(file_path),
@ -50,7 +48,7 @@ exports.handler = async function(argv){
})) || "";
const response = await api.run_job({
const request = {
language: argv.language,
version: argv['language-version'],
files: files,
@ -59,7 +57,10 @@ exports.handler = async function(argv){
stdin,
compile_timeout: argv.ct,
run_timeout: argv.rt
})
};
let response = await argv.axios.post('/jobs', request);
response = response.data
function step(name, ctx){
console.log(chalk.bold(`== ${name} ==`))

View File

@ -1,4 +1,3 @@
const {PistonEngine} = require('piston-api-client');
const chalk = require('chalk');
exports.command = ['install <language> <language-version>']
@ -13,15 +12,12 @@ const msg_format = {
}
exports.handler = async function(argv){
const api = new PistonEngine(argv['piston-url']);
const opts = {
language: argv['language'],
version: argv['language-version']
};
const install = await api.install_package(opts).catch(x=>x);
console.log(msg_format.color(install));
exports.handler = async function({axios, language, languageVersion}){
try{
const install = await axios.post(`/packages/${language}/${languageVersion}`)
console.log(msg_format.color(install.data));
}catch({response}){
console.error(response.data.message)
}
}

View File

@ -1,4 +1,4 @@
const {PistonEngine} = require('piston-api-client');
//const fetch = require('node-fetch');
const chalk = require('chalk');
exports.command = ['list']
@ -13,13 +13,12 @@ const msg_format = {
}
exports.handler = async function(argv){
const api = new PistonEngine(argv['piston-url']);
exports.handler = async function({axios}){
const packages = await api.list_packages();
const packages = await axios.get('/packages');
const pkg_msg = packages
const pkg_msg = packages.data.data.packages
.map(msg_format.color)
.join('\n');

View File

@ -1,4 +1,15 @@
#!/usr/bin/node
#!/usr/bin/env node
const axios = require('axios').default;
const axios_instance = function(argv){
argv.axios = axios.create({
baseURL: argv['piston-url']
});
return argv;
};
require('yargs')(process.argv.slice(2))
.option('piston-url', {
alias: ['u'],
@ -6,9 +17,10 @@ require('yargs')(process.argv.slice(2))
desc: 'Piston API URL',
string: true
})
.middleware(axios_instance)
.scriptName("piston")
.commandDir('commands')
.demandCommand()
.help()
.wrap(72)
.argv
.argv;

View File

@ -6,8 +6,8 @@
"author": "Thomas Hobson <thomas@hexf.me>",
"license": "MIT",
"dependencies": {
"axios": "^0.21.1",
"chalk": "^4.1.0",
"piston-api-client": "file:../api-client",
"yargs": "^16.2.0"
}
}

View File

@ -14,6 +14,13 @@ ansi-styles@^4.0.0, ansi-styles@^4.1.0:
dependencies:
color-convert "^2.0.1"
axios@^0.21.1:
version "0.21.1"
resolved "https://registry.yarnpkg.com/axios/-/axios-0.21.1.tgz#22563481962f4d6bde9a76d516ef0e5d3c09b2b8"
integrity sha512-dKQiRHxGD9PPRIUNIWvZhPTPpl1rf/OxTYKsqKUDjBwYylTvV7SjSHJb9ratfyzM6wCdLCOYLzs73qpg5c4iGA==
dependencies:
follow-redirects "^1.10.0"
chalk@^4.1.0:
version "4.1.0"
resolved "https://registry.yarnpkg.com/chalk/-/chalk-4.1.0.tgz#4e14870a618d9e2edd97dd8345fd9d9dc315646a"
@ -53,6 +60,11 @@ escalade@^3.1.1:
resolved "https://registry.yarnpkg.com/escalade/-/escalade-3.1.1.tgz#d8cfdc7000965c5a0174b4a82eaa5c0552742e40"
integrity sha512-k0er2gUkLf8O0zKJiAhmkTnJlTvINGv7ygDNPbeIsX/TJjGJZHuh9B2UxbsaEkmlEo9MfhrSzmhIlhRlI2GXnw==
follow-redirects@^1.10.0:
version "1.13.3"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.13.3.tgz#e5598ad50174c1bc4e872301e82ac2cd97f90267"
integrity sha512-DUgl6+HDzB0iEptNQEXLx/KhTmDb8tZUHSeLqpnjpknR70H0nC2t9N73BK6fN4hOvJ84pKlIQVQ4k5FFlBedKA==
get-caller-file@^2.0.5:
version "2.0.5"
resolved "https://registry.yarnpkg.com/get-caller-file/-/get-caller-file-2.0.5.tgz#4f94412a82db32f36e3b0b9741f8a97feb031f7e"
@ -68,25 +80,15 @@ is-fullwidth-code-point@^3.0.0:
resolved "https://registry.yarnpkg.com/is-fullwidth-code-point/-/is-fullwidth-code-point-3.0.0.tgz#f116f8064fe90b3f7844a38997c0b75051269f1d"
integrity sha512-zymm5+u+sCsSWyD9qNaejV3DFvhCKclKdizYaJUuHA83RLjb7nSuGnddCHGv0hk+KY7BMAlsWeK4Ueg6EV6XQg==
node-fetch@^2.6.1:
version "2.6.1"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.1.tgz#045bd323631f76ed2e2b55573394416b639a0052"
integrity sha512-V4aYg89jEoVRxRb2fJdAg8FHvI7cEyYdVAh94HH0UIK8oJxUfkjlDQN9RbMx+bEjP7+ggMiFRprSti032Oipxw==
"piston-api-client@file:../api-client":
version "1.0.0"
dependencies:
node-fetch "^2.6.1"
require-directory@^2.1.1:
version "2.1.1"
resolved "https://registry.yarnpkg.com/require-directory/-/require-directory-2.1.1.tgz#8c64ad5fd30dab1c976e2344ffe7f792a6a6df42"
integrity sha1-jGStX9MNqxyXbiNE/+f3kqam30I=
string-width@^4.1.0, string-width@^4.2.0:
version "4.2.0"
resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.0.tgz#952182c46cc7b2c313d1596e623992bd163b72b5"
integrity sha512-zUz5JD+tgqtuDjMhwIg5uFVV3dtqZ9yQJlZVfq4I01/K5Paj5UHj7VyrQOJvzawSVlKpObApbfD0Ed6yJc+1eg==
version "4.2.2"
resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.2.tgz#dafd4f9559a7585cfba529c6a0a4f73488ebd4c5"
integrity sha512-XBJbT3N4JhVumXE0eoLU9DCjcaF92KLNqTmFCnG1pf8duUxFGwtP6AD6nkjw9a3IdiRtL3E2w3JDiE/xi3vOeA==
dependencies:
emoji-regex "^8.0.0"
is-fullwidth-code-point "^3.0.0"
@ -121,9 +123,9 @@ y18n@^5.0.5:
integrity sha512-hsRUr4FFrvhhRH12wOdfs38Gy7k2FFzB9qgN9v3aLykRq0dRcdcpz5C9FxdS2NuhOrI/628b/KSTJ3rwHysYSg==
yargs-parser@^20.2.2:
version "20.2.6"
resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-20.2.6.tgz#69f920addf61aafc0b8b89002f5d66e28f2d8b20"
integrity sha512-AP1+fQIWSM/sMiET8fyayjx/J+JmTPt2Mr0FkrgqB4todtfa53sOsrSAcIrJRD5XS20bKUwaDIuMkWKCEiQLKA==
version "20.2.7"
resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-20.2.7.tgz#61df85c113edfb5a7a4e36eb8aa60ef423cbc90a"
integrity sha512-FiNkvbeHzB/syOjIUxFDCnhSfzAL8R5vs40MgLFBorXACCOAEaWu0gRZl14vG8MR9AOJIZbmkjhusqBYZ3HTHw==
yargs@^16.2.0:
version "16.2.0"

5
shell.nix Normal file
View File

@ -0,0 +1,5 @@
{ pkgs ? import <nixpkgs> {} }:
pkgs.mkShell {
# nativeBuildInputs is usually what you want -- tools you need to run
nativeBuildInputs = [ pkgs.nodejs-15_x pkgs.yarn ];
}