Skip to content

Commit

Permalink
feat: add --print-session option to history command
Browse files Browse the repository at this point in the history
And document it while we're at it
  • Loading branch information
lucapette committed Dec 27, 2023
1 parent 70712e7 commit ea3190e
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 11 deletions.
6 changes: 4 additions & 2 deletions docs/docs/reference/language/operators.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ The purpose of this document is to spec `TypeStream` data operators.

### Description

The `each` data operator is used to execute a [block expression](spec.md#block-expression) for each record in a data stream. The
The `each` data operator is used to execute a [block
expression](spec.md#block-expression) for each record in a data stream. The
block expression is required.

`each` **must** be the last operator in a pipeline.

Here's an example of using `each` to make a HTTP request for each record in a data stream:
Here's an example of using `each` to make a HTTP request for each record in a
data stream:

```sh
cat /dev/kafka/local/books | each { book -> http post https://example.com/new_books "{\"book_id\": #{$book.id}}" }
Expand Down
21 changes: 19 additions & 2 deletions docs/docs/reference/language/shell.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,30 @@ Since `TypeStream` is heavily inspired by the Unix philosophy, it's only natural
that it supports many shell commands.

:::note
Note that feature parity with Unix shells is not a goal of `TypeStream` so do not expect commands to be exactly the same as their Unix counterparts.

Furthermore, `TypeStream` supports a few commands that are not available in Unix shells.
Feature parity with Unix shells is a non-goal of `TypeStream` so do not expect
commands to be exactly the same as their Unix counterparts.

`TypeStream` also supports a few commands that are not available in Unix shells.

:::

## Cd

## History

### Synopsis

`history [-p]`

### Description

The `history` command is used to display the history of commands executed in the current session.

The following options are supported:

- `-p` `--print-session` - prints the history in a copy-paste friendly format

## Http

### Synopsis
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,6 @@ private fun kill(session: Session, args: List<String>): ShellCommandOutput {
return ShellCommandOutput.withError("kill: ${args.joinToString(" ")}")
}

private fun history(session: Session, @Suppress("UNUSED_PARAMETER") args: List<String>) =
ShellCommandOutput.withOutput(session.env.history().mapIndexed { index, command ->
DataStream("/bin/history", Schema.String("${index + 1} $command"))
})

private fun openaiComplete(
@Suppress("UNUSED_PARAMETER") session: Session,
args: List<String>,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.typestream.compiler.shellcommand

import io.typestream.compiler.types.DataStream
import io.typestream.compiler.types.schema.Schema
import io.typestream.compiler.vm.Session
import io.typestream.option.Option
import io.typestream.option.parseOptions

data class HistoryOptions(
@param:Option(
names = ["-p", "--print-session"],
description = "inverts matching pattern"
) val printSession: Boolean = false,
)

fun history(session: Session, args: List<String>): ShellCommandOutput {
val (options, _) = parseOptions<HistoryOptions>(args)

if (options.printSession) {
return ShellCommandOutput.withOutput(session.env.history().map { command ->
DataStream("/bin/history", Schema.String(command))
})
}

return ShellCommandOutput.withOutput(session.env.history().mapIndexed { index, command ->
DataStream("/bin/history", Schema.String("${index + 1} $command"))
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ fun http(

val url = arguments.first()

val output = when(verb) {
val output = when (verb) {
"get" -> HttpClient.get(url)
"post" -> {
if (arguments.size < 2) {
Expand All @@ -34,6 +34,7 @@ fun http(
val body = arguments[1]
HttpClient.post(url, body)
}

else -> return ShellCommandOutput.withError("$verb requests not supported")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import org.testcontainers.junit.jupiter.Testcontainers


@Testcontainers
internal class ShellCommandsKtTest {
internal class CdKtTest {
@Container
private val testKafka = TestKafka()

Expand Down

0 comments on commit ea3190e

Please sign in to comment.