From 5cf63d7c3c355821a8339a409e92acdadf5fc7bf Mon Sep 17 00:00:00 2001 From: silverwizard Date: Sun, 28 Oct 2018 21:49:30 -0400 Subject: [PATCH] Make pig latin --- Dockerfile | 5 +++++ LICENSE.md | 1 + README.md | 13 +++++++++++++ docker-compose.yml | 11 +++++++++++ piglatin.js | 8 ++++++++ piglatin.sh | 42 ++++++++++++++++++++++++++++++++++++++++++ test.sh | 40 ++++++++++++++++++++++++++++++++++++++++ 7 files changed, 120 insertions(+) create mode 100644 Dockerfile create mode 100644 LICENSE.md create mode 100644 README.md create mode 100644 docker-compose.yml create mode 100644 piglatin.js create mode 100755 piglatin.sh create mode 100644 test.sh diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..a0bd10c --- /dev/null +++ b/Dockerfile @@ -0,0 +1,5 @@ +FROM node +MAINTAINER Sean +RUN npm install pig-latin +ADD piglatin.js piglatin.js +CMD ["node", "piglatin.js"] diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..631255f --- /dev/null +++ b/LICENSE.md @@ -0,0 +1 @@ +All work done by Sean Howard. Licensed under CC0. diff --git a/README.md b/README.md new file mode 100644 index 0000000..9e566b1 --- /dev/null +++ b/README.md @@ -0,0 +1,13 @@ +piglatin.sh exists to support the piglatin docker container service. + +It requires an external pig latin service to be running, but a docker container is provided to support local development. + +.piglatinrc MUST be in the form + +`HOST=hostname +PORT=portname` + +If you must either of these then the defaults will be used. Other imports are ignored. + +Requirements: + This requires a standard POSIX bash shell with dos2unix installed to support removing CRLFs from webserver diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..95eb719 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,11 @@ +version: '2' +services: + pig: + image: node + build: + context: . + dockerfile: ./Dockerfile + ports: + - "8124:8124" + + diff --git a/piglatin.js b/piglatin.js new file mode 100644 index 0000000..642f0ca --- /dev/null +++ b/piglatin.js @@ -0,0 +1,8 @@ +var piglatin = require('pig-latin'); +var http = require('http'); + +server = http.createServer(function (req, res) { + res.write(piglatin(req.url.replace("/",""))); +}); + server.listen(8124, "0.0.0.0"); +console.log('Server running at http://127.0.0.1:8124/'); diff --git a/piglatin.sh b/piglatin.sh new file mode 100755 index 0000000..91afe43 --- /dev/null +++ b/piglatin.sh @@ -0,0 +1,42 @@ +#!/bin/bash +#Should be configurable for users without a local piglatin service +# set a default hostname +HOST=localhost +# set a default port +PORT=8124 +# if the .piglatinrc file exists, use this instead of the default +if [ -f ~/.piglatinrc ]; then +source ~/.piglatinrc +fi +# Since the container will only do one word at a time, parse the words out +for PIG in $@ +do +if [ ${PIG:0:2} = "yt" -o ${PIG:0:2} = "xr" ] +then + export DEMANGLE="${PIG:0:2}" + PIG=`echo $PIG| sed -e "s/^yt/e/" -e "s/^xr/e/"` +fi +#this is the easiest way to get the correct line from the container +WORD=`printf "GET /$PIG HTTP/1.1\n\nclose\n\n"|nc $HOST $PORT|dos2unix|tail -n3|head -n1|sed "s/way$/ay/"` +RETURN=0 +if [ "${WORD:0:1}" = "u" ] +then + if [ "${WORD: -3}" = "qay" ] + then + echo $WORD | sed -e "s/qay/quay/" -e "s/^u//" + RETURN=1 + fi +fi +if [ $RETURN = 0 ] +then + if [ -n "$DEMANGLE" ] + then + WORD=`echo $WORD | sed -e "s/^./$DEMANGLE/"` + fi + echo $WORD +fi +# the container only has one worker so treat it slowly +sleep 1 +# merge script output +done | tr "\n" " "|sed "s/ $//" +echo diff --git a/test.sh b/test.sh new file mode 100644 index 0000000..8418fc2 --- /dev/null +++ b/test.sh @@ -0,0 +1,40 @@ +#!/bin/sh + +if [ $# -ne 1 ]; then + echo "Usage: sh test.sh /path/to/program" 1>&2 + exit 1 +fi + +PROGRAM="$1" + +assert_run() { + OUTPUT="$("$PROGRAM" "$1")" + if [ "$2" != "$("$PROGRAM" "$1")" ]; then + echo "$PROGRAM '$1'" 1>&2 + echo "expected: $2" 1>&2 + echo "got: $OUTPUT" 1>&2 + exit 1 + fi +} + +assert_run "apple" "appleay" +assert_run "ear" "earay" +assert_run "igloo" "iglooay" +assert_run "object" "objectay" +assert_run "under" "underay" +assert_run "equal" "equalay" +assert_run "pig" "igpay" +assert_run "koala" "oalakay" +assert_run "yellow" "ellowyay" +assert_run "xenon" "enonxay" +assert_run "qat" "atqay" +assert_run "chair" "airchay" +assert_run "queen" "eenquay" +assert_run "square" "aresquay" +assert_run "therapy" "erapythay" +assert_run "thrush" "ushthray" +assert_run "school" "oolschay" +assert_run "yttria" "yttriaay" +assert_run "xray" "xrayay" + +assert_run "quick fast run" "ickquay astfay unray"