j5

aditionnal tools for the s6 service manager
Log | Files | Refs | README

commit 3489cdb67c8e4cccb70713b052d9291ef6d8a8b7
parent 1ad5933ced8f15c613c3a5cf95c82da3ef67c293
Author: Josuah Demangeon <mail@josuah.net>
Date:   Sun, 27 Jan 2019 00:11:47 +0100

Add documentation: man page and readme

Diffstat:
MMakefile | 2++
MREADME | 34++++++++++++++++++++++++----------
Aj5-envdir.1 | 91+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mj5-envdir.c | 48+++++++++++++++++++++++++++++-------------------
4 files changed, 146 insertions(+), 29 deletions(-)

diff --git a/Makefile b/Makefile @@ -12,6 +12,8 @@ clean: install: all mkdir -p ${PREFIX}/bin cp ${bin} ${PREFIX}/bin + mkdir -p ${PREFIX}/share/man/man1 + cp *.1 ${PREFIX}/share/man/man1 j5-envdir: j5-envdir.o ${obj} ./make-bin $@ j5-envdir.o ${obj} diff --git a/README b/README @@ -1,14 +1,28 @@ -import PACK_LOG -import PACK_DEF -import PACK_OUT +j5 - s6 extensions +================== -export PREFIX -export one variable per dependency with all non-alnum replaced by '_'. +j5 is set of tools to provide tiny parts that I needed and s6 does not provide. -copy ${PACK_DEF}/${pack}/${version}/data and ${PACK_DEF}/${pack}/default/data - to ${PACK_OUT}/ -read ${PACK_DEF}/${pack}/default/version +j5-envdir +--------- -read ${PACK_LOG}/${pack}/${version}/dependencies -read ${PACK_LOG}/${pack}/default/dependencies +[s6-envdir][1] exports environment variables according to the content of an +envdir: every file name becomes a variable name, whose value gets the content +of the file. + +This conveniently stores state of variables on the filesystem and permit to +programatically write environment variables before a daemon start. + +[execline][2] comes with multisubstitute and importas which permit to substitute +command line argument with environment variable content like in a shell scipt. +This however require to add an importas statement for each variable to +substitute. Substituting *all* arguments is not desireable neither, as environment +variables comes from various sources. + +Therefore, j5-envdir is the program that handles variables sent to a daemon before it +starts: it subsitute them directly on the command line and on a configuration file +as they get found from the envdir directly instead of doing it by hand with importas. + +[1]: https://skarnet.org/software/s6/s6-envdir.html +[2]: https://skarnet.org/software/execline/importas.html diff --git a/j5-envdir.1 b/j5-envdir.1 @@ -0,0 +1,91 @@ +.Dt J5-ENVDIR 1 +.Dd $Mdocdate: January 26 2019$ +.Os +. +. +.Sh NAME +. +.Nm j5-envdir +.Nd substitute variables given the content of a directory +. +. +.Sh SYNOPSIS +. +.Nm j5-envdir +.Op Fl a +.Op Fl f Ar file +.Op Fl n Ar chomp +.Op Fl p Ar prefix +.Op Fl s Ar suffix +.Ar dir +.Ar cmd +.Op Ar arguments ... +. +. +.Sh DESCRIPTION +. +The +.Nm +utility reads +.Ar dir +and for every file (or symlink to a file), it perform substitution for +variables whose key is the name of the file, and whose value is the content of +the file. +. +.Pp +It then execute the rest of the file +. +.Bl -tag -width 6n +. +.It Fl a +Perform substitution on command line arguments. +. +.It Fl f Ar file +Perform substitution on the content of +.Ar file +replacing it in-place. +. +.It Fl n Ar chomp +Strip the trailing characters if these are among the +.Ar chmop +string. +By default, the characters are newline, space and tab. +. +.It Fl p Ar prefix +Set the prefix that start a variable occurence. +By default, the prefix is +.Dq ${ . +. +.It Fl s Ar suffix +Set the suffix that end a variable occurence. +By default, the suffix is +.Dq } . +. +.El +. +. +.Sh EXIT STATUS +. +.Ex -std +. +. +.Sh EXAMPLES +. +Substitute all variables given from the +.Pa env +directory in both the file +.Pa config +and in command line arguments then start a server: +. +.Dl $ j5-envdir -a -f config env somethingd -fla -g '${GROUP}' +. +. +.Sh SEE ALSO +. +.Lk http://skarnet.org/software/s6/s6-envdir.html "s6: the s6-envdir program" +. +. +.Sh AUTHORS +. +.An Josuah Demangeon +.Aq Mt mail@josuah.net diff --git a/j5-envdir.c b/j5-envdir.c @@ -2,14 +2,15 @@ #include <dirent.h> #include <unistd.h> -#include "open.h" +#include "arg.h" +#include "env.h" +#include "genalloc.h" #include "log.h" -#include "str.h" #include "mem.h" -#include "arg.h" +#include "open.h" #include "stat.h" +#include "str.h" #include "stralloc.h" -#include "genalloc.h" char *arg_0; @@ -30,8 +31,6 @@ usage(char *progname) log_usage(progname, "[-a] [-f file] [-p prefix] [-s suffix] [-n stripchars] envdir cmd [arg...]"); } -#include <stdio.h> - int replace(stralloc *sa, char const *src, size_t siz, char const *pre, genalloc const *ga, char const *suf) { @@ -49,13 +48,13 @@ replace(stralloc *sa, char const *src, size_t siz, char const *pre, genalloc con siz -= i + len_pre; for (size_t k = 0; k < genalloc_len(ga, keyval); ++k) { - keyval *a = genalloc_get(ga, keyval, k); - size_t len_key = str_len(a->key); + keyval *kv = genalloc_get(ga, keyval, k); + size_t len_key = str_len(kv->key); if (siz < len_key + len_suf) continue; if (mem_diff(src + len_key, suf, len_suf)) continue; - if (mem_equal(src, a->key, len_key)) { - if (!stralloc_cat(sa, &a->val)) goto error; + if (mem_equal(src, kv->key, len_key)) { + if (!stralloc_cat(sa, &kv->val)) goto error; siz -= len_key + len_suf; src += len_key + len_suf; found = 1; @@ -80,19 +79,21 @@ main(int argc, char **argv) { DIR *dp; struct dirent *de; - char *pre = "${"; - char *suf = "}"; + char *flag_p = "${"; + char *flag_s = "}"; + int flag_a = 0; + int flag_e = 0; char *flag_f = NULL; char *flag_n = " \t\n"; - int flag_a = 0; genalloc ga; ARG_BEGIN { case 'a': flag_a = 1; break; + case 'e': flag_e = 1; break; case 'f': flag_f = ARG; break; case 'n': flag_n = ARG; break; - case 'p': pre = ARG; break; - case 's': suf = ARG; break; + case 'p': flag_p = ARG; break; + case 's': flag_s = ARG; break; default: usage(arg_0); break; } ARG_END; if (!*argv) usage(arg_0); @@ -119,7 +120,7 @@ main(int argc, char **argv) if (flag_a) { for (char **av = argv; *av; ++av) { stralloc sa = STRALLOC_ZERO; - if (!replace(&sa, *av, str_len(*av), pre, &ga, suf)) die_nomem(); + if (!replace(&sa, *av, str_len(*av), flag_p, &ga, flag_s)) die_nomem(); if (!stralloc_cat0(&sa)) die_nomem(); *av = sa.x; } @@ -130,14 +131,23 @@ main(int argc, char **argv) stralloc dst = STRALLOC_ZERO; if (!open_readclose(flag_f, &src, 1024)) log_fatal_sys_2(102, "open read close ", flag_f); - if (!replace(&dst, src.x, src.n, pre, &ga, suf)) die_nomem(); + if (!replace(&dst, src.x, src.n, flag_p, &ga, flag_s)) die_nomem(); if (!open_writeclose(flag_f, dst.x, dst.n)) log_fatal_sys_2(102, "open write close ", flag_f); } + if (flag_e) { + for (size_t i = 0; i < genalloc_len(&ga, keyval); ++i) { + keyval *kv = genalloc_get(&ga, keyval, i); + if (!stralloc_cat0(&kv->val)) die_nomem(); + kv->val.n--; + if (!env_set(kv->key, kv->val.x)) die_nomem(); + } + } + for (size_t i = 0; i < genalloc_len(&ga, keyval); ++i) { - keyval *a = genalloc_get(&ga, keyval, i); - stralloc_free(&a->val); + keyval *kv = genalloc_get(&ga, keyval, i); + stralloc_free(&kv->val); } genalloc_free(&ga);