From 2c588c51a32ff7966ae674729122689eb9a43882 Mon Sep 17 00:00:00 2001 From: saravanakumardb1 Date: Thu, 12 Feb 2026 20:38:57 -0800 Subject: [PATCH] refactor(scripts): simplify switch-network to env-var based approach MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Single env var NETWORK=corp|home controls all proxy config. Source from ~/.zshrc — sets http_proxy, NPM_CONFIG_REGISTRY, PIP_TRUSTED_HOST, NODE_TLS_REJECT_UNAUTHORIZED automatically. No more ~/.npmrc rewriting. --- scripts/switch-network.sh | 115 ++++++++++++++------------------------ 1 file changed, 43 insertions(+), 72 deletions(-) diff --git a/scripts/switch-network.sh b/scripts/switch-network.sh index 91bd0cd8..e3fb8fe8 100755 --- a/scripts/switch-network.sh +++ b/scripts/switch-network.sh @@ -1,81 +1,52 @@ #!/bin/bash -# Toggle npm/pnpm between corporate proxy and direct (home) network. +# ───────────────────────────────────────────────────────────── +# Dual-network development setup +# ───────────────────────────────────────────────────────────── # -# Usage: -# source scripts/switch-network.sh corp # AT&T / corporate proxy -# source scripts/switch-network.sh home # Direct internet (home/VPN off) -# source scripts/switch-network.sh status # Show current config +# Controls: one env var — NETWORK=corp or NETWORK=home # -# This modifies ~/.npmrc. Must be sourced (not executed) to affect current shell. +# Usage (add to ~/.zshrc): +# export NETWORK=corp # at work +# export NETWORK=home # at home (or just unset it) +# +# Then source this file from ~/.zshrc: +# source "$HOME/code/mygh/learning_ai_common_plat/scripts/switch-network.sh" +# +# What it sets: +# NETWORK=corp → http_proxy, https_proxy, npm/pnpm registry, pip trusted-host +# NETWORK=home → all proxy vars unset, default registries +# ───────────────────────────────────────────────────────────── -set -euo pipefail +_CORP_PROXY="http://cso.proxy.att.com:8080/" +_CORP_NPM_REGISTRY="https://jfrog-pkg-proxy.it.att.com/artifactory/api/npm/att-npm-proxy-group/" -NPMRC="$HOME/.npmrc" -CORP_PROXY="http://cso.proxy.att.com:8080/" -CORP_REGISTRY="https://jfrog-pkg-proxy.it.att.com/artifactory/api/npm/att-npm-proxy-group/" -HOME_REGISTRY="https://registry.npmjs.org/" +if [ "${NETWORK:-home}" = "corp" ]; then + # ── Corporate proxy ── + export http_proxy="$_CORP_PROXY" + export https_proxy="$_CORP_PROXY" + export HTTP_PROXY="$_CORP_PROXY" + export HTTPS_PROXY="$_CORP_PROXY" + export NPM_CONFIG_REGISTRY="$_CORP_NPM_REGISTRY" + export NPM_CONFIG_PROXY="$_CORP_PROXY" + export NPM_CONFIG_HTTPS_PROXY="$_CORP_PROXY" + export NPM_CONFIG_STRICT_SSL="false" + export PIP_TRUSTED_HOST="pypi.org files.pythonhosted.org" + export NODE_TLS_REJECT_UNAUTHORIZED="0" +else + # ── Home / direct internet ── + unset http_proxy https_proxy HTTP_PROXY HTTPS_PROXY 2>/dev/null + unset NPM_CONFIG_REGISTRY NPM_CONFIG_PROXY NPM_CONFIG_HTTPS_PROXY 2>/dev/null + unset NPM_CONFIG_STRICT_SSL NODE_TLS_REJECT_UNAUTHORIZED 2>/dev/null + unset PIP_TRUSTED_HOST 2>/dev/null +fi -show_status() { - echo "=== Current npm config ===" - echo " proxy: $(npm config get proxy 2>/dev/null || echo 'not set')" - echo " https-proxy: $(npm config get https-proxy 2>/dev/null || echo 'not set')" - echo " registry: $(npm config get registry 2>/dev/null || echo 'not set')" - if command -v pnpm &>/dev/null; then - echo " pnpm store: $(pnpm store path 2>/dev/null || echo 'unknown')" - fi - echo "" - if [ -f "$NPMRC" ]; then - echo "=== ~/.npmrc ===" - cat "$NPMRC" +# Quick status on new shell (only if interactive) +if [[ $- == *i* ]]; then + if [ "${NETWORK:-home}" = "corp" ]; then + echo "🏢 NETWORK=corp — proxy active" else - echo "No ~/.npmrc found" + echo "🏠 NETWORK=home — direct internet" fi -} +fi -set_corp() { - cat > "$NPMRC" < "$NPMRC" <