Skip to content

Commit 3e5bcca

Browse files
committed
core: Add a ys::ext for extra / external functions
1 parent ad12f4b commit 3e5bcca

2 files changed

Lines changed: 39 additions & 0 deletions

File tree

core/src/yamlscript/runtime.clj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
[ys.clj]
2828
[ys.csv]
2929
[ys.dwim]
30+
[ys.ext]
3031
[ys.json]
3132
[ys.std]
3233
[ys.taptest]
@@ -123,6 +124,8 @@
123124
(sci/copy-ns babashka.process (sci/create-ns 'process)))
124125
(def set-namespace
125126
(sci/copy-ns clojure.set (sci/create-ns 'set)))
127+
(def ext-namespace
128+
(sci/copy-ns ys.ext (sci/create-ns 'ys.ext)))
126129
(def std-namespace
127130
(sci/copy-ns ys.std (sci/create-ns 'std)))
128131
(def str-namespace
@@ -149,6 +152,7 @@
149152

150153
'cli cli-namespace 'ys.cli cli-namespace
151154
'csv csv-namespace 'ys.csv csv-namespace
155+
'ext ext-namespace 'ys.ext ext-namespace 'x ext-namespace
152156
'fs fs-namespace 'ys.fs fs-namespace
153157
'http http-namespace 'ys.http http-namespace
154158
'io io-namespace 'ys.io io-namespace

core/src/ys/ext.clj

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
;; Copyright 2023-2025 Ingy dot Net
2+
;; This code is licensed under MIT license (See License for details)
3+
4+
(ns ys.ext
5+
(:require
6+
[clojure.string :as str]
7+
[babashka.process :as process]
8+
[yamlscript.util :as util]
9+
[ys.yaml :as yaml]))
10+
11+
(defn github-raw-url [spec]
12+
(let [[owner repo ref & path] (str/split spec #"/")]
13+
(format
14+
"https://raw.githubusercontent.com/%s/%s/refs/heads/%s/%s"
15+
owner repo ref (str/join "/" path))))
16+
17+
(defn yq [data cmd]
18+
(let [yaml (yaml/dump data)
19+
res (process/sh {:in yaml}
20+
"yq" "-e" cmd)
21+
{:keys [exit out err]} res]
22+
(when (and
23+
(not= 0 exit)
24+
(not= err "Error: no matches found\n"))
25+
(util/die "yq error: " (:err res)))
26+
27+
(let [data (if (str/blank? out)
28+
nil
29+
(yaml/load-all out))]
30+
(if (= 1 (count data))
31+
(first data)
32+
data))))
33+
34+
(comment
35+
)

0 commit comments

Comments
 (0)