-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add Plugin to run rules and small formatting changes
- Loading branch information
1 parent
77862f8
commit a09f0ec
Showing
6 changed files
with
143 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
cpg-console/src/main/kotlin/de/fraunhofer/aisec/cpg/console/RunRulePlugin.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Copyright (c) 2021, Fraunhofer AISEC. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* $$$$$$\ $$$$$$$\ $$$$$$\ | ||
* $$ __$$\ $$ __$$\ $$ __$$\ | ||
* $$ / \__|$$ | $$ |$$ / \__| | ||
* $$ | $$$$$$$ |$$ |$$$$\ | ||
* $$ | $$ ____/ $$ |\_$$ | | ||
* $$ | $$\ $$ | $$ | $$ | | ||
* \$$$$$ |$$ | \$$$$$ | | ||
* \______/ \__| \______/ | ||
* | ||
*/ | ||
package de.fraunhofer.aisec.cpg.console | ||
|
||
import org.jetbrains.kotlinx.ki.shell.BaseCommand | ||
import org.jetbrains.kotlinx.ki.shell.Command | ||
import org.jetbrains.kotlinx.ki.shell.Plugin | ||
import org.jetbrains.kotlinx.ki.shell.Shell | ||
import org.jetbrains.kotlinx.ki.shell.configuration.ReplConfiguration | ||
|
||
class RunRulePlugin : Plugin { | ||
inner class Load(conf: ReplConfiguration) : BaseCommand() { | ||
override val name: String by conf.get(default = "runRule") | ||
override val short: String by conf.get(default = "rr") | ||
override val description: String = | ||
"runs a rule. Requires `result` to be set, e.g. by running the :translate " + "command." | ||
|
||
override val params = "<rule[ rule]>" | ||
|
||
override fun execute(line: String): Command.Result { | ||
val ruletoRun = line.split(" ")[1] // [":r", "<rule>"] | ||
return Command.Result.RunSnippets( | ||
listOf( | ||
// import | ||
"import de.fraunhofer.aisec.cpg.rules.$ruletoRun", | ||
"import de.fraunhofer.aisec.cpg.query.SarifReporter", | ||
// run it | ||
"val rule = $ruletoRun()", | ||
"rule.run(result)", | ||
"rules.add(rule)", | ||
) | ||
) | ||
} | ||
} | ||
|
||
lateinit var repl: Shell | ||
|
||
override fun init(repl: Shell, config: ReplConfiguration) { | ||
this.repl = repl | ||
|
||
repl.registerCommand(Load(config)) | ||
} | ||
|
||
override fun cleanUp() { | ||
// nothing to do | ||
} | ||
} |