add jq
authorAJ ONeal <aj@therootcompany.com>
Fri, 3 Jul 2020 06:17:35 +0000 (06:17 +0000)
committerAJ ONeal <aj@therootcompany.com>
Fri, 3 Jul 2020 06:20:00 +0000 (06:20 +0000)
jq/README.md [new file with mode: 0644]
jq/install.ps1 [new file with mode: 0644]
jq/install.sh [new file with mode: 0644]
jq/releases.js [new file with mode: 0644]

diff --git a/jq/README.md b/jq/README.md
new file mode 100644 (file)
index 0000000..0923289
--- /dev/null
@@ -0,0 +1,114 @@
+---
+title: jq
+homepage: https://stedolan.github.io/jq/
+tagline: |
+  jq is a lightweight and flexible command-line JSON processor.
+description: |
+  `jq` is like `sed` for JSON data - you can use it to slice and filter and map and transform structured data with the same ease that `sed`, `awk`, `grep` and friends let you play with text.
+---
+
+All jq selectors begin with `.` - don't forget that!
+
+Be sure to checkout the
+[official tutorial](https://stedolan.github.io/jq/tutorial/) and
+[jq manual](https://stedolan.github.io/jq/manual/) for more info.
+
+You can also [try online](https://jqplay.org/).
+
+### How to select a single a property from an object
+
+```bash
+echo '{ "name": "foo" }' | jq '.name'
+```
+
+```txt
+"foo"
+```
+
+### How to remove quotes from strings
+
+The `-r` or `--raw-output` flag unwraps strings:
+
+```bash
+echo '{ "name": "foo" }' | jq -r '.name'
+```
+
+```txt
+"foo"
+```
+
+### How to select a whole object
+
+```bash
+echo '{ "name": "foo" }' | jq '.'
+```
+
+```txt
+{
+  "name": "foo"
+}
+```
+
+### How to select an element from an array
+
+```bash
+echo '[ { "name": "foo" } ]' | jq '.[0]'
+```
+
+```txt
+{
+  "name": "foo"
+}
+```
+
+### How to select a single property from an array element
+
+```bash
+echo '[ { "name": "foo" } ]' | jq -r '.[0].name'
+```
+
+```txt
+foo
+```
+
+### How to select some properties from multiple elements
+
+```bash
+echo '[ { "name": "foo" }, { "name": "bar" } ]' \
+    | jq -r '.[].name'
+```
+
+```txt
+foo
+bar
+```
+
+### How transform or zip an array
+
+Anything that doesn't start with a `.` is part of the transformation template.
+
+Anything that collects starts with `.[]`.
+
+Anything that transforms has a pipe and selector `| .whatever`.
+
+Be sure to checkout the
+[official tutorial](https://stedolan.github.io/jq/tutorial/) and
+[jq manual](https://stedolan.github.io/jq/manual/) for more info.
+
+```bash
+echo '[ { "name": "foo", "age": 0 }, { "name": "bar", "age": 2 } ]' \
+    | jq '{ names: [.[] | .name], ages: [.[] | .age] }'
+```
+
+```txt
+{
+  "names": [
+    "foo",
+    "bar"
+  ],
+  "ages": [
+    0,
+    2
+  ]
+}
+```
diff --git a/jq/install.ps1 b/jq/install.ps1
new file mode 100644 (file)
index 0000000..e9d2aba
--- /dev/null
@@ -0,0 +1,41 @@
+#!/usr/bin/env pwsh
+
+$VERNAME = "$Env:PKG_NAME-v$Env:WEBI_VERSION.exe"
+$EXENAME = "$Env:PKG_NAME.exe"
+# Fetch archive
+IF (!(Test-Path -Path "$Env:USERPROFILE\Downloads\$Env:WEBI_PKG_FILE"))
+{
+    # TODO: arch detection
+    echo "Downloading $Env:PKG_NAME from $Env:WEBI_PKG_URL to $Env:USERPROFILE\Downloads\$Env:WEBI_PKG_FILE"
+    & curl.exe -A "$Env:WEBI_UA" -fsSL "$Env:WEBI_PKG_URL" -o "$Env:USERPROFILE\Downloads\$Env:WEBI_PKG_FILE.part"
+    & move "$Env:USERPROFILE\Downloads\$Env:WEBI_PKG_FILE.part" "$Env:USERPROFILE\Downloads\$Env:WEBI_PKG_FILE"
+}
+
+IF (!(Test-Path -Path "$Env:USERPROFILE\.local\xbin\$VERNAME"))
+{
+    echo "Installing $Env:PKG_NAME"
+    # TODO: temp directory
+
+    # Enter tmp
+    pushd .local\tmp
+
+        # Remove any leftover tmp cruft
+        Remove-Item -Path "$Env:PKG_NAME-v*" -Recurse -ErrorAction Ignore
+
+        # Move single binary into temporary folder
+        echo "Moving $Env:USERPROFILE\Downloads\$Env:WEBI_PKG_FILE"
+        & move "$Env:USERPROFILE\Downloads\$Env:WEBI_PKG_FILE" "$VERNAME"
+
+        # Settle unpacked archive into place
+        echo "New Name: $VERNAME"
+        Get-ChildItem "$Env:PKG_NAME-v*" | Select -f 1 | Rename-Item -NewName "$VERNAME"
+        echo "New Location: $Env:USERPROFILE\.local\xbin\$VERNAME"
+        Move-Item -Path "$VERNAME" -Destination "$Env:USERPROFILE\.local\xbin"
+
+    # Exit tmp
+    popd
+}
+
+echo "Copying into '$Env:USERPROFILE\.local\bin\$EXENAME' from '$Env:USERPROFILE\.local\xbin\$VERNAME'"
+Remove-Item -Path "$Env:USERPROFILE\.local\bin\$EXENAME" -Recurse -ErrorAction Ignore
+Copy-Item -Path "$Env:USERPROFILE\.local\xbin\$VERNAME" -Destination "$Env:USERPROFILE\.local\bin\$EXENAME" -Recurse
diff --git a/jq/install.sh b/jq/install.sh
new file mode 100644 (file)
index 0000000..1bb6cf0
--- /dev/null
@@ -0,0 +1,18 @@
+{
+    set -e
+    set -u
+
+    ##############
+    # Install jq #
+    ##############
+
+    WEBI_SINGLE=true
+
+    pkg_get_current_version() {
+      # 'jq --version' has output in this format:
+      #       jq-1.6
+      # This trims it down to just the version number:
+      #       1.6
+      echo $(jq --version 2>/dev/null | head -n 1 | sed 's:^jq-::')
+    }
+}
diff --git a/jq/releases.js b/jq/releases.js
new file mode 100644 (file)
index 0000000..001702a
--- /dev/null
@@ -0,0 +1,23 @@
+'use strict';
+
+var github = require('../_common/github.js');
+var owner = 'stedolan';
+var repo = 'jq';
+
+module.exports = function (request) {
+  return github(request, owner, repo).then(function (all) {
+    all.releases.forEach(function (rel) {
+      console.log(rel.version);
+      rel.version = String(rel.version).replace(/^jq\-/, '');
+    });
+    return all;
+  });
+};
+
+if (module === require.main) {
+  module.exports(require('@root/request')).then(function (all) {
+    all = require('../_webi/normalize.js')(all);
+    console.info(JSON.stringify(all));
+    //console.info(JSON.stringify(all, null, 2));
+  });
+}