Skip to content

Commit

Permalink
Add interpolation
Browse files Browse the repository at this point in the history
* use interpolation to add args to the image commands
  • Loading branch information
slimslenderslacks committed Sep 10, 2024
1 parent 76acc60 commit 31f7897
Show file tree
Hide file tree
Showing 28 changed files with 677 additions and 74 deletions.
5 changes: 2 additions & 3 deletions functions/codescope/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@ RUN --mount=type=cache,target=/nix,from=nixos/nix:latest@sha256:3f6c77ee4d2c82e4
cp -R $(nix-store -qR /tmp/output/result) /tmp/nix-store-closure
EOF

FROM scratch
FROM babashka/babashka:latest@sha256:9e0381fc4c78ee6ff12fd8836352cf343afba289aceb77e36129d92f30a92cc7

WORKDIR /app

COPY --from=builder /tmp/nix-store-closure /nix/store
COPY --from=builder /tmp/output/ /app/

ENTRYPOINT ["/app/result/bin/codescope"]

ENTRYPOINT ["/app/result/bin/entrypoint"]
27 changes: 25 additions & 2 deletions functions/codescope/flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,31 @@
in
rec {

packages.clipboard = clipboard;
packages.default = pkgs.callPackage ./derivation.nix {inherit clipboard;};
packages = rec {

codescope = pkgs.callPackage ./derivation.nix {inherit clipboard;};

# this derivation just contains the init.clj script
scripts = pkgs.stdenv.mkDerivation {
name = "scripts";
src = ./.;
installPhase = ''
cp init.clj $out
'';
};

run-entrypoint = pkgs.writeShellScriptBin "entrypoint" ''
export PATH=${pkgs.lib.makeBinPath [codescope pkgs.man]}
export SSL_CERT_FILE=${pkgs.cacert}/etc/ssl/certs/ca-bundle.crt
/usr/local/bin/bb ${scripts} "$@"
'';

default = pkgs.buildEnv {
name = "codescope";
paths = [ run-entrypoint ];
};
};

devShells.default = pkgs.mkShell {
name = "python";
nativeBuildInputs = with pkgs;
Expand Down
41 changes: 41 additions & 0 deletions functions/codescope/init.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
(ns init
(:require
[babashka.process]
[cheshire.core]))

(defn output [args]
(try
(->
(apply babashka.process/process
{:out :string}
args)
(deref)
(babashka.process/check))
(catch Throwable _ nil)))

(defn man [t]
(output ["man" t]))
(defn help [t]
(output [t "--help"]))
(defn h [t]
(output [t "-h"]))

(try
(let [[json-string & extra-args] *command-line-args*
{:keys [args]} (cheshire.core/parse-string json-string true)]
(println
(-> (if (= "man" (first extra-args))
(let [t "codescope"] (or (man t) (help t) (h t)))
(-> (babashka.process/process
{:out :string}
(format "codescope %s /project" args))
(deref)
(babashka.process/check)))
(deref)
(babashka.process/check)
:out)))
(catch Throwable t
(binding [*out* *err*]
(println (str "Error: " (.getMessage t)))
(System/exit 1))))

20 changes: 19 additions & 1 deletion functions/codescope/runbook.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,23 @@ docker build -t vonwig/codescope:latest -f Dockerfile .
This is containerized so you'll need to bind mount the project path in the container.

```sh
docker run -it --rm --mount=type=bind,source=$PWD,target=/project vonwig/codescope:latest /project
docker run -it --rm \
--mount=type=bind,source=/Users/slim/slimslenderslacks/flask-nix-example,target=/project --workdir /project \
vonwig/codescope:latest \
'{"args": "/project"}'
```

```sh
docker run -it --rm \
--mount=type=bind,source=/Users/slim/slimslenderslacks/flask-nix-example,target=/project --workdir /project \
vonwig/codescope:latest \
'{"args": "-i /project"}'
```

```sh
docker run -it --rm \
--mount=type=bind,source=/Users/slim/slimslenderslacks/flask-nix-example,target=/project --workdir /project \
vonwig/codescope:latest \
'{"args": ""}' man
```

29 changes: 29 additions & 0 deletions functions/javascript-runner/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# syntax = docker/dockerfile:1.4
FROM nixos/nix:latest@sha256:3f6c77ee4d2c82e472e64e6cd7087241dc391421a0b42c22e6849c586d5398d9 AS builder

WORKDIR /tmp/build
RUN mkdir /tmp/nix-store-closure

# ignore SC2046 because the output of nix-store -qR will never have spaces - this is safe here
# hadolint ignore=SC2046
RUN --mount=type=cache,target=/nix,from=nixos/nix:latest@sha256:3f6c77ee4d2c82e472e64e6cd7087241dc391421a0b42c22e6849c586d5398d9,source=/nix \
--mount=type=cache,target=/root/.cache \
--mount=type=bind,target=/tmp/build \
<<EOF
nix \
--extra-experimental-features "nix-command flakes" \
--option filter-syscalls false \
--show-trace \
--log-format raw \
build . --out-link /tmp/output/result
cp -R $(nix-store -qR /tmp/output/result) /tmp/nix-store-closure
EOF

FROM scratch

WORKDIR /app

COPY --from=builder /tmp/nix-store-closure /nix/store
COPY --from=builder /tmp/output/ /app/

ENTRYPOINT ["/app/result/bin/entrypoint"]
32 changes: 32 additions & 0 deletions functions/javascript-runner/chat
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@


## ROLE assistant
In Node.js, you can access command line arguments via the process.argv array. This will contain the complete command-line.

Here is a simple program that reads a JavaScript string from the first command line argument and then evaluates it.

```javascript
// current file name will occupy the 0th index, followed by file name (or execution command e.g "node") on 1st index and user supplied argument from 2nd index onwards
const args = process.argv.slice(2);

if(args.length == 0) {
console.log("Please provide javascript command as argument.");
process.exit();
}

try {
let result = eval(args[0]);
console.log(result);
} catch(e) {
console.error("Error in executing provided javascript command:", e.message);
}
```

To use this script, you need to write the JavaScript string as a command line argument.

```shell
node evalScript.js "console.log('Hello, world!')"
```

Note: Using eval() function can be dangerous because it executes the code it's given with the privileges of the caller. Even, it can be harmful when used with user-provided input, leading to potential code injection vulnerabilities. Be cautious while using eval().
{"done":"stop"}
116 changes: 116 additions & 0 deletions functions/javascript-runner/flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 44 additions & 0 deletions functions/javascript-runner/flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
description = "javascript runner";

inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-23.11";
flake-utils.url = "github:numtide/flake-utils";
devshell = {
url = "github:numtide/devshell";
inputs.nixpkgs.follows = "nixpkgs";
};
};

outputs = { self, nixpkgs, flake-utils, devshell }:

flake-utils.lib.eachDefaultSystem
(system:
let
overlays = [
devshell.overlays.default
];
pkgs = import nixpkgs {
inherit system overlays;
};

in
rec {

packages = rec {

# this derivation just contains the init.clj script
scripts = pkgs.stdenv.mkDerivation {
name = "scripts";
src = ./.;
installPhase = ''
cp main.js $out
'';
};

default = pkgs.writeShellScriptBin "entrypoint" ''
${pkgs.nodejs}/bin/node ${scripts} "$@"
'';
};
});
}
14 changes: 14 additions & 0 deletions functions/javascript-runner/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
if(process.argv.length > 2) {
// get the first command line argument
var cmdArgument = process.argv[2];

// evaluate JS string
try {
var result = eval(cmdArgument);
} catch(e) {
console.error('Error: ' + e.message);
}
} else {
console.error('No JavaScript string provided to evaluate!');
}

31 changes: 31 additions & 0 deletions functions/javascript-runner/runbook.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# prompt user

Write a javascript program that reads a javascript string from the first command line argument and then evaluates it.

# build

```sh
docker build -t vonwig/javascript-runner:latest .
```

```sh
docker push vonwig/javascript-runner:latest
```

# run

```sh
docker run --rm -v thread:/thread vonwig/javascript-runner:latest "console.log('gorsh');"
```

# multi-platform build

```sh
docker buildx build \
--builder hydrobuild \
--platform linux/amd64,linux/arm64 \
--tag vonwig/javascript-runner:latest \
--file Dockerfile \
--push .
```

File renamed without changes.
File renamed without changes.
8 changes: 8 additions & 0 deletions functions/node_intersector/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

```sh
docker build . -t vonwig/node_intersector
```

```sh
docker run --mount type=bind,source=$PWD,target=/project --workdir /project vonwig/node_intersector '{"path": "test_file.py", "line": 32}'
```
File renamed without changes.
Loading

0 comments on commit 31f7897

Please sign in to comment.