Add Env support to placeholders

This commit is contained in:
Artem Chepurnoy 2024-01-05 18:32:23 +02:00
parent b151d19794
commit 72db7043a6
No known key found for this signature in database
GPG Key ID: FAC37D0CF674043E
3 changed files with 37 additions and 0 deletions

View File

@ -0,0 +1,25 @@
package com.artemchep.keyguard.common.service.placeholder.impl
import com.artemchep.keyguard.common.io.IO
import com.artemchep.keyguard.common.io.ioEffect
import com.artemchep.keyguard.common.service.placeholder.Placeholder
class EnvironmentPlaceholder(
) : Placeholder {
private val regex = "^%(.+)%$".toRegex()
override fun get(
key: String,
): IO<String?>? = kotlin.run {
val result = regex.matchEntire(key)
if (result != null) {
ioEffect {
val variable = result.groupValues[1]
System.getenv(variable)
}
} else {
// unknown
null
}
}
}

View File

@ -72,6 +72,7 @@ import com.artemchep.keyguard.common.service.placeholder.impl.CipherPlaceholder
import com.artemchep.keyguard.common.service.placeholder.impl.CommentPlaceholder import com.artemchep.keyguard.common.service.placeholder.impl.CommentPlaceholder
import com.artemchep.keyguard.common.service.placeholder.impl.CustomPlaceholder import com.artemchep.keyguard.common.service.placeholder.impl.CustomPlaceholder
import com.artemchep.keyguard.common.service.placeholder.impl.DateTimePlaceholder import com.artemchep.keyguard.common.service.placeholder.impl.DateTimePlaceholder
import com.artemchep.keyguard.common.service.placeholder.impl.EnvironmentPlaceholder
import com.artemchep.keyguard.common.service.placeholder.impl.TextTransformPlaceholder import com.artemchep.keyguard.common.service.placeholder.impl.TextTransformPlaceholder
import com.artemchep.keyguard.common.service.placeholder.impl.UrlPlaceholder import com.artemchep.keyguard.common.service.placeholder.impl.UrlPlaceholder
import com.artemchep.keyguard.common.service.placeholder.placeholderFormat import com.artemchep.keyguard.common.service.placeholder.placeholderFormat
@ -474,6 +475,7 @@ fun vaultViewScreenState(
CustomPlaceholder(secretOrNull), CustomPlaceholder(secretOrNull),
DateTimePlaceholder(), DateTimePlaceholder(),
TextTransformPlaceholder(), TextTransformPlaceholder(),
EnvironmentPlaceholder(),
) )
val extractors = LinkInfoRegistry(linkInfoExtractors) val extractors = LinkInfoRegistry(linkInfoExtractors)
val cipherUris = secretOrNull val cipherUris = secretOrNull

View File

@ -88,6 +88,16 @@ _Example_:
https://example.com?user=joe&password=Password1%21 https://example.com?user=joe&password=Password1%21
``` ```
#### Environmental variables
System environment variables are supported.
The name of the variable must be enclosed in `%` characters.
_Example_:
```
> {%HOME%}
/home/username
```
#### Date-time #### Date-time
##### Local ##### Local