Skip to content

Commit 4b28344

Browse files
committed
Run command in the project directory, if available
Fixes #57.
1 parent de0e9f0 commit 4b28344

File tree

4 files changed

+22
-3
lines changed

4 files changed

+22
-3
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
## [Unreleased]
66

7+
- Attempt to use the project directory as the working directory when running the command.
8+
79
## [0.1.0] - 2024-03-18
810

911
- Extend compatibility to 2024.1

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
<!-- Plugin description -->
88
Use an external command to acquire database connection credentials.
99

10-
Executes an external command and extracts database and username from its output.
10+
Executes an external command and extracts database and username from its output. The command is executed in the
11+
project’s root directory, if that can be resolved.
1112

1213
The plugin will first attempt to extract the information from output from a [Vault](https://www.vaultproject.io/)-like JSON with the following format:
1314

src/main/kotlin/com/github/liff/dbexternalcommandauth/Acquire.kt

+8-1
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,20 @@ import com.intellij.credentialStore.Credentials
44
import com.intellij.util.io.awaitExit
55
import kotlinx.coroutines.Dispatchers
66
import kotlinx.coroutines.withContext
7+
import java.io.File
78

89
suspend fun acquire(
910
command: String,
11+
directory: File?,
1012
parse: (String) -> Credentials? = ::parse,
1113
): Credentials? =
1214
withContext(Dispatchers.IO) {
13-
val proc = wrap(command).start()
15+
val proc = wrap(command)
16+
.apply {
17+
if (directory != null)
18+
directory(directory)
19+
}
20+
.start()
1421

1522
proc.outputStream.close()
1623

src/main/kotlin/com/github/liff/dbexternalcommandauth/CommandAuthProvider.kt

+10-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import com.intellij.database.dataSource.DatabaseCredentialsAuthProvider.Companio
99
import com.intellij.database.dataSource.ui.AuthWidgetBuilder
1010
import com.intellij.database.view.DatabaseCoreUiService
1111
import com.intellij.openapi.project.Project
12+
import com.intellij.openapi.project.guessProjectDir
1213

1314
class CommandAuthProvider : DatabaseAuthProvider {
1415
override suspend fun interceptConnection(
@@ -19,7 +20,15 @@ class CommandAuthProvider : DatabaseAuthProvider {
1920
proto.connectionPoint.getAdditionalProperty("command")
2021
?: error(MyBundle.message("no-command"))
2122

22-
val credentials = acquire(command)
23+
val directory = proto.project.guessProjectDir()?.let {
24+
try {
25+
it.toNioPath().toFile()
26+
} catch (_: UnsupportedOperationException) {
27+
null
28+
}
29+
}
30+
31+
val credentials = acquire(command = command, directory = directory)
2332

2433
applyCredentials(
2534
proto = proto,

0 commit comments

Comments
 (0)