haskell-te: 7d0eda3d79d6f2b4f251e063eba2baa8ecb6d903
1: { fail, jq, lib, runCommand, withNix, wrap, writeScript }:
2: with builtins;
3: with lib;
4:
5: rec {
6: script = ''
7: [[ -f "$clusters" ]] || {
8: echo "Given cluster file '$clusters' doesn't exist" 1>&2
9: exit 2
10: }
11: "${fromStdin}" < "$clusters"
12: '';
13:
14: fromStdin = wrap {
15: name = "format-stdin";
16: paths = [ fail jq ];
17: vars = {
18: FILTER =
19: "map(select(.cluster == $cl and .type != null and .quickspecable))";
20: };
21: script = ''
22: set -e
23: set -o pipefail
24: [[ -n "$clCount" ]] || fail "No clCount given, aborting"
25:
26: INPUT=$(cat)
27:
28: # Select entries which have a "cluster" attribute matching the given
29: # number, a non-null "type" attribute and a true "quickspecable" attribute
30: function clusterContent {
31: echo "$INPUT" | jq -c --argjson cl "$1" "$FILTER | map(del(.features))"
32: }
33:
34: function postProcess {
35: if [[ -n "$SIMPLE" ]]
36: then
37: jq -s '.'
38: else
39: cat
40: fi
41: }
42:
43: for CLUSTER in $(seq 1 "$clCount")
44: do
45: # Work out the relevant output path; we use "$out1" "$out2", etc. to
46: # avoid clashing with bash's argument names "$1", "$2", etc.
47: if [[ -n "$SIMPLE" ]]
48: then
49: clusterContent "$CLUSTER"
50: else
51: outPath=$(eval echo "\$out$CLUSTER")
52:
53: # Store the cluster's content at this path
54: clusterContent "$CLUSTER" > "$outPath"
55: fi
56: done | postProcess
57: '';
58: };
59:
60: format = clusterCount: clusters:
61: let cCount = fromJSON clusterCount;
62: result = runCommand "format" (withNix {
63: inherit clusters;
64: clCount = toString clusterCount;
65: outputs = map (n: "out" + toString n)
66: (range 1 cCount); })
67: script;
68:
69: wrapped = map (n: result."out${toString n}") (range 1 cCount);
70: in assert isList wrapped;
71: assert isString clusterCount;
72: assert isInt cCount;
73: wrapped;
74: }
Generated by git2html.