From 2f8c462c0f16230e9aa7e099cfc5dc36d80912e0 Mon Sep 17 00:00:00 2001 From: Tiiffi Date: Fri, 16 Dec 2016 21:13:01 +0200 Subject: [PATCH] Added support for setting basic options with environment variables. --- README.md | 6 ++++++ mcrcon.1 | 12 +++++++++++- mcrcon.c | 20 ++++++++++++++------ 3 files changed, 31 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index f59367a..508b952 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,12 @@ Option: -v Output version information ``` +Server address, port and password can be set using following environment variables: + MCRCON_HOST + MCRCON_PORT + MCRCON_PASS + +Command-line options will override environment variables. Commands with arguments must be enclosed in quotes. Example: diff --git a/mcrcon.1 b/mcrcon.1 index 0a51872..b11f5d2 100644 --- a/mcrcon.1 +++ b/mcrcon.1 @@ -1,7 +1,7 @@ .\" Process this file with .\" groff -man -Tascii mcrcon.1 .\" -.TH MCRCON 1 "November 2016" "Version 0.0.6" +.TH MCRCON 1 "December 2016" "Version 0.0.6" .SH NAME mcrcon \- sends rcon commands to a Minecraft server .SH SYNOPSIS @@ -34,6 +34,16 @@ Output raw packets (for debugging and custom handling) Output version information .PP Commands with arguments must be enclosed in quotes. +.SH ENVIRONMENTAL VARIABLES +Server address, port and password can be set using following environment variables: +.PP +\fBMCRCON_HOST +.br +\fBMCRCON_PORT +.br +\fBMCRCOM_PASS\fR +.PP +Note that command-line options will override environment variables. .SH EXAMPLES Make rcon connection in terminal mode using default port .RS diff --git a/mcrcon.c b/mcrcon.c index 7263e3e..77c626a 100644 --- a/mcrcon.c +++ b/mcrcon.c @@ -140,11 +140,14 @@ int main(int argc, char *argv[]) int opt, ret = 0; int terminal_mode = 0; - char *host = NULL; - char *pass = NULL; - char *port = "25575"; + char *host = getenv("MCRCON_HOST"); + char *pass = getenv("MCRCON_PASS"); + char *port = getenv("MCRCON_PORT"); + + if (!port) + port = "25575"; - if(argc < 2) + if(argc < 2 && host == NULL && pass == NULL) usage(); // default getopt error handler enabled @@ -247,11 +250,16 @@ void usage(void) " -s\t\tSilent mode (do not print received packets)\n" " -c\t\tDisable colors\n" " -r\t\tOutput raw packets (debugging and custom handling)\n" - " -v\t\tOutput version information\n" + " -v\t\tOutput version information\n\n" + "Server address, port and password can be set using following environment variables:\n" + " MCRCON_HOST\n" + " MCRCON_PORT\n" + " MCRCON_PASS\n\n" ,stdout ); - puts("\nCommands with arguments must be enclosed in quotes.\n"); + puts("Command-line options will override environment variables."); + puts("Rcon commands with arguments must be enclosed in quotes.\n"); puts("Example:\n\t"IN_NAME" -H my.minecraft.server -p password \"say Server is restarting!\" save-all stop\n"); puts(VER_STR"\nReport bugs to tiiffi_at_gmail_dot_com or https://github.com/Tiiffi/mcrcon/issues/\n");