* Added cligen variable translation.

* See FAQ and example
This commit is contained in:
Olof hagsand 2018-05-28 22:50:22 +02:00
parent b230215eaf
commit cfe4702069
6 changed files with 65 additions and 1 deletions

View file

@ -3,8 +3,10 @@
## 3.7.0 (Upcoming) ## 3.7.0 (Upcoming)
### Major changes: ### Major changes:
### Minor changes: ### Minor changes:
* Added cligen variable translation.
* See FAQ and example
### Corrected Bugs ### Corrected Bugs
- Fixed JSON unbalanced braces resultin assert. * Fixed JSON unbalanced braces resultin assert.
## 3.6.0 (30 April 2018) ## 3.6.0 (30 April 2018)

View file

@ -277,6 +277,9 @@ cli_load_syntax(clicon_handle h,
} }
if (cligen_expandv_str2fn(pt, (expandv_str2fn_t*)clixon_str2fn, handle) < 0) if (cligen_expandv_str2fn(pt, (expandv_str2fn_t*)clixon_str2fn, handle) < 0)
goto done; goto done;
/* Variable translation functions */
if (cligen_translate_str2fn(pt, (translate_str2fn_t*)clixon_str2fn, handle) < 0)
goto done;
/* Make sure we have a syntax mode specified */ /* Make sure we have a syntax mode specified */
if (mode == NULL || strlen(mode) < 1) { /* may be null if not given in file */ if (mode == NULL || strlen(mode) < 1) { /* may be null if not given in file */

View file

@ -344,3 +344,33 @@ To authenticate, the callback needs to return the value 1 and supply a username.
See [../apps/example/example_restconf.c] plugin_credentials() for See [../apps/example/example_restconf.c] plugin_credentials() for
an example of HTTP basic auth. an example of HTTP basic auth.
## How do I write a CLI translator function
The CLI can perform variable translation. This is useful if you want to
prcess the input, such as hashing, encrypting or in other way
translate the input.
Yang example:
```
list translate{
leaf value{
type string;
}
}
```
CLI specification:
```
translate value (<value:string translate:incstr()>),cli_set("/translate/value");
```
If you run this example using the `incstr()` function which increments the characters in the input, you get this result:
```
cli> translate value HAL
cli> show configuration
translate {
value IBM;
}
```
You can perform translation on any type, not only strings.

View file

@ -11,6 +11,12 @@ module example {
} }
description description
"Example code that includes ietf-ip and ietf-routing"; "Example code that includes ietf-ip and ietf-routing";
/* Translation function example - See also example_cli */
list translate{
leaf value{
type string;
}
}
rpc client-rpc { rpc client-rpc {
description "Example local client-side RPC that is processed by the description "Example local client-side RPC that is processed by the
the netconf/restconf and not sent to the backend. the netconf/restconf and not sent to the backend.

View file

@ -137,3 +137,21 @@ clixon_plugin_init(clicon_handle h)
return &api; return &api;
} }
/*! Translate function from an original value to a new.
* In this case, assume string and increment characters, eg HAL->IBM
*/
int
incstr(cligen_handle h,
cg_var *cv)
{
char *str;
int i;
if (cv_type_get(cv) != CGV_STRING)
return 0;
str = cv_string_get(cv);
for (i=0; i<strlen(str); i++)
str[i]++;
return 0;
}

View file

@ -3,6 +3,11 @@ CLICON_MODE="example";
CLICON_PROMPT="%U@%H> "; CLICON_PROMPT="%U@%H> ";
CLICON_PLUGIN="example_cli"; CLICON_PLUGIN="example_cli";
# Translate variable "value" by incrementing its characters
translate value (<value:string translate:incstr()>),cli_set("/translate/value");
# Note, when switching to PT, change datamodel to only @datamodel # Note, when switching to PT, change datamodel to only @datamodel
set @datamodel:example, cli_set(); set @datamodel:example, cli_set();
merge @datamodel:example, cli_merge(); merge @datamodel:example, cli_merge();