diff --git a/CHANGELOG.md b/CHANGELOG.md index bc1f43d1..caec7492 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,8 +3,10 @@ ## 3.7.0 (Upcoming) ### Major changes: ### Minor changes: +* Added cligen variable translation. + * See FAQ and example ### Corrected Bugs -- Fixed JSON unbalanced braces resultin assert. +* Fixed JSON unbalanced braces resultin assert. ## 3.6.0 (30 April 2018) diff --git a/apps/cli/cli_plugin.c b/apps/cli/cli_plugin.c index 04fc027c..369f1691 100644 --- a/apps/cli/cli_plugin.c +++ b/apps/cli/cli_plugin.c @@ -277,6 +277,9 @@ cli_load_syntax(clicon_handle h, } if (cligen_expandv_str2fn(pt, (expandv_str2fn_t*)clixon_str2fn, handle) < 0) 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 */ if (mode == NULL || strlen(mode) < 1) { /* may be null if not given in file */ diff --git a/doc/FAQ.md b/doc/FAQ.md index 949f94ce..2ac6dab8 100644 --- a/doc/FAQ.md +++ b/doc/FAQ.md @@ -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 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 (),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. \ No newline at end of file diff --git a/example/example.yang b/example/example.yang index f9159228..ad8e1367 100644 --- a/example/example.yang +++ b/example/example.yang @@ -11,6 +11,12 @@ module example { } description "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 { description "Example local client-side RPC that is processed by the the netconf/restconf and not sent to the backend. diff --git a/example/example_cli.c b/example/example_cli.c index b8a91fa3..fef13c35 100644 --- a/example/example_cli.c +++ b/example/example_cli.c @@ -137,3 +137,21 @@ clixon_plugin_init(clicon_handle h) 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),cli_set("/translate/value"); + + + # Note, when switching to PT, change datamodel to only @datamodel set @datamodel:example, cli_set(); merge @datamodel:example, cli_merge();