Skip to content

Commit

Permalink
Consolidate GRPC samples (#486)
Browse files Browse the repository at this point in the history
* Add RunnerService examples

* Rename

* Setup instructions

* Consolidate

* Break out setup

* Fix headline

* Return default lifecycle identity if options are nil

* Use json file

* Return default only if unset

* Fix plural
  • Loading branch information
sourishkrout authored Feb 2, 2024
1 parent be9f21e commit bca0000
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 53 deletions.
File renamed without changes.
6 changes: 6 additions & 0 deletions examples/grpc-samples/resolve-vars.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"script": "export NAME=\"Noname\"\nexport LICENSE=$(cat ../LICENSE)\necho \"LICENSE: $LICENSE\"",
"env": [
"LICENSE="
]
}
28 changes: 26 additions & 2 deletions examples/PROJECTS.md → examples/grpc-samples/services.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ runme:
version: v2.2
---

## gRPC runme.project.v1.ProjectService
## ProjectService

Flags matche the launcher arguments in .vscode/launch.json.
Flags match the launcher arguments in `.vscode/launch.json` for `Debug Server`. Be sure to complete [setup.md](setup.md).

List available operations:

Expand Down Expand Up @@ -39,3 +39,27 @@ $ grpcurl \
-d "{\"directory\":{\"path\":\".\"}}" \
127.0.0.1:9999 runme.project.v1.ProjectService/Load
```

## RunnerService

List all runner services.

```sh {"id":"01HNGQNYYWKP635FT8GHE67476","promptEnv":"false"}
export VERSION="v1" # there's also v2alpha1
$ grpcurl \
-cacert /tmp/runme/tls/cert.pem \
-cert /tmp/runme/tls/cert.pem \
-key /tmp/runme/tls/key.pem \
127.0.0.1:9999 list runme.runner.$VERSION.RunnerService
```

Resolve variables inside cell:

```sh {"id":"01HNGQS6TV8YKQAKE0ZD7TZREH","promptEnv":"false","terminalRows":"15"}
$ grpcurl \
-cacert /tmp/runme/tls/cert.pem \
-cert /tmp/runme/tls/cert.pem \
-key /tmp/runme/tls/key.pem \
-d @ \
127.0.0.1:9999 runme.runner.v1.RunnerService/ResolveVars < resolve-vars.json
```
34 changes: 34 additions & 0 deletions examples/grpc-samples/setup.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
runme:
id: 01HF7BT3HBDTRGQAQMH51RDQEC
version: v2.2
---

# GRPC Setup

Install system dependencies:

```sh {"id":"01HF7BT3HBDTRGQAQMGTJN1KP3"}
$ brew bundle --no-lock
```

## Exercise GRPC interface

Issue a simple call to the deserialize API, first set markdown input data:

```sh {"id":"01HF7BT3HBDTRGQAQMH0ZYTWA9"}
export MD="# Ohai this is my cool headline"
```

Then issue RPC call and display the result:

```sh {"closeTerminalOnSuccess":"false","id":"01HF7BT3HBDTRGQAQMH2K85BHG","terminalRows":"15"}
$ data="$(echo $MD | openssl base64 | tr -d '\n')"
$ cd ../.. && grpcurl \
-cacert /tmp/runme/tls/cert.pem \
-cert /tmp/runme/tls/cert.pem \
-key /tmp/runme/tls/key.pem \
-protoset <(buf build -o -) \
-d "{\"source\": \"$data\"}" \
127.0.0.1:9999 runme.parser.v1.ParserService/Deserialize
```
48 changes: 0 additions & 48 deletions examples/server/grpc.md

This file was deleted.

14 changes: 11 additions & 3 deletions internal/document/editor/editorservice/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func NewParserServiceServer(logger *zap.Logger) parserv1.ParserServiceServer {
func (s *parserServiceServer) Deserialize(_ context.Context, req *parserv1.DeserializeRequest) (*parserv1.DeserializeResponse, error) {
s.logger.Info("Deserialize", zap.ByteString("source", req.Source[:min(len(req.Source), 64)]))

identityResolver := identity.NewResolver(fromProtoRunmeIdentityToLifecycleIdentity(req.Options.Identity))
identityResolver := identity.NewResolver(fromProtoDeserializeReqOptionsToLifecycleIdentity(req.Options))
notebook, err := editor.Deserialize(req.Source, identityResolver)
if err != nil {
s.logger.Info("failed to call Deserialize", zap.Error(err))
Expand Down Expand Up @@ -237,15 +237,23 @@ func (*parserServiceServer) serializeCellOutputs(cell *parserv1.Cell, options *p
return outputs
}

func fromProtoRunmeIdentityToLifecycleIdentity(idt parserv1.RunmeIdentity) identity.LifecycleIdentity {
func fromProtoDeserializeReqOptionsToLifecycleIdentity(opt *parserv1.DeserializeRequestOptions) identity.LifecycleIdentity {
var idt parserv1.RunmeIdentity

if opt != nil {
idt = opt.Identity
}

switch idt {
case parserv1.RunmeIdentity_RUNME_IDENTITY_ALL:
return identity.AllLifecycleIdentity
case parserv1.RunmeIdentity_RUNME_IDENTITY_DOCUMENT:
return identity.DocumentLifecycleIdentity
case parserv1.RunmeIdentity_RUNME_IDENTITY_CELL:
return identity.CellLifecycleIdentity
default:
case parserv1.RunmeIdentity_RUNME_IDENTITY_UNSPECIFIED:
return identity.UnspecifiedLifecycleIdentity
default:
return identity.DefaultLifecycleIdentity
}
}

0 comments on commit bca0000

Please sign in to comment.