11package com.github.blarc.sops.intellij.plugin
22
33import com.github.blarc.sops.intellij.plugin.settings.AppSettings
4+ import com.github.blarc.sops.intellij.plugin.settings.ProjectSettings
45import com.intellij.execution.configurations.GeneralCommandLine
56import com.intellij.execution.process.*
67import com.intellij.execution.util.ExecUtil
8+ import com.intellij.openapi.components.service
9+ import com.intellij.openapi.project.Project
710import com.intellij.openapi.util.Key
811import com.intellij.openapi.vfs.LocalFileSystem
912import com.intellij.openapi.vfs.VirtualFile
@@ -18,36 +21,42 @@ object SopsWrapper {
1821
1922 suspend fun version (
2023 sopsPath : String ,
24+ project : Project ,
2125 onSuccess : suspend (String ) -> Unit ,
2226 onError : suspend (String ) -> Unit = {}
2327 ) {
2428 run (
25- sopsPath, " --version" ,
29+ sopsPath,
30+ " --version" ,
31+ project,
2632 onSuccess = { result -> onSuccess(result.lines().first()) },
2733 onError = onError
2834 )
2935 }
3036
3137 suspend fun encrypt (
3238 file : VirtualFile ,
39+ project : Project ,
3340 inPlace : Boolean = false,
3441 onSuccess : suspend (String ) -> Unit ,
3542 onError : suspend (String ) -> Unit = {}
3643 ) {
37- run (" encrypt" , file, inPlace, onSuccess, onError)
44+ run (" encrypt" , project, file, inPlace, onSuccess, onError)
3845 }
3946
4047 suspend fun decrypt (
4148 file : VirtualFile ,
49+ project : Project ,
4250 inPlace : Boolean = false,
4351 onSuccess : suspend (String ) -> Unit ,
4452 onError : suspend (String ) -> Unit = {}
4553 ) {
46- run (" decrypt" , file, inPlace, onSuccess, onError)
54+ run (" decrypt" , project, file, inPlace, onSuccess, onError)
4755 }
4856
4957 suspend fun decrypt (
5058 text : String ,
59+ project : Project ,
5160 onSuccess : suspend (String ) -> Unit ,
5261 onError : suspend (String ) -> Unit = {}
5362 ) {
@@ -58,11 +67,12 @@ object SopsWrapper {
5867 tmpFile.deleteOnExit()
5968
6069 val file = LocalFileSystem .getInstance().refreshAndFindFileByIoFile(tmpFile)
61- run (" decrypt" , file!! , false , onSuccess, onError)
70+ run (" decrypt" , project, file!! , false , onSuccess, onError)
6271 }
6372
6473 suspend fun edit (
6574 file : VirtualFile ,
75+ project : Project ,
6676 newText : String? ,
6777 onSuccess : suspend () -> Unit = {},
6878 onError : suspend (String , Int ) -> Unit = { _, _ -> }
@@ -73,7 +83,7 @@ object SopsWrapper {
7383 onError(" Sops path not configured" , 1 )
7484 return
7585 }
76- val command = buildCommand(sopsPath, file.parent.path)
86+ val command = buildCommand(sopsPath, project, file.parent.path)
7787
7888 val scriptFiles = ScriptUtil .createScriptFiles()
7989 val editorPath: String = scriptFiles.script.toAbsolutePath().toString()
@@ -130,6 +140,7 @@ object SopsWrapper {
130140
131141 suspend fun run (
132142 sopsCommand : String ,
143+ project : Project ,
133144 file : VirtualFile ? = null,
134145 inPlace : Boolean = false,
135146 onSuccess : suspend (String ) -> Unit ,
@@ -140,18 +151,19 @@ object SopsWrapper {
140151 onError(" Sops path not configured" )
141152 return
142153 }
143- run (sopsPath, sopsCommand, file, inPlace, onSuccess, onError)
154+ run (sopsPath, sopsCommand, project, file, inPlace, onSuccess, onError)
144155 }
145156
146157 suspend fun run (
147158 sopsPath : String ,
148159 sopsCommand : String ,
160+ project : Project ,
149161 file : VirtualFile ? = null,
150162 inPlace : Boolean = false,
151163 onSuccess : suspend (String ) -> Unit ,
152164 onError : suspend (String ) -> Unit
153165 ) {
154- val command = buildCommand(sopsPath, file?.parent?.path)
166+ val command = buildCommand(sopsPath, project, file?.parent?.path)
155167 command.addParameter(sopsCommand)
156168 if (inPlace) {
157169 command.addParameter(" --in-place" )
@@ -176,10 +188,11 @@ object SopsWrapper {
176188 }
177189 }
178190
179- private fun buildCommand (sopsPath : String , cwd : String? = null): GeneralCommandLine {
191+ private fun buildCommand (sopsPath : String , project : Project , cwd : String? = null): GeneralCommandLine {
192+ val projectSettings = project.service<ProjectSettings >()
180193 val command: GeneralCommandLine = GeneralCommandLine (sopsPath)
181194 .withParentEnvironmentType(GeneralCommandLine .ParentEnvironmentType .CONSOLE )
182- .withEnvironment(AppSettings .instance.sopsEnvironment)
195+ .withEnvironment(projectSettings.sopsProjectEnvironment + AppSettings .instance.sopsEnvironment)
183196 .withCharset(StandardCharsets .UTF_8 )
184197 .withWorkDirectory(cwd)
185198
0 commit comments