← back to Koroseal Goodrich Audit

spec_backfill.sh

45 lines

#!/bin/bash
# Koroseal spec backfill from koroseal.com -> custom.<key> metafields, ONLY where empty.
# Steve-approved 2026-07-15. canary=FB21 series first; all=the 1,573 staged.
set -uo pipefail
TOKEN="$(grep '^SHOPIFY_ADMIN_TOKEN=' ~/Projects/secrets-manager/.env | cut -d= -f2)"
BASE="https://designer-laboratory-sandbox.myshopify.com/admin/api/2024-10"
H="X-Shopify-Access-Token: $TOKEN"
RUN=~/Projects/koroseal-goodrich-audit/koroseal_spec_backfill_staged.tsv
MODE="${1:-canary}"
FILTER='^FB21-'; [ "$MODE" = "all" ] && FILTER='.'

# Koroseal spec label -> custom metafield key : type
map_key(){ case "$1" in
  Material) echo "material|multi_line_text_field";;
  Backing) echo "backing|single_line_text_field";;
  "Pattern Match") echo "pattern_match|single_line_text_field";;
  "Fire Rating") echo "fire_rating|single_line_text_field";;
  "Roll Width") echo "roll_width|single_line_text_field";;
  "Horizontal Repeat") echo "horizontal_repeat|single_line_text_field";;
  "Vertical Repeat") echo "vertical_repeat|single_line_text_field";;
  Designer) echo "designer|single_line_text_field";;
  *) echo "";; esac; }

grep -E "$FILTER" "$RUN" | while IFS=$'\t' read -r mfr pid dwsku how spec; do
  [ -z "${pid:-}" ] && continue
  # existing custom keys on this product (don't clobber)
  have=$(curl -s "$BASE/products/$pid/metafields.json?namespace=custom" -H "$H" | python3 -c 'import sys,json;print(" ".join(m["key"] for m in json.load(sys.stdin).get("metafields",[])))' 2>/dev/null)
  wrote=0
  # iterate spec fields
  while IFS=$'\t' read -r label value; do
    [ -z "$label" ] && continue
    km=$(map_key "$label"); [ -z "$km" ] && continue
    key="${km%%|*}"; typ="${km##*|}"
    case " $have " in *" $key "*) continue;; esac   # skip if present
    body=$(python3 -c "import json,sys;print(json.dumps({'metafield':{'namespace':'custom','key':'$key','type':'$typ','value':sys.argv[1]}}))" "$value")
    rc=$(curl -s -o /dev/null -w "%{http_code}" -X POST "$BASE/products/$pid/metafields.json" -H "$H" -H "Content-Type: application/json" -d "$body")
    [ "$rc" = "201" ] && wrote=$((wrote+1))
    sleep 0.25
  done < <(echo "$spec" | python3 -c 'import sys,json
for k,v in json.load(sys.stdin).items(): print(k+"\t"+str(v))')
  echo "[$mfr] $how -> wrote $wrote metafields"
  sleep 0.3
done
echo "SPEC-BACKFILL DONE ($MODE)"