stateful / runme

DevOps Workflows Built with Markdown
https://runme.dev
Apache License 2.0
901 stars 30 forks source link

export function in shell cell? #578

Open shafr opened 1 month ago

shafr commented 1 month ago

Hi there, is there a way how to export functions in shell? So I can define one in the first cell and then use it afterwads in consequent cells ?

function fetch(){
    link=$1

    curl -L \
    -H "Accept: application/vnd.github+json" \
    -H "Authorization: Bearer $TOKEN" \
    -H "X-GitHub-Api-Version: 2022-11-28" \
    $link
}
fetch "https://api.github.com/"
sourishkrout commented 1 month ago

Hi @shafr, thanks for using Runme!

Runme currently does not support block-scope sharing of variables and functions. It's a design tradeoff to make Runme work right out of the box, across language runtimes (shell/bash, python, ruby, node/deno/bun etc), without any prerequisite requirements, specifically bash versions and configurations.

Essentially every cell is stateless across the notebook. However, you reference cell outputs (much like piping) and manipulate ENV variables which will be sticky across the whole Runme session and even across notebooks. More details here in our quick start guide.

You could make something similar work by sourcing the functions definitions in every cell block. Here's an example: https://gist.github.com/sourishkrout/f92132a1ec0dc5cbe79c8ffdbe81cbaa#file-exports-01HY1B7ASJ0517Q6FNGQBFWBK3.md

If it'd be helpful we could file a feature request which would allow declaring a script, e.g. exports.sh or a cell ID/name, in Runme's document-/cell-level config that would automatically source the exports ahead of executing every file. This would help reducing "documentation clutter".

Please let me know what you think.