HOWTO: Programatically set up SyncThing via the bulit-in CLI command.

To add a script like demonstration of the original guide, I have created a Taskfile that I have used to test this procedure:

version: 3

includes:
  test: TestTasks.yaml

env:
  SYNCTHING_A_ADDRESS: http://localhost:8385
  SYNCTHING_B_ADDRESS: http://localhost:8386
  PODMAN_A: podman exec syncthingA
  PODMAN_B: podman exec syncthingB
  SYNCTHING_A_CLI: "{{ .PODMAN_A }} syncthing cli"
  SYNCTHING_B_CLI: "{{ .PODMAN_B }} syncthing cli"
  SYNCTHING_LOCAL: "{{ .SYNCTHING_A_CLI }}"
  SYNCTHING_REMOTE: "{{ .SYNCTHING_B_CLI }}"
  DEFAULT_FOLDER_PATH: "/config"

tasks:
  default:
    desc: List available tasks
    cmds:
      - task -l

  eval:devices:
    desc: Return device IDs to be evaluated
    cmds:
      - |
        echo ID_A=$( {{ .SYNCTHING_A_CLI }} show system | jq -r .myID )
        echo ID_B=$( {{ .SYNCTHING_B_CLI }} show system | jq -r .myID )

  eval:folder:
    desc: Return folder ID to be evaluated
    cmds:
      - |
        echo FOLDER_ID=$( {{ .SYNCTHING_A_CLI }} config folders list )

  server:up:
    aliases: [server]
    desc: Start the serves
    cmds:
      - podman-compose up -d

  server:down:
    desc: Stop the serves
    cmds:
      - podman-compose down

  test:share-and-accept:
    cmds:
      - task: server:up
      - task: show
      - task: handshake
      - task: create-folder
      - task: show
      - task: share-folder
      - task: accept-folder
      - task: create-file-a
      - task: create-file-b
      - sleep 8
      - task: check-file-a
      - task: check-file-b

  show:
    desc: Show the configs
    cmds:
      - |
        eval $(task eval:devices)
        eval $(task eval:folder)

        echo "A is: ${ID_A}"
        echo "B is: ${ID_B}"
        echo "Folder is: ${FOLDER_ID}"

  handshake:
    desc: Let the devices get to know each other
    cmds:
      - |
        eval $(task eval:devices)

        {{ .SYNCTHING_A_CLI }} config devices add --device-id $ID_B
        {{ .SYNCTHING_B_CLI }} config devices add --device-id $ID_A

  create-folder:
    desc: Create a folder on A
    cmds:
      - |
        eval $(task eval:devices)
        FOLDER_ID="$(pwgen -A 5)-$(pwgen -A 5)"

        {{ .SYNCTHING_A_CLI }} config folders add --id $FOLDER_ID --path /config/folder_example --label Example
        echo $FOLDER_ID

  share-folder:
    desc: Share a folder from A to B
    cmds:
      - |
        eval $(task eval:devices)
        eval $(task eval:folder)

        {{ .SYNCTHING_A_CLI }} config folders $FOLDER_ID devices add --device-id $ID_B

  accept-folder:
    desc: Accept the shared folder on B
    cmds:
      - |
        eval $(task eval:devices)
        eval $(task eval:folder)

        FOLDER_PATH=$({{.SYNCTHING_A_CLI}} config dump-json | jq -r ".folders[] | select(.id==\"$FOLDER_ID\") | .path")
        FOLDER_LABEL=$({{.SYNCTHING_A_CLI}} config dump-json | jq -r ".folders[] | select(.id==\"$FOLDER_ID\") | .label")

        {{ .SYNCTHING_B_CLI }} config folders add --id $FOLDER_ID --path $FOLDER_PATH --label $FOLDER_LABEL
        {{ .SYNCTHING_B_CLI }} config folders $FOLDER_ID devices add --device-id $ID_A

  create-file-a:
    desc: Create a test file (a_file) on A
    cmds:
      - "{{ .PODMAN_A }} touch /config/folder_example/a_file"

  check-file-a:
    desc: Check if a_file exists on B
    cmds:
      - "{{ .PODMAN_B }} stat /config/folder_example/a_file"

  create-file-b:
    desc: Create a test file (b_file) on B
    cmds:
      - "{{ .PODMAN_B }} touch /config/folder_example/b_file"

  check-file-b:
    desc: Check if b_file exists in A
    cmds:
      - "{{ .PODMAN_A }} stat /config/folder_example/b_file"

This works nicely in combination with the following podman/docker compose file:

version: "3.7"

x-syncthing: &syncthing
  image: lscr.io/linuxserver/syncthing:latest
  environment:
    - PUID=1000
    - PGID=1000
    - TZ=Etc/UTC
    # config and data can not be set, since the init process sets --home
    # - STCONFDIR=/config
    # - STDATADIR=/data
    # this is set in accordance to the linuxserver init command
    - STHOMEDIR=/config

services:
  syncthingA:
    <<: *syncthing
    container_name: syncthingA
    volumes:
      - dataa:/data
    ports:
      - 8385:8384
      - 22001:22000/tcp
      - 22001:22000/udp
      - 21028:21027/udp
  syncthingB:
    <<: *syncthing
    container_name: syncthingB
    volumes:
      - datab:/data
    ports:
      - 8386:8384
      - 22002:22000/tcp
      - 22002:22000/udp
      - 21029:21027/udp

volumes:
  dataa:
  datab:

As you see, I have executed everything with podman, but it should work with docker as well.