From 697a9073501b811da744ad19342a14d7347d1f4a Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Tue, 11 Jul 2023 19:12:16 +0200 Subject: [PATCH] klinfix: new commands show log [FILE] and follow [FILE] With completion support. The log command has an optional last arg, after FILE, tail NUM, which runs tail -n NUM FILE instead of cat. Signed-off-by: Joachim Wiberg --- src/klinfix/src/klinfix.c | 34 ++++++++++++++++++++++++++++++++ src/klinfix/xml/infix.xml | 41 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) diff --git a/src/klinfix/src/klinfix.c b/src/klinfix/src/klinfix.c index 585a9a81..198e823c 100644 --- a/src/klinfix/src/klinfix.c +++ b/src/klinfix/src/klinfix.c @@ -1,5 +1,9 @@ #include +#include +#include #include +#include +#include #include #include @@ -13,6 +17,35 @@ const uint8_t kplugin_klinfix_major = 1; const uint8_t kplugin_klinfix_minor = 0; +int klix_files(kcontext_t *ctx) +{ + const char *path; + struct dirent *d; + DIR *dir; + + path = kcontext_script(ctx); + if (!path) { + fprintf(stderr, "Error: missing path argument to file search.\n"); + return -1; + } + + dir = opendir(path); + if (!dir) { + fprintf(stderr, "Error: %s", strerror(errno)); + return -1; + } + + while ((d = readdir(dir))) { + if (d->d_type != DT_REG) + continue; + + printf("%s\n", d->d_name); + } + closedir(dir); + + return 0; +} + int klix_ds_from_str(const char *text, sr_datastore_t *ds) { size_t len = strlen(text); @@ -219,6 +252,7 @@ int kplugin_klinfix_init(kcontext_t *ctx) kplugin_add_syms(plugin, ksym_new("klix_copy", klix_copy)); kplugin_add_syms(plugin, ksym_new("klix_commit", klix_commit)); + kplugin_add_syms(plugin, ksym_new("klix_files", klix_files)); kplugin_add_syms(plugin, ksym_new("klix_rpc", klix_rpc)); return 0; diff --git a/src/klinfix/xml/infix.xml b/src/klinfix/xml/infix.xml index ee912fb3..029ed34a 100644 --- a/src/klinfix/xml/infix.xml +++ b/src/klinfix/xml/infix.xml @@ -87,6 +87,12 @@ + + + /var/log + + + @@ -220,9 +226,44 @@ + + + + + + + + + file=${KLISH_PARAM_fn:-syslog} + if [ -n "$KLISH_PARAM_lines" ]; then + tail -n $KLISH_PARAM_lines /log/$file + else + cat /log/$file + fi + + + + + cat /cfg/factory-config.cfg | jq . + + + + + + cat /cfg/startup-config.cfg | jq . + + + + + + + file=${KLISH_PARAM_fn:-syslog} + echo -e "\e[1mPress Ctrl-C to abort ────────────────────────────────────────────\e[0m" + tail -F /log/$file +