Skip to content

Commit

Permalink
Feature: [Add support for -V option to give version](#472)
Browse files Browse the repository at this point in the history
  • Loading branch information
olofhagsand committed Dec 14, 2023
1 parent 2f81a08 commit 261469b
Show file tree
Hide file tree
Showing 11 changed files with 209 additions and 42 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ Expected: February 2024

### Minor features

* Feature: [Add support for -V option to give version](https://github.com/clicon/clixon/issues/472)
* All clixon applications added command-line option `-V` for printing version
* New ca_version callback for customized version output
* Optimization:
* Added mountpoint cache as yang flag `YANG_FLAG_MTPOINT_POTENTIAL`
* Optimized `yang_find`, especially namespace lookup
Expand Down
22 changes: 17 additions & 5 deletions apps/backend/backend_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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]);
Expand Down Expand Up @@ -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]);
Expand Down Expand Up @@ -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)
Expand Down
27 changes: 19 additions & 8 deletions apps/cli/cli_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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]);
Expand Down Expand Up @@ -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]);
Expand Down Expand Up @@ -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
*/
Expand Down
24 changes: 18 additions & 6 deletions apps/netconf/netconf_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand All @@ -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]);
Expand Down Expand Up @@ -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]);
Expand Down Expand Up @@ -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){
Expand Down
15 changes: 14 additions & 1 deletion apps/restconf/restconf_main_fcgi.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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);
Expand All @@ -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]);
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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.
*/
Expand Down
20 changes: 17 additions & 3 deletions apps/restconf/restconf_main_native.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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");
Expand Down
13 changes: 9 additions & 4 deletions apps/snmp/snmp_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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;
}

/*
Expand Down
Loading

0 comments on commit 261469b

Please sign in to comment.