From 29b88760a160dc5db087a6dd973211c20508ba54 Mon Sep 17 00:00:00 2001 From: KatolaZ Date: Mon, 28 Jan 2019 06:14:11 +0000 Subject: new config file on stderr + amend README --- README.md | 49 +++++++++++++++++++++++++++++++++---------------- phrollo | 19 +++---------------- 2 files changed, 36 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index 4b7cb99..1079751 100644 --- a/README.md +++ b/README.md @@ -5,8 +5,8 @@ configuration file and outputs the relevant selectors to be added to a gophermap. The list of selectors of updated phlogs is sorted in decreasing order of update time (meaning that more recent updates appear -at the top of the list). `phrollo` also outputs an updaterd config file, -to be used for future calls. +at the top of the list). `phrollo` also prints on stderr an updaterd +config file, to be used for future calls. `phrollo` detects updates by using `shasum -a 256`, i.e. by computing a hash of the resource and comparing it with the hash obtained when the @@ -15,25 +15,23 @@ resource was last seen. Usage =========== -1) +Either : ``` ./phrollo status.txt ``` - -In this case, `phrollo` will put a new config file in `status.txt.new`, -and output the sorted list of gopher selectors. The config file received -on input will also be saved in `status.txt.bak`. -2) +or: ``` - cat config.txt | ./phrollo + cat status.txt | ./phrollo ``` -In this case, the new config file will be put in `phrollo.out`, and -output the sorted list of gopher selectors. +will print the sorted list of gopher selectors on stdout, and the +updated config file on stderr. IMPORTANT: The next time `phrollo` is run, it must be given the last -config file generated. +config file generated, so be sure to redirect stderr appropriately. A +fully working example is given below. + Configuration file ======================== @@ -71,7 +69,13 @@ them with a "-" dash. Notice that fields are separated by actual TABs: - KatolaZ /~katolaz/phlog republic.circumlunar.space 70 - ``` -Now we can feed this file to `phrollo` to get: +Now we can feed this file to `phrollo`: + +``` +cat config.txt | ./phrollo 2> config_updated.txt > gophermap +``` + +and we will get the following output in `gophermap`: ``` 1(20190126) Alex Shroeder / alexschroeder.ch 70 @@ -83,8 +87,7 @@ Now we can feed this file to `phrollo` to get: ``` which is a list of selectors corresponding to the phlogs specified in -the config file. Moreover, `phrollo` will also create the updated -config file `phrollo.out`: +the config file, and the updated config file in config_updated.txt: ``` 20190126 Tomasino /phlog gopher.black 70 bd141abfc29522e3e2b5d00f1a656212201ae5def60de90a7ce847cddeb6d0db @@ -96,4 +99,18 @@ config file `phrollo.out`: ``` Then, we can run `phrollo` again using the latest updated config file -(i.e., `phrollo.out`) to detect updates. +to detect updates and generate the new gophermap. It is possible to +automate the update procedure by something like: + +``` +#!/bin/sh + +cp config.txt config.txt."$(date +%Y%m%d)" +cat config.txt | ./phrollo 2> config.txt.new > gophermap.new + +[ $? -eq 0 ] && { + mv config.txt.new config.txt + mv gophermap.new gophermap +} + +``` diff --git a/phrollo b/phrollo index 4f6f67b..55fb5d8 100755 --- a/phrollo +++ b/phrollo @@ -3,25 +3,12 @@ # MIT License FILEIN="${1:-/dev/stdin}" -TODAY="$(date +%Y%m%d)" -if [ "${FILEIN}" != '/dev/stdin' ]; then { - cat "${FILEIN}" | tee "${FILEIN}".bak > "${FILEIN}".new - FILEOUT="${FILEIN}".new -} else { - FILEOUT="$(basename $0).out" -} -fi - IFS=' ' while read -r D T S H P SH; do NSH="$(printf "$S\r\n" | nc $H $P | shasum -a 256 | cut -d " " -f 1)" [ $? -eq 0 ] && - [ "${SH}" != "${NSH}" ] && { - D="${TODAY}" - SH="${NSH}" - } - printf "%s\t%s\t%s\t%s\t%s\t%s\n" "$D" "$T" "$S" "$H" "$P" "$SH" -done < "${FILEIN}" | sort -rnk1 -k2 | tee "${FILEOUT}" | \ + [ "${SH}" != "${NSH}" ] && {D="$(date +%Y%m%d)"; SH="${NSH}"} + printf "%s\t%s\t%s\t%s\t%s\t%s\n" "$D" "$T" "$S" "$H" "$P" "$SH" | tee /dev/stderr +done < "${FILEIN}" | sort -rnk1 -k2 | \ sed -r "s/^([0-9][0-9]*) /1(\1) /g;s/ [a-f0-9][a-f0-9]*$//g" |\ sort -rk1 -k2 - -- cgit v1.2.3