Skip to content

Commit

Permalink
get rid of mix_env
Browse files Browse the repository at this point in the history
  • Loading branch information
mruoss committed Nov 3, 2023
1 parent 891b377 commit 55f68b2
Show file tree
Hide file tree
Showing 16 changed files with 24 additions and 60 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ npm start

```bash
mix escript.install hex livebook
MIX_ENV=dev livebook server ./dev.livemd
livebook server ./dev.livemd
```

### Build Assets
Expand Down
1 change: 1 addition & 0 deletions assets/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export interface Root {
}

declare global {
const DEBUG: boolean
const Terminal: typeof TerminalType
const React: typeof React
const ReactDOM: {
Expand Down
4 changes: 1 addition & 3 deletions assets/kino_terminal/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import xterm from 'xterm'

import { Attributes } from '../shared/types'

export interface KinoTerminalAttrs extends Pick<Attributes, 'mix_env'> {
export interface KinoTerminalAttrs {
buffer: string | Uint8Array
}

Expand Down
1 change: 0 additions & 1 deletion assets/shared/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
export interface Attributes {
error?: string
result_variable: string
mix_env: string
}

export interface GVK {
Expand Down
8 changes: 2 additions & 6 deletions assets/shared/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { KinoContext } from '../kino'
import { Attributes } from './types'

/* eslint-disable @typescript-eslint/no-explicit-any */
export const debounce = <T extends (...args: any[]) => ReturnType<T>>(
Expand All @@ -16,11 +15,8 @@ export const debounce = <T extends (...args: any[]) => ReturnType<T>>(
}
}

export const loadReact = async (
ctx: KinoContext,
attrs: Pick<Attributes, 'mix_env'>,
): Promise<void> => {
if (attrs.mix_env == 'dev') {
export const loadReact = async (ctx: KinoContext): Promise<void> => {
if (DEBUG) {
await ctx.importJS(
'https://cdnjs.cloudflare.com/ajax/libs/react/18.2.0/umd/react.development.js',
)
Expand Down
44 changes: 6 additions & 38 deletions dev.livemd
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Mix.install([{:kino_k8s, path: "~/src/community/kino_k8s"}])

## Section

<!-- livebook:{"attrs":{"mix_env":"dev","opts":{"context":"rancher-desktop","insecure_skip_tls_verify":true},"result_variable":"conn","source":"~/.kube/config","source_type":"file"},"chunks":null,"kind":"Elixir.KinoK8s.ConnectionCell","livebook_object":"smart_cell"} -->
<!-- livebook:{"attrs":{"opts":{"context":"rancher-desktop","insecure_skip_tls_verify":true},"result_variable":"conn","source":"~/.kube/config","source_type":"file"},"chunks":null,"kind":"Elixir.KinoK8s.ConnectionCell","livebook_object":"smart_cell"} -->

```elixir
{:ok, conn} =
Expand All @@ -21,35 +21,19 @@ Mix.install([{:kino_k8s, path: "~/src/community/kino_k8s"}])

```

<!-- livebook:{"attrs":{"connect_to":"exec","connect_tos":["exec","logs"],"connection":{"conn_hash":37669473,"variable":"conn"},"connections":[{"conn_hash":37669473,"variable":"conn"}],"container":"lb-tcp-80","containers":["lb-tcp-80","lb-tcp-443"],"mix_env":"dev","namespace":"kube-system","namespaces":["default","kube-system","kube-public","kube-node-lease","collab-api"],"pod":"svclb-traefik-d6dd8e93-fkqnz","pods":["svclb-traefik-d6dd8e93-fkqnz","traefik-66c46d954f-wqx7m","coredns-597584b69b-xxbkz","local-path-provisioner-79f67d76f8-b48ds","metrics-server-5f9f776df5-rvlgr"]},"chunks":null,"kind":"Elixir.KinoK8s.TerminalCell","livebook_object":"smart_cell"} -->
<!-- livebook:{"attrs":{"connect_to":"exec","connect_tos":["exec","logs"],"connection":null,"connections":[],"container":null,"containers":null,"namespace":null,"namespaces":null,"pod":null,"pods":null},"chunks":null,"kind":"Elixir.KinoK8s.TerminalCell","livebook_object":"smart_cell"} -->

```elixir
connect_to_pod = fn terminal_kino_pid ->
{:ok, send_to_websocket} =
K8s.Client.connect(
"v1",
"pods/exec",
[namespace: "kube-system", name: "svclb-traefik-d6dd8e93-fkqnz"],
container: "lb-tcp-80",
command: "/bin/sh",
tty: true
)
|> K8s.Client.put_conn(conn)
|> K8s.Client.stream_to(terminal_kino_pid)

send_to_websocket
end

KinoK8s.KinoTerminal.open(connect_to_pod)

```

<!-- livebook:{"attrs":{"connection":{"conn_hash":37669473,"variable":"conn"},"connections":[{"conn_hash":37669473,"variable":"conn"}],"gvk":null,"mix_env":"dev","namespace":null,"namespaces":null,"request_type":"get","request_types":["get","list","watch"],"resource":null,"resources":null,"result_type":null,"result_types":{"get":null,"list":["list","stream"],"watch":["stream"]},"result_variable":"result"},"chunks":null,"kind":"Elixir.KinoK8s.GetCell","livebook_object":"smart_cell"} -->
<!-- livebook:{"attrs":{"connection":{"conn_hash":37669473,"variable":"conn"},"connections":[{"conn_hash":37669473,"variable":"conn"}],"gvk":null,"namespace":null,"namespaces":null,"request_type":"get","request_types":["get","list","watch"],"resource":null,"resources":null,"result_type":null,"result_types":{"get":null,"list":["list","stream"],"watch":["stream"]},"result_variable":"result"},"chunks":null,"kind":"Elixir.KinoK8s.GetCell","livebook_object":"smart_cell"} -->

```elixir

```

<!-- livebook:{"attrs":{"connect_to":"exec","connect_tos":["exec","logs"],"connection":{"conn_hash":37669473,"variable":"conn"},"connections":[{"conn_hash":37669473,"variable":"conn"}],"container":null,"containers":null,"mix_env":"dev","namespace":"default","namespaces":["default","kube-system","kube-public","kube-node-lease","collab-api"],"pod":null,"pods":[]},"chunks":null,"kind":"Elixir.KinoK8s.TerminalCell","livebook_object":"smart_cell"} -->
<!-- livebook:{"attrs":{"connect_to":"exec","connect_tos":["exec","logs"],"connection":null,"connections":[],"container":null,"containers":null,"namespace":null,"namespaces":null,"pod":null,"pods":null},"chunks":null,"kind":"Elixir.KinoK8s.TerminalCell","livebook_object":"smart_cell"} -->

```elixir

Expand All @@ -58,21 +42,5 @@ KinoK8s.KinoTerminal.open(connect_to_pod)
<!-- livebook:{"attrs":{"body":"kind: ConfigMap\napiVersion: v1\nmetadata:\n name: kino-k8s-cm\n namespace: default\ndata:\n key: default\n","connection":{"conn_hash":37669473,"variable":"conn"},"connections":[{"conn_hash":37669473,"variable":"conn"}],"method":"create","methods":["apply","create","update"],"result_variable":"applied_resource"},"chunks":null,"kind":"Elixir.KinoK8s.ApplyCell","livebook_object":"smart_cell"} -->

```elixir
import YamlElixir.Sigil

{:ok, applied_resource} =
~y"""
kind: ConfigMap
apiVersion: v1
metadata:
name: kino-k8s-cm
namespace: default
data:
key: default
"""
|> K8s.Client.create()
|> K8s.Client.put_conn(conn)
|> K8s.Client.run()

applied_resource |> Ymlr.document!() |> IO.puts()

```
3 changes: 2 additions & 1 deletion lib/assets/apply_cell/main.js

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion lib/assets/connection_cell/main.js

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion lib/assets/get_cell/main.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions lib/assets/kino_terminal/main.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion lib/assets/terminal_cell/main.js

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions lib/kino_k8s/connection_cell.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ defmodule KinoK8s.ConnectionCell do
result_variable: Kino.SmartCell.prefixed_var_name("conn", attrs["result_variable"]),
source_type: attrs["source_type"] || @default_source_type,
source: attrs["source"] || @default_file,
opts: attrs["opts"] || %{"insecure_skip_tls_verify" => true},
opts: attrs["opts"] || %{"insecure_skip_tls_verify" => true}
# running_on_k8s: File.exists?("/var/run/secrets/kubernetes.io/serviceaccount"),
mix_env: Mix.env()
)

{:ok, ctx}
Expand Down
1 change: 0 additions & 1 deletion lib/kino_k8s/get_cell.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ defmodule KinoK8s.GetCell do
def init(attrs, ctx) do
ctx =
assign(ctx,
mix_env: Mix.env(),
connections: [],
connection: nil,
result_variable: Kino.SmartCell.prefixed_var_name("result", attrs["result_variable"]),
Expand Down
2 changes: 1 addition & 1 deletion lib/kino_k8s/kino_terminal.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ defmodule KinoK8s.KinoTerminal do
@impl true
def init(attrs, ctx) do
send_to_process = attrs.connect.(self())
{:ok, assign(ctx, mix_env: Mix.env(), buffer: [], send_to_process: send_to_process)}
{:ok, assign(ctx, buffer: [], send_to_process: send_to_process)}
end

@impl true
Expand Down
1 change: 0 additions & 1 deletion lib/kino_k8s/terminal_cell.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ defmodule KinoK8s.TerminalCell do
{:ok,
ctx
|> assign(
mix_env: Mix.env(),
connections: [],
connection: nil,
connect_tos: ["exec", "logs"],
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"tailwind:build:get_cell": "tailwindcss --content './assets/{get_cell,shared}/**/*.{ts,tsx}' -i assets/get_cell/main.css -o ./lib/assets/get_cell/main.css",
"tailwind:build:terminal_cell": "tailwindcss --content './assets/{terminal_cell,shared}/**/*.{ts,tsx}' -i assets/terminal_cell/main.css -o ./lib/assets/terminal_cell/main.css",
"tailwind": "npm-run-all --parallel tailwind:build:*",
"es:build": "esbuild assets/*/main.ts* --minify --external:react --external:react-dom/client --external:xterm --entry-names=[dir]/[name] --outbase=assets --outdir=lib/assets --target=es2020 --format=esm --bundle",
"es:watch": "npm run es:build -- --sourcemap=inline --watch",
"es:build": "esbuild assets/*/main.ts* --define:DEBUG=false --minify --external:react --external:react-dom/client --external:xterm --entry-names=[dir]/[name] --outbase=assets --outdir=lib/assets --target=es2020 --format=esm --bundle",
"es:watch": "npm run es:build -- --sourcemap=inline --watch --define:DEBUG=true",
"start": "npm-run-all --parallel 'tailwind:build:* -- --watch' es:watch",
"build": "npm run clean && npm-run-all --parallel tailwind es:build",
"clean": "rm -r lib/assets"
Expand Down

0 comments on commit 55f68b2

Please sign in to comment.