dolang-shell-modules 0.1.0

Stock embedded Do modules for the shell.
import xdg _vfs
  shell:
    - Vfs
  fs:
    - with_temp_dir
  proc.run:
    - sudo
  strand:
    - pipeline

def run_agent
  thunk
  :user = nil
do
  let runtime_dir = xdg.runtime_dir()
  let agent = _vfs.bin()

  with_temp_dir parent: $runtime_dir do |subdir|
    subdir.chmod 0o700
    let socket = (subdir / "socket")
    let input = strand.input()
    pipeline
      do sudo -E
        if (user != nil)
          -u $user
        $agent $socket
      do
        strand.next()
        let a = Vfs.unix_socket $socket
        return strand.redirect input: $input do a $thunk

def run_command
  cmd
  :user = nil
  ...args
do
  sudo
    if (user != nil)
      -u $user
    --
    $cmd ...args

# Run a verbatim command through `sudo`.
#
# Pass `user` to run as a user other than root.
#
# **Example:**
# ```
# i
pub def run
  cmd
  :user = nil
  ...args
do
  run_command $cmd :user ...args.pos_only()

# Run a function with elevated privileges via `sudo`.
#
# Filesystem access and spawned programs will be redirected for
# the duration of the function.
#
# Pass `user` to run as a specific user instead of root.
#
# **Example:**
# ```
# import fs
# let shadow = sudo.with do fs.read /etc/shadow
# ```
pub def with
  func
  :user = nil
do
  run_agent $func :user