jakubriegel / kotlin-shell Goto Github PK
View Code? Open in Web Editor NEWTool for performing shell-like programing in Kotlin
License: Apache License 2.0
Tool for performing shell-like programing in Kotlin
License: Apache License 2.0
Is it possible to automatically throw on errors? Similar to set -e
:
shell {
throwOnNonZeroStatus() // or simply throwOnError() ?
// will fail the script if the zip file is not present
"unzip ${zipFile.absolutePath}"()
}
As titled, sorry if this may sound dumb, but I'm looking at parsing manually specific commands outputs.
Bintray is going away as of May 1st: https://jfrog.com/blog/into-the-sunset-bintray-jcenter-gocenter-and-chartcenter/
Is it possible to update the docs to indicate how to include kotlin-shell
assuming bintray is not available?
First, thanks for the initiative to make Kotlin usable for shell scripting.
Please add a LICENSE file in the root of the project, stating under what license we're allowed to use this project.
Suggestion: Apache 2.0
Hey, very cool project.
This page https://github.com/jakubriegel/kotlin-shell/packages/765107 incorrectly lists groupId
as kotlin-shell-core.eu.jrie.jetbrains
where it should be eu.jrie.jetbrains
Publish this project to Maven central, since this is a lot more convenient to use than custom repos.
Information on requirements (pom metadata, GPG signing, etc.) here: https://central.sonatype.org/publish/publish-guide/
See an example of how to publish to Maven central from Gradle here: https://github.com/testerum/testerum-api/tree/gradle
Hello,
Is there an easy way to access the stdout of a Process in its entirety after it has completed?
If not, would a PR for such a way be welcome?
Hi, first thank you for this very nice project, I was planning to write something similar!
The following seems to substitute consecutive double new lines with only one.
pipeline {
"echo a\n\nb\n\nc\n".process() pipe stdout
}
The problem is also with different commands separating records by double new lines. (In my application I had to process the output of udevadm monitor -k -p
).
It looks that the problem is in .process()
and not in pipe
, since the following works correctly:
pipeline {
"a\n\nb\n\nc\n" pipe stdout
}
I am trying to run the following command in kotlin-shell:
`
#!/usr/bin/env kscript
import eu.jrie.jetbrains.kotlinshell.shell.*
shell {
println("wget -q -O - https://pkg.jenkins.io/debian-stable/jenkins.io.key | apt-key add -"())
}
`
But it looks like it doesn't work correctly and prints as an output result of the first command, instead of the output of both of them.
On the other hand, I was able to implement similar functionality in few lines of code, and this approach works:
`
import java.io.File
import java.util.concurrent.TimeUnit
data class ProcessResult(val exitCode: Int, val systemOut: String, val systemErr: String)
fun String.shellRun(workingDir: File? = null, waitTimeSec: Long = 60, throwException: Boolean = true): ProcessResult {
val proc = ProcessBuilder("/bin/sh", "-c", this).directory(workingDir)
.redirectOutput(ProcessBuilder.Redirect.PIPE)
.redirectError(ProcessBuilder.Redirect.PIPE)
.start()
val exitedNormally = proc.waitFor(waitTimeSec, TimeUnit.SECONDS)
if (!exitedNormally) {
throw IllegalStateException("Command has timed out after $waitTimeSec seconds.")
}
val processResult = ProcessResult(proc.exitValue(),
proc.inputStream.bufferedReader().readText().trim(),
proc.errorStream.bufferedReader().readText().trim())
if (throwException && processResult.exitCode != 0) {
throw IllegalStateException("Command failed.\n$this\n${processResult.systemErr}")
}
return processResult
}
println("wget -q -O - https://pkg.jenkins.io/debian-stable/jenkins.io.key | apt-key add -".shellRun())
println("echo deb https://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list".shellRun())
println("apt update;apt -y install jenkins".shellRun())
`
Is it possible to add the above functionality to kotlin-shell? It would be a great help with moving real bash configuration scripts into Kotlin.
root@if-alex-node-0:~# kotlin -version
Kotlin version 1.3.71-release-431 (JRE 14+36-1461)
root@if-alex-node-0:~# ./fsmoke-test.main.kts
:: problems summary ::
:::: WARNINGS
module not found: eu.jrie.jetbrains#kotlin-shell-core;0.2.1
==== central: tried
https://repo1.maven.org/maven2/eu/jrie/jetbrains/kotlin-shell-core/0.2.1/kotlin-shell-core-0.2.1.pom
-- artifact eu.jrie.jetbrains#kotlin-shell-core;0.2.1!kotlin-shell-core.jar:
https://repo1.maven.org/maven2/eu/jrie/jetbrains/kotlin-shell-core/0.2.1/kotlin-shell-core-0.2.1.jar
error: cannot access script base class 'org.jetbrains.kotlin.mainKts.MainKtsScript'. Check your module classpath for missing or conflicting dependencies (fsmoke-test.main.kts:3:1)
error: unresolved reference: Repository (fsmoke-test.main.kts:3:7)
error: unresolved reference: DependsOn (fsmoke-test.main.kts:4:7)
error: unresolved reference: DependsOn (fsmoke-test.main.kts:5:7)
error: unresolved reference: CompilerOptions (fsmoke-test.main.kts:6:7)
error: unresolved reference: kotlinx (fsmoke-test.main.kts:7:13)
error: an annotation argument must be a compile-time constant (fsmoke-test.main.kts:7:13)
error: unresolved reference: shell (fsmoke-test.main.kts:29:1)
error: unresolved reference: stringLambda (fsmoke-test.main.kts:30:19)
error: unresolved reference: it (fsmoke-test.main.kts:30:34)
error: unresolved reference: pipeline (fsmoke-test.main.kts:31:5)
error: unresolved reference: process (fsmoke-test.main.kts:31:62)
error: unresolved reference: process (fsmoke-test.main.kts:31:93)
fsmoke-test.main.kts: error: unresolved dependency: eu.jrie.jetbrains#kotlin-shell-core;0.2.1: not found
fsmoke-test.main.kts:3:1: error: cannot access script base class 'org.jetbrains.kotlin.mainKts.MainKtsScript'. Check your module classpath for missing or conflicting dependencies
@file:Repository("https://dl.bintray.com/jakubriegel/kotlin-shell")
^
fsmoke-test.main.kts:3:7: error: unresolved reference: Repository
@file:Repository("https://dl.bintray.com/jakubriegel/kotlin-shell")
^
fsmoke-test.main.kts:4:7: error: unresolved reference: DependsOn
@file:DependsOn("eu.jrie.jetbrains:kotlin-shell-core:0.2.1")
...comes from this code...
#!/usr/bin/env kotlin
@file:Repository("https://dl.bintray.com/jakubriegel/kotlin-shell")
@file:DependsOn("eu.jrie.jetbrains:kotlin-shell-core:0.2.1")
import eu.jrie.jetbrains.kotlinshell.shell.*
shell {
val toUpper = stringLambda { it.toUpperCase() to "" }
pipeline { "curl https://uli-ifinder-demo.intrafind.net".process() pipe "grep Analyzer".process() pipe toUpper }
}
System.exit(0)
...whereas...
@file:Repository("https://jcenter.bintray.com")
@file:DependsOn("org.slf4j:slf4j-simple:1.7.30")
@file:DependsOn("com.squareup.okhttp3:okhttp:4.4.1")
@file:CompilerOptions("-Xopt-in=kotlin.RequiresOptIn")
...works fine.
Hi!
First of all, great project, exactly what I was looking for!
I'm currently writing a script which delegates to git to create a commit / branch and corresponding merge request. This seems to work fine, except for the commit itself
shell {
"git checkout -b silencer/exclude-${packageName.replace(':', '_')}"()
"git add renovate.json"()
"git -c user.name=silencer -c user.email= commit -m 'chore(dependency-updates): Exclude $packageName from $groupName'"()
"git push -o merge_request.create"()
}
Somehow the interpolation in the commit message is not working, the string 'chore(dependency-updates): Exclude $packageName from $groupName'
is split into multiple words it seems. Is this a bug or an error on my side?
Thanks in advance!
Hi,
super cool project!
I need to define multiple export
s like:
export JAVA_OPTS='-Xms82G -Xmx82G -XX:+UseParallelGC'
export EPISIM_SETUP='org.matsim.run.batch.DresdenCalibration'
export EPISIM_PARAMS='org.matsim.run.batch.DresdenCalibration$Params'
export EPISIM_INPUT='dresden'
export EPISIM_OUTPUT='output-dresden'
I'd like another ::export
that accept a vararg
, in this way I may simply type
export(
"JAVA_OPTS" to "-Xms82G -Xmx82G -XX:+UseParallelGC",
"EPISIM_SETUP" to "org.matsim.run.batch.DresdenCalibration",
"EPISIM_PARAMS" to "org.matsim.run.batch.DresdenCalibration$Params",
"EPISIM_INPUT" to "dresden",
"EPISIM_OUTPUT" to "output-dresden")
Currently org.zeroturnaround.exec.ProcessExecutor
calls are logged, which leads to dangerous scenarios where sensitive environment info can be leaked into logs.
Example
[[[SystemProcess 1] -1 git]_execution_thread] DEBUG o.z.exec.ProcessExecutor - Executing [git, clone, https://github.com/bkbnio/skelegro] in /Users/lappy/Workspace/bkbnio/cortex with environment {PATH=..., VERY_SECRET_KEY=123}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.