Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add option to opa check #133

Merged
merged 1 commit into from
Dec 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Use of this source code is governed by the MIT license that can be
* found in the LICENSE file.
*/

package org.openpolicyagent.ideaplugin.opa.project.settings

import com.intellij.openapi.options.BoundSearchableConfigurable
import com.intellij.openapi.project.Project
import com.intellij.openapi.ui.DialogPanel

import com.intellij.ui.layout.panel

/**
* UI for the opa setting options.
*/
class OpaOptionsConfigurable(private val project: Project) :
BoundSearchableConfigurable("Opa", "Opa Options", "Settings.Project.Opa") {

private val settings
get() = OpaProjectSettings.getInstance(project)

override fun createPanel(): DialogPanel {
return panel {
row("OPA check options:") {
textField(settings::opaCheckOptions)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Use of this source code is governed by the MIT license that can be
* found in the LICENSE file.
*/

package org.openpolicyagent.ideaplugin.opa.project.settings

import com.intellij.openapi.components.SimplePersistentStateComponent
import com.intellij.openapi.components.State
import com.intellij.openapi.components.Storage
import com.intellij.openapi.components.service
import com.intellij.openapi.project.Project

@State(name = "OPASettings", storages = [(Storage("opa.xml"))])
class OpaProjectSettings(val project: Project) : SimplePersistentStateComponent<OpaSettingsState>(OpaSettingsState()) {

var opaCheckOptions
get() = state.opaCheckOptions
set(value) {
state.opaCheckOptions = value
}

companion object {
@JvmStatic
val defaultOpaCheckOptions = "--strict"

@JvmStatic
fun getInstance(project: Project): OpaProjectSettings {
return project.service()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* Use of this source code is governed by the MIT license that can be
* found in the LICENSE file.
*/

package org.openpolicyagent.ideaplugin.opa.project.settings

import com.intellij.openapi.components.BaseState

class OpaSettingsState: BaseState() {
var opaCheckOptions = OpaProjectSettings.defaultOpaCheckOptions
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import org.openpolicyagent.ideaplugin.ide.actions.fileDirectChildOfRoot
import org.openpolicyagent.ideaplugin.ide.actions.getImportsAsString
import org.openpolicyagent.ideaplugin.ide.actions.getPackageAsString
import org.openpolicyagent.ideaplugin.ide.extensions.OPAActionToolWindow
import org.openpolicyagent.ideaplugin.opa.project.settings.OpaProjectSettings
import org.openpolicyagent.ideaplugin.openapiext.execute
import org.openpolicyagent.ideaplugin.openapiext.isUnitTestMode
import org.openpolicyagent.ideaplugin.openapiext.virtualFile
Expand Down Expand Up @@ -41,15 +42,21 @@ class OpaActions : OpaBaseTool() {
}

/**
* Opens window running `opa test` on the current file
* Opens window running `opa check` on the current file
* or displays popup if current file is not a rego file
*/

fun checkDocument(project: Project, document: Document) {
val file = document.virtualFile ?: return
// todo: get path to file relative to project path
val args = mutableListOf("check", file.name)
OPAActionToolWindow().runProcessInConsole(project, args, "Opa Check")

val checkArgs = OpaProjectSettings.getInstance(project).opaCheckOptions
.split(" ")
.filter { elem -> elem.trim().isNotEmpty() }

val opaArgs = mutableListOf("check")
opaArgs.addAll(checkArgs)
opaArgs.add(file.path)

OPAActionToolWindow().runProcessInConsole(project, opaArgs, "Opa Check")
}


Expand Down
5 changes: 5 additions & 0 deletions src/main/resources/META-INF/opa-core.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@
icon="/icons/opa_13_13.svg"
factoryClass="org.openpolicyagent.ideaplugin.ide.extensions.OPAActionToolWindowFactory"
canCloseContents="true" />

<!-- Project configuration -->
<projectService serviceImplementation="org.openpolicyagent.ideaplugin.opa.project.settings.OpaProjectSettings"/>
<projectConfigurable groupId="language" id="Settings.Project.Opa" instance="org.openpolicyagent.ideaplugin.opa.project.settings.OpaOptionsConfigurable"/>

</extensions>

<actions>
Expand Down