path_exists: fix colors (+param struct)

This commit is contained in:
Michael Stapelberg 2021-11-02 21:05:52 +01:00
parent 7d613fbe95
commit b20491cb6b
3 changed files with 35 additions and 13 deletions

View File

@ -769,7 +769,16 @@ int main(int argc, char *argv[]) {
CASE_SEC_TITLE("path_exists") {
SEC_OPEN_MAP("path_exists");
print_path_exists(json_gen, buffer, title, cfg_getstr(sec, "path"), cfg_getstr(sec, "format"), cfg_getstr(sec, "format_down"));
path_exists_ctx_t ctx = {
.json_gen = json_gen,
.buf = buffer,
.buflen = sizeof(buffer),
.title = title,
.path = cfg_getstr(sec, "path"),
.format = cfg_getstr(sec, "format"),
.format_down = cfg_getstr(sec, "format_down"),
};
print_path_exists(&ctx);
SEC_CLOSE_MAP;
}

View File

@ -298,7 +298,18 @@ typedef struct {
void print_run_watch(run_watch_ctx_t *ctx);
void print_path_exists(yajl_gen json_gen, char *buffer, const char *title, const char *path, const char *format, const char *format_down);
typedef struct {
yajl_gen json_gen;
char *buf;
const size_t buflen;
const char *title;
const char *path;
const char *format;
const char *format_down;
} path_exists_ctx_t;
void print_path_exists(path_exists_ctx_t *ctx);
void print_cpu_temperature_info(yajl_gen json_gen, char *buffer, int zone, const char *path, const char *format, const char *format_above_threshold, int);
void print_cpu_usage(yajl_gen json_gen, char *buffer, const char *format, const char *format_above_threshold, const char *format_above_degraded_threshold, const char *path, const float max_threshold, const float degraded_threshold);

View File

@ -10,19 +10,20 @@
#define STRING_SIZE 5
void print_path_exists(yajl_gen json_gen, char *buffer, const char *title, const char *path, const char *format, const char *format_down) {
void print_path_exists(path_exists_ctx_t *ctx) {
const char *walk;
char *outwalk = buffer;
char *outwalk = ctx->buf;
struct stat st;
const bool exists = (stat(path, &st) == 0);
const bool exists = (stat(ctx->path, &st) == 0);
#define json_gen ctx->json_gen
if (exists || format_down == NULL) {
walk = format;
if (exists || ctx->format_down == NULL) {
walk = ctx->format;
} else {
walk = format_down;
walk = ctx->format_down;
}
INSTANCE(path);
INSTANCE(ctx->path);
START_COLOR((exists ? "color_good" : "color_bad"));
@ -31,13 +32,14 @@ void print_path_exists(yajl_gen json_gen, char *buffer, const char *title, const
snprintf(string_status, STRING_SIZE, "%s", (exists ? "yes" : "no"));
placeholder_t placeholders[] = {
{.name = "%title", .value = title},
{.name = "%title", .value = ctx->title},
{.name = "%status", .value = string_status}};
const size_t num = sizeof(placeholders) / sizeof(placeholder_t);
buffer = format_placeholders(walk, &placeholders[0], num);
char *formatted = format_placeholders(walk, &placeholders[0], num);
OUTPUT_FORMATTED;
free(formatted);
END_COLOR;
OUTPUT_FULL_TEXT(buffer);
free(buffer);
OUTPUT_FULL_TEXT(ctx->buf);
}