Skip to content

Instantly share code, notes, and snippets.

@jcalfee
Created April 21, 2026 13:20
Show Gist options
  • Select an option

  • Save jcalfee/e0141e7eaa90f4676f231b6c5b27797f to your computer and use it in GitHub Desktop.

Select an option

Save jcalfee/e0141e7eaa90f4676f231b6c5b27797f to your computer and use it in GitHub Desktop.
`ipfs-edit` — Tiny Interactive IPFS MFS Editor

A single-file bash script that makes editing and publishing IPFS content extremely simple.

It mounts the IPFS namespaces, drops you into your chosen MFS folder with a clear prompt, lets you edit normally, then publishes to any IPNS key when you type save.

Features

  • Three clean phases: Start → Edit → Publish/Cleanup
  • Shows every important ipfs command with set -o xtrace
  • Optional interactive prompts (no arguments needed)
  • Automatically restores your original working directory when done
  • Early error checking and clean exits
  • Works with any IPNS key and any MFS path

Installation

# 1. Save the script
curl -L https://gist.githubusercontent.com/.../ipfs-edit/raw/ipfs-edit -o ~/bin/ipfs-edit

# 2. Make it executable
chmod +x ~/bin/ipfs-edit

# 3. (Optional) Add ~/bin to PATH if not already there
echo 'export PATH="$HOME/bin:$PATH"' >> ~/.bashrc

Usage

# Recommended — interactive mode
ipfs-edit

# Quick mode — key only (path defaults to /<key>)
ipfs-edit public

What happens inside

  1. Shows your available IPNS keys (ipfs key list)
  2. Mounts /ipfs, /ipns, /mfs
  3. Changes into your MFS folder
  4. Prefixes your normal prompt so you always know where you are
  5. You edit files normally with any tools
  6. Type save → publishes to the chosen key and exits
  7. Type exit (or Ctrl+D) → discards changes and cleans up

Full Script

#!/bin/bash
# ipfs-edit — 3-part interactive IPFS MFS editor
# Usage:  ipfs-edit                    ← interactive
#         ipfs-edit <IPNS_KEY>         ← path defaults to /<key>
#         ipfs-edit <IPNS_KEY> <MFS_PATH>

# ──────────────────────────────────────────────────────────────
# Argument handling
# ──────────────────────────────────────────────────────────────
if [[ $# -eq 0 ]]; then
    echo "=== Available IPNS keys ==="
    ipfs key list | grep -v '^self$'
    echo
    read -r -p "Enter IPNS key name: " KEY
    if [[ -z "$KEY" ]]; then
        echo "Error: No key name entered." >&2
        exit 1
    fi

    read -r -p "Enter MFS path (default: /$KEY): " MFS_PATH
    [[ -z "$MFS_PATH" ]] && MFS_PATH="/$KEY"
elif [[ $# -eq 1 ]]; then
    KEY="$1"
    MFS_PATH="/$KEY"
elif [[ $# -eq 2 ]]; then
    KEY="$1"
    MFS_PATH="$2"
else
    echo "Usage: ${0##*/} [IPNS_KEY] [MFS_PATH]" >&2
    exit 1
fi

# Ensure path starts with slash
[[ $MFS_PATH == /* ]] || MFS_PATH="/$MFS_PATH"

# ──────────────────────────────────────────────────────────────
# Main script (executed directly)
# ──────────────────────────────────────────────────────────────
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
    ORIGINAL_PWD="$(pwd)"
    ORIGINAL_PS1="${PS1}"
    RC_FILE="/tmp/ipfs-edit-rc.$$"

    cat > "$RC_FILE" << 'EOF'
# Source user's .bashrc to get real PS1
[ -f ~/.bashrc ] && . ~/.bashrc
ORIGINAL_PS1="${PS1}"

ipfs-edit-start() {
    echo "=== PART 1: IPFS Edit Started ==="
    echo "MFS Path : $MFS_PATH"
    echo "IPNS Key : $KEY"

    command -v ipfs >/dev/null 2>&1 || { echo "Error: ipfs not in PATH"; exit 1; }
    ipfs id >/dev/null 2>&1 || { echo "Error: IPFS daemon not running"; exit 1; }

    (set -o xtrace; ipfs mount) >/dev/null 2>&1 || { echo "Failed to mount"; exit 1; }
    (set -o xtrace; ipfs files mkdir -p "$MFS_PATH") 2>&1 || { echo "Failed to mkdir"; exit 1; }

    if cd "/mfs${MFS_PATH}" 2>/dev/null; then
        export PS1="(IPFS-edit:${MFS_PATH}) [save | exit]
${ORIGINAL_PS1}"
        echo "You are now editing."
        echo "Type: save     → publish & exit"
        echo "      exit     → discard & exit"
    else
        echo "Error: Could not cd to /mfs${MFS_PATH}"
        exit 1
    fi
}

save() {
    echo "=== PART 2: Publishing ==="
    CID=$(set -o xtrace; ipfs files stat --hash "$MFS_PATH")
    [[ -z "$CID" ]] && { echo "Error: Could not read CID"; return 1; }
    echo "CID: $CID"

    (set -o xtrace; umount /ipns) 2>/dev/null || true
    (set -o xtrace; ipfs name publish -k "$KEY" "$CID")
    echo "Published successfully to key '$KEY'."
    ipfs-cleanup
}

ipfs-cleanup() {
    echo "=== PART 3: Cleanup ==="
    (set -o xtrace; umount /ipfs /mfs /ipns) 2>/dev/null || true
    export PS1="$ORIGINAL_PS1"
    echo "IPFS edit session finished."
}

trap 'ipfs-cleanup' EXIT
ipfs-edit-start
EOF

    env ORIGINAL_PS1="$ORIGINAL_PS1" ORIGINAL_PWD="$ORIGINAL_PWD" \
        MFS_PATH="$MFS_PATH" KEY="$KEY" \
        bash --rcfile "$RC_FILE" -i

    cd "$ORIGINAL_PWD"
    echo "Returned to original directory: $(pwd)"
    rm -f "$RC_FILE"
else
    echo "ipfs-edit functions loaded (sourced mode)."
fi

Save the script above as ipfs-edit, make it executable, and drop it in ~/bin/ (or anywhere in your $PATH).

Enjoy the cleanest way to edit and publish IPFS content! 🚀


Just copy everything above and create the gist. Let me know if you want a shorter version or any tweaks before you publish it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment