Code Monkey home page Code Monkey logo

kotlin-shell's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

kotlin-shell's Issues

Throw on errors ?

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}"()
    }

clarify license

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

.process() substitute "\n\n" by "\n"?

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
    }

Running piped commands seems not to work

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.

module not found: eu.jrie.jetbrains#kotlin-shell-core;0.2.1

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.

Interpolation of Kotlin variables not working?

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!

::export with vararg

Hi,

super cool project!

I need to define multiple exports 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")

Sensitive ENV info leaks on process ๐Ÿšจ

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}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.