feat: add xsv (query CSV like SQL)

This commit is contained in:
Don Johnson
2023-10-16 23:17:32 +00:00
committed by AJ ONeal
parent aa616be917
commit b138dd5ef2
4 changed files with 186 additions and 0 deletions
+58
View File
@@ -0,0 +1,58 @@
---
title: xsv
homepage: https://github.com/BurntSushi/xsv
tagline: |
xsv: A fast CSV command line toolkit written in Rust.
---
To update or switch versions, run `webi xsv@stable` (or `@v2`, `@beta`, etc).
### Files
These are the files / directories that are created and/or modified with this
install:
```text
~/.config/envman/PATH.env
~/.local/bin/xsv
~/.local/opt/xsv
```
## Cheat Sheet
> `xsv` is a command line program for manipulating CSV files. It offers a range
> of functionalities including slicing, joining, indexing and more. Designed for
> simplicity and speed, it is an essential tool for anyone working with CSV
> data.
### Basic Usage
To count the number of rows in a CSV file:
```sh
xsv count data.csv
```
### Joining Two CSV Files
To perform an inner join on two CSV files based on a common column:
```sh
xsv join column1 file1.csv column2 file2.csv
```
### Sample Data
To randomly sample rows from a CSV file:
```sh
xsv sample 100 data.csv
```
### Analyzing Data
To display basic statistics for each column in a CSV file:
```sh
xsv stats data.csv
```
+62
View File
@@ -0,0 +1,62 @@
#!/usr/bin/env pwsh
###############
# Install xsv #
###############
# Every package should define these variables
$pkg_cmd_name = "xsv"
$pkg_dst_cmd = "$Env:USERPROFILE\.local\bin\xsv.exe"
$pkg_dst_bin = "$Env:USERPROFILE\.local\bin"
$pkg_dst = "$pkg_dst_cmd"
$pkg_src_cmd = "$Env:USERPROFILE\.local\opt\xsv-v$Env:WEBI_VERSION\bin\xsv.exe"
$pkg_src_bin = "$Env:USERPROFILE\.local\opt\xsv-v$Env:WEBI_VERSION\bin"
$pkg_src_dir = "$Env:USERPROFILE\.local\opt\xsv-v$Env:WEBI_VERSION"
$pkg_src = "$pkg_src_cmd"
New-Item "$Env:USERPROFILE\Downloads\webi" -ItemType Directory -Force | out-null
$pkg_download = "$Env:USERPROFILE\Downloads\webi\$Env:WEBI_PKG_FILE"
# Fetch archive
IF (!(Test-Path -Path "$Env:USERPROFILE\Downloads\webi\$Env:WEBI_PKG_FILE"))
{
echo "Checking for (or Installing) MSVC Runtime..."
& "$Env:USERPROFILE\.local\bin\webi-pwsh.ps1" vcruntime
echo "Downloading xsv from $Env:WEBI_PKG_URL to $pkg_download"
& curl.exe -A "$Env:WEBI_UA" -fsSL "$Env:WEBI_PKG_URL" -o "$pkg_download.part"
& move "$pkg_download.part" "$pkg_download"
}
IF (!(Test-Path -Path "$pkg_src_cmd"))
{
echo "Installing xsv"
# TODO: create package-specific temp directory
# Enter tmp
pushd .local\tmp
# Remove any leftover tmp cruft
Remove-Item -Path ".\xsv-v*" -Recurse -ErrorAction Ignore
Remove-Item -Path ".\xsv.exe" -Recurse -ErrorAction Ignore
# Unpack archive file into this temporary directory
# Windows BSD-tar handles zip. Imagine that.
echo "Unpacking $pkg_download"
& tar xf "$pkg_download"
# Settle unpacked archive into place
echo "Install Location: $pkg_src_cmd"
New-Item "$pkg_src_bin" -ItemType Directory -Force | out-null
Move-Item -Path ".\xsv.exe" -Destination "$pkg_src_bin"
# Exit tmp
popd
}
echo "Copying into '$pkg_dst_cmd' from '$pkg_src_cmd'"
Remove-Item -Path "$pkg_dst_cmd" -Recurse -ErrorAction Ignore | out-null
New-Item "$pkg_dst_bin" -ItemType Directory -Force | out-null
Copy-Item -Path "$pkg_src" -Destination "$pkg_dst" -Recurse
+46
View File
@@ -0,0 +1,46 @@
#!/bin/sh
# shellcheck disable=SC2034
# "'pkg_cmd_name' appears unused. Verify it or export it."
__init_xsv() {
set -e
set -u
##################
# Install xsv #
##################
# Every package should define these 6 variables
pkg_cmd_name="xsv"
pkg_dst_cmd="$HOME/.local/bin/xsv"
pkg_dst="$pkg_dst_cmd"
pkg_src_cmd="$HOME/.local/opt/xsv-v$WEBI_VERSION/bin/xsv"
pkg_src_dir="$HOME/.local/opt/xsv-v$WEBI_VERSION"
pkg_src="$pkg_src_cmd"
# pkg_install must be defined by every package
pkg_install() {
# ~/.local/opt/xsv-v0.99.9/bin
mkdir -p "$(dirname "${pkg_src_cmd}")"
# mv ./xsv-*/xsv ~/.local/opt/xsv-v0.99.9/bin/xsv
mv ./xsv "${pkg_src_cmd}"
}
# pkg_get_current_version is recommended, but not required
pkg_get_current_version() {
# 'xsv --version' has output in this format:
# xsv 0.99.9 (rev abcdef0123)
# This trims it down to just the version number:
# 0.99.9
xsv --version 2> /dev/null |
head -n 1 |
cut -d ' ' -f 2
}
}
__init_xsv
+20
View File
@@ -0,0 +1,20 @@
'use strict';
var github = require('../_common/github.js');
var owner = 'BurntSushi';
var repo = 'xsv';
module.exports = function (request) {
return github(request, owner, repo).then(function (all) {
return all;
});
};
if (module === require.main) {
module.exports(require('@root/request')).then(function (all) {
all = require('../_webi/normalize.js')(all);
// just select the first 5 for demonstration
all.releases = all.releases.slice(0, 5);
console.info(JSON.stringify(all, null, 2));
});
}