mirror of
https://github.com/openhab/openhab-addons.git
synced 2025-01-27 07:41:39 +01:00
[transformation][exec] allow spaces in parameters by enclosing in single quotes (#16160)
Signed-off-by: Leo Siepel <leosiepel@gmail.com>
This commit is contained in:
parent
3c0eb946f1
commit
2dfd663a86
@ -5,6 +5,10 @@ Transforms an input string with an external program.
|
|||||||
Executes an external program and returns the output as a string.
|
Executes an external program and returns the output as a string.
|
||||||
In the given command line the placeholder `%s` is substituted with the input value.
|
In the given command line the placeholder `%s` is substituted with the input value.
|
||||||
|
|
||||||
|
The provided command line is split on spaces before it is passed to the shell.
|
||||||
|
Using single quotes (`'`) splitting can be avoided (e.g. `'%s'` would prevent splitting on spaces within the input value).
|
||||||
|
The surrounding single quotes are removed.
|
||||||
|
|
||||||
The external program must either be in the executable search path of the server process, or an absolute path has to be used.
|
The external program must either be in the executable search path of the server process, or an absolute path has to be used.
|
||||||
|
|
||||||
For security reasons all commands need to be whitelisted.
|
For security reasons all commands need to be whitelisted.
|
||||||
@ -23,7 +27,7 @@ numfmt --to=iec-i --suffix=B --padding=7 %s
|
|||||||
|
|
||||||
### General Setup
|
### General Setup
|
||||||
|
|
||||||
**Item**
|
#### Item
|
||||||
|
|
||||||
This will replace the visible label in the UI with the transformation you apply with the command <TransformProgram>.
|
This will replace the visible label in the UI with the transformation you apply with the command <TransformProgram>.
|
||||||
|
|
||||||
@ -31,7 +35,7 @@ This will replace the visible label in the UI with the transformation you apply
|
|||||||
String yourItem "Some info [EXEC(/absolute/path/to/your/<TransformProgram> %s):%s]"
|
String yourItem "Some info [EXEC(/absolute/path/to/your/<TransformProgram> %s):%s]"
|
||||||
```
|
```
|
||||||
|
|
||||||
**Rule**
|
#### Rule
|
||||||
|
|
||||||
```java
|
```java
|
||||||
rule "Your Rule Name"
|
rule "Your Rule Name"
|
||||||
@ -53,7 +57,7 @@ Substitute the `/absolute/path/to/your/<TransformProgram>` with
|
|||||||
|
|
||||||
When the input argument for `%s` is `fri` the execution returns a string with the last weekday of the month, formated as readable text.
|
When the input argument for `%s` is `fri` the execution returns a string with the last weekday of the month, formated as readable text.
|
||||||
|
|
||||||
```
|
```shell
|
||||||
Fri 31 Mar 2017 13:58:47 IST`
|
Fri 31 Mar 2017 13:58:47 IST`
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -84,9 +88,7 @@ If omitted the default is `%s`, so the input value will be put into the transfor
|
|||||||
|
|
||||||
Please note: This profile is a one-way transformation, i.e. only values from a device towards the item are changed, the other direction is left untouched.
|
Please note: This profile is a one-way transformation, i.e. only values from a device towards the item are changed, the other direction is left untouched.
|
||||||
|
|
||||||
# Further Reading
|
## Further Reading
|
||||||
|
|
||||||
* [Manual](http://man7.org/linux/man-pages/man1/date.1.html) and [tutorial](https://linode.com/docs/tools-reference/tools/use-the-date-command-in-linux/) for date.
|
* [Manual](http://man7.org/linux/man-pages/man1/date.1.html) and [tutorial](https://linode.com/docs/tools-reference/tools/use-the-date-command-in-linux/) for date.
|
||||||
* [Manual](http://man7.org/linux/man-pages/man1/numfmt.1.html) and [tutorial](https://www.pixelbeat.org/docs/numfmt.html) for numfmt.
|
* [Manual](http://man7.org/linux/man-pages/man1/numfmt.1.html) and [tutorial](https://www.pixelbeat.org/docs/numfmt.html) for numfmt.
|
||||||
|
|
||||||
|
|
||||||
|
@ -13,6 +13,7 @@
|
|||||||
package org.openhab.transform.exec.internal;
|
package org.openhab.transform.exec.internal;
|
||||||
|
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||||
import org.eclipse.jdt.annotation.Nullable;
|
import org.eclipse.jdt.annotation.Nullable;
|
||||||
@ -35,6 +36,7 @@ import org.slf4j.LoggerFactory;
|
|||||||
@NonNullByDefault
|
@NonNullByDefault
|
||||||
@Component(property = { "openhab.transform=EXEC" })
|
@Component(property = { "openhab.transform=EXEC" })
|
||||||
public class ExecTransformationService implements TransformationService {
|
public class ExecTransformationService implements TransformationService {
|
||||||
|
private static final Pattern SPLIT_ON_SPACE = Pattern.compile("(['])((?:\\\\\\1|.)+?)\\1|([^\\s']+)");
|
||||||
private final Logger logger = LoggerFactory.getLogger(ExecTransformationService.class);
|
private final Logger logger = LoggerFactory.getLogger(ExecTransformationService.class);
|
||||||
private final ExecTransformationWhitelistWatchService execTransformationWhitelistWatchService;
|
private final ExecTransformationWhitelistWatchService execTransformationWhitelistWatchService;
|
||||||
|
|
||||||
@ -66,8 +68,9 @@ public class ExecTransformationService implements TransformationService {
|
|||||||
long startTime = System.currentTimeMillis();
|
long startTime = System.currentTimeMillis();
|
||||||
|
|
||||||
String formattedCommandLine = String.format(commandLine, source);
|
String formattedCommandLine = String.format(commandLine, source);
|
||||||
String result = ExecUtil.executeCommandLineAndWaitResponse(Duration.ofSeconds(5),
|
String[] cmdLineParts = SPLIT_ON_SPACE.matcher(formattedCommandLine).results()
|
||||||
formattedCommandLine.split(" "));
|
.map(mr -> mr.group(2) == null ? mr.group() : mr.group(2)).toArray(String[]::new);
|
||||||
|
String result = ExecUtil.executeCommandLineAndWaitResponse(Duration.ofSeconds(5), cmdLineParts);
|
||||||
logger.trace("command line execution elapsed {} ms", System.currentTimeMillis() - startTime);
|
logger.trace("command line execution elapsed {} ms", System.currentTimeMillis() - startTime);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
Loading…
Reference in New Issue
Block a user