Feature: [Add support for -V option to give version](https://github.com/clicon/clixon/issues/472)

This commit is contained in:
Olof hagsand 2023-12-14 22:48:13 +01:00
parent 2f81a086c8
commit 261469be16
11 changed files with 209 additions and 42 deletions

View file

@ -79,7 +79,7 @@
#include "backend_plugin_restconf.h"
/* Command line options to be passed to getopt(3) */
#define BACKEND_OPTS "hD:f:E:l:C:d:p:b:Fza:u:P:1qs:c:U:g:y:o:"
#define BACKEND_OPTS "hVD:f:E:l:C:d:p:b:Fza:u:P:1qs:c:U:g:y:o:"
#define BACKEND_LOGFILE "/usr/local/var/clixon_backend.log"
@ -462,6 +462,7 @@ usage(clicon_handle h,
fprintf(stderr, "usage:%s <options>*\n"
"where options are\n"
"\t-h\t\tHelp\n"
"\t-V \t\tPrint version and exit\n"
"\t-D <level>\tDebug level\n"
"\t-f <file>\tClixon config file\n"
"\t-E <dir> \tExtra configuration file directory\n"
@ -532,6 +533,7 @@ main(int argc,
size_t sz;
int config_dump;
enum format_enum config_dump_format = FORMAT_XML;
int print_version = 0;
/* In the startup, logs to stderr & syslog and debug flag set later */
clicon_log_init(__PROGRAM__, LOG_INFO, logdst);
@ -560,6 +562,10 @@ main(int argc,
*/
help = 1;
break;
case 'V':
cligen_output(stdout, "Clixon version %s\n", CLIXON_VERSION_STRING);
print_version++; /* plugins may also print versions w ca-version callback */
break;
case 'D' : /* debug */
if (sscanf(optarg, "%d", &dbg) != 1)
usage(h, argv[0]);
@ -611,12 +617,13 @@ main(int argc,
while ((c = getopt(argc, argv, BACKEND_OPTS)) != -1)
switch (c) {
case 'h' : /* help */
case 'V' : /* version */
case 'D' : /* debug */
case 'f': /* config file */
case 'E': /* extra config dir */
case 'f' : /* config file */
case 'E' : /* extra config dir */
case 'l' :
break; /* see above */
case 'C': /* Explicitly dump configuration */
case 'C' : /* Explicitly dump configuration */
if ((config_dump_format = format_str2int(optarg)) == (enum format_enum)-1){
fprintf(stderr, "Unrecognized dump format: %s(expected: xml|json|text)\n", argv[0]);
usage(h, argv[0]);
@ -823,7 +830,12 @@ main(int argc,
clixon_plugins_load(h, CLIXON_PLUGIN_INIT, dir,
clicon_option_str(h, "CLICON_BACKEND_REGEXP")) < 0)
goto done;
/* Print version, customized variant must wait for plugins to load */
if (print_version){
if (clixon_plugin_version_all(h, stdout) < 0)
goto done;
exit(0);
}
/* Load Yang modules
* 1. Load a yang module as a specific absolute filename */
if ((str = clicon_yang_main_file(h)) != NULL)

View file

@ -73,8 +73,7 @@
#include "cli_handle.h"
/* Command line options to be passed to getopt(3) */
#define CLI_OPTS "+hD:f:E:l:C:F:1a:u:d:m:qp:GLy:c:U:o:"
#define CLI_OPTS "+hVD:f:E:l:C:F:1a:u:d:m:qp:GLy:c:U:o:"
/*! Check if there is a CLI history file and if so dump the CLI histiry to it
*
* Just log if file does not exist or is not readable
@ -481,6 +480,7 @@ usage(clicon_handle h,
"and extra-options are app-dependent and passed to the plugin init function\n"
"where options are\n"
"\t-h \t\tHelp\n"
"\t-V \t\tPrint version and exit\n"
"\t-D <level> \tDebug level\n"
"\t-f <file> \tConfig-file (mandatory)\n"
"\t-E <dir> \tExtra configuration file directory\n"
@ -532,8 +532,9 @@ main(int argc,
size_t cligen_bufthreshold;
int dbg=0;
int nr;
int config_dump;
int config_dump;
enum format_enum config_dump_format = FORMAT_XML;
int print_version = 0;
/* Defaults */
once = 0;
@ -574,6 +575,10 @@ main(int argc,
*/
help = 1;
break;
case 'V': /* version */
cligen_output(stdout, "Clixon version %s\n", CLIXON_VERSION_STRING);
print_version++; /* plugins may also print versions w ca-version callback */
break;
case 'D' : /* debug */
if (sscanf(optarg, "%d", &dbg) != 1)
usage(h, argv[0]);
@ -616,11 +621,12 @@ main(int argc,
while ((c = getopt(argc, argv, CLI_OPTS)) != -1){
switch (c) {
case 'D' : /* debug */
case 'f': /* config file */
case 'E': /* extra config dir */
case 'l': /* Log destination */
case 'V' : /* version */
case 'f' : /* config file */
case 'E' : /* extra config dir */
case 'l' : /* Log destination */
break; /* see above */
case 'C': /* Explicitly dump configuration */
case 'C' : /* Explicitly dump configuration */
if ((config_dump_format = format_str2int(optarg)) == (enum format_enum)-1){
fprintf(stderr, "Unrecognized dump format: %s(expected: xml|json|text)\n", argv[0]);
usage(h, argv[0]);
@ -766,7 +772,12 @@ main(int argc,
goto done;
}
#endif
/* Print version, customized variant must wait for plugins to load */
if (print_version){
if (clixon_plugin_version_all(h, stdout) < 0)
goto done;
exit(0);
}
/* Add (hardcoded) netconf features in case ietf-netconf loaded here
* Otherwise it is loaded in netconf_module_load below
*/

View file

@ -70,7 +70,7 @@
#include "netconf_rpc.h"
/* Command line options to be passed to getopt(3) */
#define NETCONF_OPTS "hD:f:E:l:C:q01ca:u:d:p:y:U:t:eo:"
#define NETCONF_OPTS "hVD:f:E:l:C:q01ca:u:d:p:y:U:t:eo:"
#define NETCONF_LOGFILE "/tmp/clixon_netconf.log"
@ -637,6 +637,7 @@ usage(clicon_handle h,
fprintf(stderr, "usage:%s\n"
"where options are\n"
"\t-h\t\tHelp\n"
"\t-V \t\tPrint version and exit\n"
"\t-D <level>\tDebug level\n"
"\t-f <file>\tConfiguration file (mandatory)\n"
"\t-E <dir> \tExtra configuration file directory\n"
@ -683,6 +684,7 @@ main(int argc,
size_t sz;
int config_dump = 0;
enum format_enum config_dump_format = FORMAT_XML;
int print_version = 0;
/* Create handle */
if ((h = clicon_handle_init()) == NULL)
@ -702,6 +704,10 @@ main(int argc,
case 'h' : /* help */
usage(h, argv[0]);
break;
case 'V': /* version */
cligen_output(stdout, "Clixon version %s\n", CLIXON_VERSION_STRING);
print_version++; /* plugins may also print versions w ca-version callback */
break;
case 'D' : /* debug */
if (sscanf(optarg, "%d", &dbg) != 1)
usage(h, argv[0]);
@ -743,12 +749,13 @@ main(int argc,
while ((c = getopt(argc, argv, NETCONF_OPTS)) != -1)
switch (c) {
case 'h' : /* help */
case 'V' : /* version */
case 'D' : /* debug */
case 'f': /* config file */
case 'E': /* extra config dir */
case 'l': /* log */
case 'f' : /* config file */
case 'E' : /* extra config dir */
case 'l' : /* log */
break; /* see above */
case 'C': /* Explicitly dump configuration */
case 'C' : /* Explicitly dump configuration */
if ((config_dump_format = format_str2int(optarg)) == (enum format_enum)-1){
fprintf(stderr, "Unrecognized dump format: %s(expected: xml|json|text)\n", argv[0]);
usage(h, argv[0]);
@ -855,7 +862,12 @@ main(int argc,
if ((dir = clicon_netconf_dir(h)) != NULL &&
clixon_plugins_load(h, CLIXON_PLUGIN_INIT, dir, NULL) < 0)
goto done;
/* Print version, customized variant must wait for plugins to load */
if (print_version){
if (clixon_plugin_version_all(h, stdout) < 0)
goto done;
exit(0);
}
/* Load Yang modules
* 1. Load a yang module as a specific absolute filename */
if ((str = clicon_yang_main_file(h)) != NULL){

View file

@ -88,7 +88,7 @@
#include "restconf_stream.h"
/* Command line options to be passed to getopt(3) */
#define RESTCONF_OPTS "hD:f:E:l:C:p:d:y:a:u:rW:R:o:"
#define RESTCONF_OPTS "hVD:f:E:l:C:p:d:y:a:u:rW:R:o:"
/*! Convert FCGI parameters to clixon runtime data
*
@ -270,6 +270,7 @@ usage(clicon_handle h,
fprintf(stderr, "usage:%s [options]\n"
"where options are\n"
"\t-h \t\t Help\n"
"\t-V \t\tPrint version and exit\n"
"\t-D <level>\t Debug level\n"
"\t-f <file>\t Configuration file (mandatory)\n"
"\t-E <dir> \t Extra configuration file directory\n"
@ -321,6 +322,7 @@ main(int argc,
size_t sz;
int config_dump = 0;
enum format_enum config_dump_format = FORMAT_XML;
int print_version = 0;
/* In the startup, logs to stderr & debug flag set later */
clicon_log_init(__PROGRAM__, LOG_INFO, logdst);
@ -336,6 +338,10 @@ main(int argc,
case 'h':
usage(h, argv[0]);
break;
case 'V':
cligen_output(stdout, "Clixon version %s\n", CLIXON_VERSION_STRING);
print_version++; /* plugins may also print versions w ca-version callback */
break;
case 'D' : /* debug */
if (sscanf(optarg, "%d", &dbg) != 1)
usage(h, argv[0]);
@ -391,6 +397,7 @@ main(int argc,
while ((c = getopt(argc, argv, RESTCONF_OPTS)) != -1)
switch (c) {
case 'h' : /* help */
case 'V' : /* version */
case 'D' : /* debug */
case 'f': /* config file */
case 'E': /* extra config dir */
@ -480,6 +487,12 @@ main(int argc,
if ((dir = clicon_restconf_dir(h)) != NULL)
if (clixon_plugins_load(h, CLIXON_PLUGIN_INIT, dir, NULL) < 0)
return -1;
/* Print version, customized variant must wait for plugins to load */
if (print_version){
if (clixon_plugin_version_all(h, stdout) < 0)
goto done;
exit(0);
}
/* Create a pseudo-plugin to create extension callback to set the ietf-routing
* yang-data extension for api-root top-level restconf function.
*/

View file

@ -159,7 +159,7 @@
#endif
/* Command line options to be passed to getopt(3) */
#define RESTCONF_OPTS "hD:f:E:l:C:p:y:a:u:rW:R:o:"
#define RESTCONF_OPTS "hVD:f:E:l:C:p:y:a:u:rW:R:o:"
/* If set, open outwards socket non-blocking, as opposed to blocking
* Should work both ways, but in the ninblocking case,
@ -903,6 +903,7 @@ restconf_openssl_init(clicon_handle h,
* That is, EITHER local config OR read config from backend once
* @param[in] h Clixon handle
* @param[in] inline_config If set, restconf conf is given by -R command-line
* @param[in] print_version If set, print version and exit
* @param[out] xrestconf XML restconf config, malloced (if retval = 1)
* @retval 1 OK (and xrestconf set)
* @retval 0 Fail - no config
@ -911,6 +912,7 @@ restconf_openssl_init(clicon_handle h,
int
restconf_clixon_init(clicon_handle h,
char *inline_config,
int print_version,
cxobj **xrestconfp)
{
int retval = -1;
@ -954,6 +956,12 @@ restconf_clixon_init(clicon_handle h,
if ((dir = clicon_restconf_dir(h)) != NULL)
if (clixon_plugins_load(h, CLIXON_PLUGIN_INIT, dir, NULL) < 0)
return -1;
/* Print version, customized variant must wait for plugins to load */
if (print_version){
if (clixon_plugin_version_all(h, stdout) < 0)
goto done;
exit(0);
}
/* Create a pseudo-plugin to create extension callback to set the ietf-routing
* yang-data extension for api-root top-level restconf function.
*/
@ -1101,6 +1109,7 @@ usage(clicon_handle h,
fprintf(stderr, "usage:%s [options]\n"
"where options are\n"
"\t-h \t\t Help\n"
"\t-V \t\tPrint version and exit\n"
"\t-D <level>\t Debug level, overrides any config debug setting\n"
"\t-f <file>\t Configuration file (mandatory)\n"
"\t-E <dir> \t Extra configuration file directory\n"
@ -1136,8 +1145,9 @@ main(int argc,
int ret;
cxobj *xrestconf = NULL;
char *inline_config = NULL;
int config_dump = 0;
int config_dump = 0;
enum format_enum config_dump_format = FORMAT_XML;
int print_version = 0;
/* In the startup, logs to stderr & debug flag set later */
clicon_log_init(__PROGRAM__, LOG_INFO, logdst);
@ -1151,6 +1161,10 @@ main(int argc,
case 'h':
usage(h, argv0);
break;
case 'V': /* version */
cligen_output(stdout, "Clixon version %s\n", CLIXON_VERSION_STRING);
print_version++; /* plugins may also print versions w ca-version callback */
break;
case 'D' : /* debug. Note this overrides any setting in the config */
if (sscanf(optarg, "%d", &dbg) != 1)
usage(h, argv0);
@ -1300,7 +1314,7 @@ main(int argc,
goto done;
/* Clixon inits / configs */
if ((ret = restconf_clixon_init(h, inline_config, &xrestconf)) < 0)
if ((ret = restconf_clixon_init(h, inline_config, print_version, &xrestconf)) < 0)
goto done;
if (ret == 0){ /* restconf disabled */
clicon_log(LOG_INFO, "restconf configuration not found or disabled");

View file

@ -70,7 +70,7 @@
#include "snmp_register.h"
/* Command line options to be passed to getopt(3) */
#define SNMP_OPTS "hD:f:l:C:o:z"
#define SNMP_OPTS "hVD:f:l:C:o:z"
/* Forward */
static int clixon_snmp_input_cb(int s, void *arg);
@ -319,6 +319,7 @@ usage(clicon_handle h,
fprintf(stderr, "usage:%s\n"
"where options are\n"
"\t-h\t\tHelp\n"
"\t-V \t\tPrint version and exit\n"
"\t-D <level>\tDebug level (>1 for extensive libnetsnmp debug)\n"
"\t-f <file>\tConfiguration file (mandatory)\n"
"\t-l (e|o|s|f<file>) Log on std(e)rr, std(o)ut, (s)yslog(default), (f)ile\n"
@ -373,23 +374,27 @@ main(int argc,
case 'h' : /* help */
usage(h, argv[0]);
break;
case 'V': /* version */
cligen_output(stdout, "Clixon version %s\n", CLIXON_VERSION_STRING);
exit(0);
break;
case 'D' : /* debug */
if (sscanf(optarg, "%d", &dbg) != 1)
usage(h, argv[0]);
break;
case 'f': /* override config file */
case 'f': /* override config file */
if (!strlen(optarg))
usage(h, argv[0]);
clicon_option_str_set(h, "CLICON_CONFIGFILE", optarg);
break;
case 'l': /* Log destination: s|e|o */
case 'l': /* Log destination: s|e|o */
if ((logdst = clicon_log_opt(optarg[0])) < 0)
usage(h, argv[0]);
if (logdst == CLICON_LOG_FILE &&
strlen(optarg)>1 &&
clicon_log_file(optarg+1) < 0)
goto done;
break;
break;
}
/*