summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHiltjo Posthuma <hiltjo@codemadness.org>2021-01-25 19:35:02 +0100
committerHiltjo Posthuma <hiltjo@codemadness.org>2021-01-27 15:24:02 +0100
commit4d9f922c8396bada73fb0b1e318c8b947f0f606b (patch)
tree2a607d68bfde072dbe3ca910a14677d61308fe0c
parentddda48dac8e373f9bc0884ffeb84605925d988e5 (diff)
sfeed_update: allow to reuse the code more easily as an included script
This adds a main() function. When the environment variable $SFEED_UPDATE_INCLUDE is set then it will not execute the main handler. The other functions are included and can be reused. This is also useful for unit-testing.
-rwxr-xr-xsfeed_update50
-rw-r--r--sfeed_update.111
2 files changed, 37 insertions, 24 deletions
diff --git a/sfeed_update b/sfeed_update
index 9a729e4..7d11f18 100755
--- a/sfeed_update
+++ b/sfeed_update
@@ -186,26 +186,30 @@ feeds() {
echo "See sfeedrc.example for an example." >&2
}
-# job counter.
-curjobs=0
-# signal number received for parent.
-signo=0
-# SIGINT: signal to interrupt parent.
-trap -- "sighandler 2" "INT"
-# SIGTERM: signal to terminate parent.
-trap -- "sighandler 15" "TERM"
-# load config file.
-loadconfig "$1"
-# fetch feeds and store in temporary directory.
-sfeedtmpdir="$(mktemp -d '/tmp/sfeed_XXXXXX')"
-# make sure path exists.
-mkdir -p "${sfeedpath}"
-# fetch feeds specified in config file.
-feeds
-# wait till all feeds are fetched (concurrently).
-[ ${signo} -eq 0 ] && wait
-# cleanup temporary files etc.
-cleanup
-# on signal SIGINT and SIGTERM exit with signal number + 128.
-[ ${signo} -ne 0 ] && exit $((signo+128))
-exit 0
+main() {
+ # job counter.
+ curjobs=0
+ # signal number received for parent.
+ signo=0
+ # SIGINT: signal to interrupt parent.
+ trap -- "sighandler 2" "INT"
+ # SIGTERM: signal to terminate parent.
+ trap -- "sighandler 15" "TERM"
+ # load config file.
+ loadconfig "$1"
+ # fetch feeds and store in temporary directory.
+ sfeedtmpdir="$(mktemp -d '/tmp/sfeed_XXXXXX')"
+ # make sure path exists.
+ mkdir -p "${sfeedpath}"
+ # fetch feeds specified in config file.
+ feeds
+ # wait till all feeds are fetched (concurrently).
+ [ ${signo} -eq 0 ] && wait
+ # cleanup temporary files etc.
+ cleanup
+ # on signal SIGINT and SIGTERM exit with signal number + 128.
+ [ ${signo} -ne 0 ] && exit $((signo+128))
+ exit 0
+}
+
+[ "${SFEED_UPDATE_INCLUDE}" = "1" ] || main "$@"
diff --git a/sfeed_update.1 b/sfeed_update.1
index 41a4870..552c81d 100644
--- a/sfeed_update.1
+++ b/sfeed_update.1
@@ -1,4 +1,4 @@
-.Dd September 19, 2020
+.Dd January 24, 2021
.Dt SFEED_UPDATE 1
.Os
.Sh NAME
@@ -44,6 +44,15 @@ The
script merges new items with this file.
The feedname cannot contain '/' characters, they will be replaced with '_'.
.El
+.Sh ENVIRONMENT VARIABLES
+.Bl -tag -width Ds
+.It SFEED_UPDATE_INCLUDE
+When set
+.Nm
+can be sourced as a script, but it won't run the
+.Fn main
+entry-point.
+.El
.Sh EXIT STATUS
.Ex -std
.Sh EXAMPLES