Skip to content

Commit 31f7897

Browse files
Add interpolation
* use interpolation to add args to the image commands
1 parent 76acc60 commit 31f7897

File tree

28 files changed

+677
-74
lines changed

28 files changed

+677
-74
lines changed

functions/codescope/Dockerfile

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,11 @@ RUN --mount=type=cache,target=/nix,from=nixos/nix:latest@sha256:3f6c77ee4d2c82e4
1919
cp -R $(nix-store -qR /tmp/output/result) /tmp/nix-store-closure
2020
EOF
2121

22-
FROM scratch
22+
FROM babashka/babashka:latest@sha256:9e0381fc4c78ee6ff12fd8836352cf343afba289aceb77e36129d92f30a92cc7
2323

2424
WORKDIR /app
2525

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

29-
ENTRYPOINT ["/app/result/bin/codescope"]
30-
29+
ENTRYPOINT ["/app/result/bin/entrypoint"]

functions/codescope/flake.nix

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,31 @@
2626
in
2727
rec {
2828

29-
packages.clipboard = clipboard;
30-
packages.default = pkgs.callPackage ./derivation.nix {inherit clipboard;};
29+
packages = rec {
30+
31+
codescope = pkgs.callPackage ./derivation.nix {inherit clipboard;};
32+
33+
# this derivation just contains the init.clj script
34+
scripts = pkgs.stdenv.mkDerivation {
35+
name = "scripts";
36+
src = ./.;
37+
installPhase = ''
38+
cp init.clj $out
39+
'';
40+
};
41+
42+
run-entrypoint = pkgs.writeShellScriptBin "entrypoint" ''
43+
export PATH=${pkgs.lib.makeBinPath [codescope pkgs.man]}
44+
export SSL_CERT_FILE=${pkgs.cacert}/etc/ssl/certs/ca-bundle.crt
45+
/usr/local/bin/bb ${scripts} "$@"
46+
'';
47+
48+
default = pkgs.buildEnv {
49+
name = "codescope";
50+
paths = [ run-entrypoint ];
51+
};
52+
};
53+
3154
devShells.default = pkgs.mkShell {
3255
name = "python";
3356
nativeBuildInputs = with pkgs;

functions/codescope/init.clj

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
(ns init
2+
(:require
3+
[babashka.process]
4+
[cheshire.core]))
5+
6+
(defn output [args]
7+
(try
8+
(->
9+
(apply babashka.process/process
10+
{:out :string}
11+
args)
12+
(deref)
13+
(babashka.process/check))
14+
(catch Throwable _ nil)))
15+
16+
(defn man [t]
17+
(output ["man" t]))
18+
(defn help [t]
19+
(output [t "--help"]))
20+
(defn h [t]
21+
(output [t "-h"]))
22+
23+
(try
24+
(let [[json-string & extra-args] *command-line-args*
25+
{:keys [args]} (cheshire.core/parse-string json-string true)]
26+
(println
27+
(-> (if (= "man" (first extra-args))
28+
(let [t "codescope"] (or (man t) (help t) (h t)))
29+
(-> (babashka.process/process
30+
{:out :string}
31+
(format "codescope %s /project" args))
32+
(deref)
33+
(babashka.process/check)))
34+
(deref)
35+
(babashka.process/check)
36+
:out)))
37+
(catch Throwable t
38+
(binding [*out* *err*]
39+
(println (str "Error: " (.getMessage t)))
40+
(System/exit 1))))
41+

functions/codescope/runbook.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,23 @@ docker build -t vonwig/codescope:latest -f Dockerfile .
4141
This is containerized so you'll need to bind mount the project path in the container.
4242

4343
```sh
44-
docker run -it --rm --mount=type=bind,source=$PWD,target=/project vonwig/codescope:latest /project
44+
docker run -it --rm \
45+
--mount=type=bind,source=/Users/slim/slimslenderslacks/flask-nix-example,target=/project --workdir /project \
46+
vonwig/codescope:latest \
47+
'{"args": "/project"}'
4548
```
49+
50+
```sh
51+
docker run -it --rm \
52+
--mount=type=bind,source=/Users/slim/slimslenderslacks/flask-nix-example,target=/project --workdir /project \
53+
vonwig/codescope:latest \
54+
'{"args": "-i /project"}'
55+
```
56+
57+
```sh
58+
docker run -it --rm \
59+
--mount=type=bind,source=/Users/slim/slimslenderslacks/flask-nix-example,target=/project --workdir /project \
60+
vonwig/codescope:latest \
61+
'{"args": ""}' man
62+
```
63+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# syntax = docker/dockerfile:1.4
2+
FROM nixos/nix:latest@sha256:3f6c77ee4d2c82e472e64e6cd7087241dc391421a0b42c22e6849c586d5398d9 AS builder
3+
4+
WORKDIR /tmp/build
5+
RUN mkdir /tmp/nix-store-closure
6+
7+
# ignore SC2046 because the output of nix-store -qR will never have spaces - this is safe here
8+
# hadolint ignore=SC2046
9+
RUN --mount=type=cache,target=/nix,from=nixos/nix:latest@sha256:3f6c77ee4d2c82e472e64e6cd7087241dc391421a0b42c22e6849c586d5398d9,source=/nix \
10+
--mount=type=cache,target=/root/.cache \
11+
--mount=type=bind,target=/tmp/build \
12+
<<EOF
13+
nix \
14+
--extra-experimental-features "nix-command flakes" \
15+
--option filter-syscalls false \
16+
--show-trace \
17+
--log-format raw \
18+
build . --out-link /tmp/output/result
19+
cp -R $(nix-store -qR /tmp/output/result) /tmp/nix-store-closure
20+
EOF
21+
22+
FROM scratch
23+
24+
WORKDIR /app
25+
26+
COPY --from=builder /tmp/nix-store-closure /nix/store
27+
COPY --from=builder /tmp/output/ /app/
28+
29+
ENTRYPOINT ["/app/result/bin/entrypoint"]

functions/javascript-runner/chat

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
3+
## ROLE assistant
4+
In Node.js, you can access command line arguments via the process.argv array. This will contain the complete command-line.
5+
6+
Here is a simple program that reads a JavaScript string from the first command line argument and then evaluates it.
7+
8+
```javascript
9+
// 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
10+
const args = process.argv.slice(2);
11+
12+
if(args.length == 0) {
13+
console.log("Please provide javascript command as argument.");
14+
process.exit();
15+
}
16+
17+
try {
18+
let result = eval(args[0]);
19+
console.log(result);
20+
} catch(e) {
21+
console.error("Error in executing provided javascript command:", e.message);
22+
}
23+
```
24+
25+
To use this script, you need to write the JavaScript string as a command line argument.
26+
27+
```shell
28+
node evalScript.js "console.log('Hello, world!')"
29+
```
30+
31+
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().
32+
{"done":"stop"}

functions/javascript-runner/flake.lock

Lines changed: 116 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
description = "javascript runner";
3+
4+
inputs = {
5+
nixpkgs.url = "github:NixOS/nixpkgs/nixos-23.11";
6+
flake-utils.url = "github:numtide/flake-utils";
7+
devshell = {
8+
url = "github:numtide/devshell";
9+
inputs.nixpkgs.follows = "nixpkgs";
10+
};
11+
};
12+
13+
outputs = { self, nixpkgs, flake-utils, devshell }:
14+
15+
flake-utils.lib.eachDefaultSystem
16+
(system:
17+
let
18+
overlays = [
19+
devshell.overlays.default
20+
];
21+
pkgs = import nixpkgs {
22+
inherit system overlays;
23+
};
24+
25+
in
26+
rec {
27+
28+
packages = rec {
29+
30+
# this derivation just contains the init.clj script
31+
scripts = pkgs.stdenv.mkDerivation {
32+
name = "scripts";
33+
src = ./.;
34+
installPhase = ''
35+
cp main.js $out
36+
'';
37+
};
38+
39+
default = pkgs.writeShellScriptBin "entrypoint" ''
40+
${pkgs.nodejs}/bin/node ${scripts} "$@"
41+
'';
42+
};
43+
});
44+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
if(process.argv.length > 2) {
2+
// get the first command line argument
3+
var cmdArgument = process.argv[2];
4+
5+
// evaluate JS string
6+
try {
7+
var result = eval(cmdArgument);
8+
} catch(e) {
9+
console.error('Error: ' + e.message);
10+
}
11+
} else {
12+
console.error('No JavaScript string provided to evaluate!');
13+
}
14+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# prompt user
2+
3+
Write a javascript program that reads a javascript string from the first command line argument and then evaluates it.
4+
5+
# build
6+
7+
```sh
8+
docker build -t vonwig/javascript-runner:latest .
9+
```
10+
11+
```sh
12+
docker push vonwig/javascript-runner:latest
13+
```
14+
15+
# run
16+
17+
```sh
18+
docker run --rm -v thread:/thread vonwig/javascript-runner:latest "console.log('gorsh');"
19+
```
20+
21+
# multi-platform build
22+
23+
```sh
24+
docker buildx build \
25+
--builder hydrobuild \
26+
--platform linux/amd64,linux/arm64 \
27+
--tag vonwig/javascript-runner:latest \
28+
--file Dockerfile \
29+
--push .
30+
```
31+

0 commit comments

Comments
 (0)