Add local_command function to run commands locally

This commit is contained in:
Gianni Ciolli
2016-07-15 19:47:49 +02:00
parent 89aeccedc2
commit 1f3e937bbe

View File

@@ -130,6 +130,7 @@ static void exit_with_errors(void);
static void print_error_list(ItemList *error_list, int log_level);
static bool remote_command(const char *host, const char *user, const char *command, PQExpBufferData *outputbuf);
static bool local_command(const char *command, PQExpBufferData *outputbuf);
static void format_db_cli_params(const char *conninfo, char *output);
static bool copy_file(const char *old_filename, const char *new_filename);
@@ -5970,6 +5971,37 @@ remote_command(const char *host, const char *user, const char *command, PQExpBuf
}
/*
* Execute a command locally.
*/
static bool
local_command(const char *command, PQExpBufferData *outputbuf)
{
FILE *fp;
char output[MAXLEN];
fp = popen(command, "r");
if (fp == NULL)
{
log_err(_("unable to execute local command:\n%s\n"), command);
return false;
}
/* TODO: better error handling */
while (fgets(output, MAXLEN, fp) != NULL)
{
appendPQExpBuffer(outputbuf, "%s", output);
}
pclose(fp);
log_verbose(LOG_DEBUG, "local_command(): output returned was:\n%s", outputbuf->data);
return true;
}
/*
* Extract values from provided conninfo string and return
* formatted as command-line parameters suitable for passing to repmgr