|
| 1 | +#!/bin/bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +BOLD="\033[1m" |
| 5 | +GREEN="\033[0;32m" |
| 6 | +YELLOW="\033[0;33m" |
| 7 | +CYAN="\033[0;36m" |
| 8 | +RED="\033[0;31m" |
| 9 | +RESET="\033[0m" |
| 10 | + |
| 11 | +GOMOD="go.mod" |
| 12 | +COMMENT_REPLACE=true |
| 13 | +# Used for go get / go mod tidy when resolving modules (override e.g. for a corporate proxy). |
| 14 | +JFROG_DEPS_GOPROXY="${JFROG_DEPS_GOPROXY:-direct}" |
| 15 | + |
| 16 | +usage() { |
| 17 | + echo -e "${BOLD}Usage:${RESET} $0 [OPTIONS] [dep1 dep2 ...]" |
| 18 | + echo |
| 19 | + echo "Update JFrog Go dependencies to their latest versions." |
| 20 | + echo "With no dependency names, updates all known JFrog modules; otherwise only those listed." |
| 21 | + echo |
| 22 | + echo -e "${BOLD}Options:${RESET}" |
| 23 | + echo " -a, --all Explicitly update all JFrog dependencies (same as passing no names)" |
| 24 | + echo " --keep-replace Don't comment out active 'replace' directives (default: comment them out)" |
| 25 | + echo " -h, --help Show this help message" |
| 26 | + echo |
| 27 | + echo -e "${BOLD}Environment:${RESET}" |
| 28 | + echo " JFROG_DEPS_GOPROXY GOPROXY for go get / go mod tidy (default: direct)" |
| 29 | + echo |
| 30 | + echo -e "${BOLD}Individual dependencies (pass one or more):${RESET}" |
| 31 | + echo " client-go github.com/jfrog/jfrog-client-go @master" |
| 32 | + echo " cli-core github.com/jfrog/jfrog-cli-core/v2 @master" |
| 33 | + echo " cli-artifactory github.com/jfrog/jfrog-cli-artifactory @main" |
| 34 | + echo " build-info-go github.com/jfrog/build-info-go @main" |
| 35 | + echo " cli-security github.com/jfrog/jfrog-cli-security @latest (tag)" |
| 36 | + echo " froggit-go github.com/jfrog/froggit-go @latest (tag)" |
| 37 | + echo " gofrog github.com/jfrog/gofrog @latest (tag)" |
| 38 | + echo |
| 39 | + echo -e "${BOLD}Examples:${RESET}" |
| 40 | + echo " $0 # Update all JFrog deps" |
| 41 | + echo " $0 --all # Same as no arguments" |
| 42 | + echo " $0 client-go cli-core # Update only those" |
| 43 | + echo " $0 --keep-replace # Update all, leave replace directives as-is" |
| 44 | + echo " JFROG_DEPS_GOPROXY=https://proxy.golang.org $0 # Use a different GOPROXY" |
| 45 | +} |
| 46 | + |
| 47 | +log_info() { echo -e "${CYAN}[INFO]${RESET} $*"; } |
| 48 | +log_ok() { echo -e "${GREEN}[OK]${RESET} $*"; } |
| 49 | +log_warn() { echo -e "${YELLOW}[WARN]${RESET} $*"; } |
| 50 | +log_error() { echo -e "${RED}[ERROR]${RESET} $*"; } |
| 51 | + |
| 52 | +ALL_KEYS="client-go cli-core cli-artifactory cli-security build-info-go froggit-go gofrog" |
| 53 | + |
| 54 | +resolve_dep() { |
| 55 | + case "$1" in |
| 56 | + client-go) echo "github.com/jfrog/jfrog-client-go|master" ;; |
| 57 | + cli-core) echo "github.com/jfrog/jfrog-cli-core/v2|master" ;; |
| 58 | + cli-artifactory) echo "github.com/jfrog/jfrog-cli-artifactory|main" ;; |
| 59 | + build-info-go) echo "github.com/jfrog/build-info-go|main" ;; |
| 60 | + cli-security) echo "github.com/jfrog/jfrog-cli-security|latest" ;; |
| 61 | + froggit-go) echo "github.com/jfrog/froggit-go|latest" ;; |
| 62 | + gofrog) echo "github.com/jfrog/gofrog|latest" ;; |
| 63 | + *) return 1 ;; |
| 64 | + esac |
| 65 | +} |
| 66 | + |
| 67 | +comment_out_jfrog_replaces() { |
| 68 | + if [[ ! -f "$GOMOD" ]]; then |
| 69 | + log_error "Cannot find $GOMOD" |
| 70 | + return 1 |
| 71 | + fi |
| 72 | + |
| 73 | + local count |
| 74 | + count=$(grep -cE '^[[:space:]]*replace[[:space:]]+github\.com/jfrog/' "$GOMOD" 2>/dev/null || true) |
| 75 | + |
| 76 | + if [[ "$count" -eq 0 ]]; then |
| 77 | + log_info "No active jfrog replace directives found" |
| 78 | + return 0 |
| 79 | + fi |
| 80 | + |
| 81 | + log_warn "Found ${BOLD}${count}${RESET} active jfrog replace directive(s) — commenting out" |
| 82 | + |
| 83 | + # macOS sed requires '' after -i; use a temp file for portability |
| 84 | + local tmp |
| 85 | + tmp=$(mktemp) |
| 86 | + while IFS= read -r line; do |
| 87 | + if echo "$line" | grep -qE '^[[:space:]]*replace[[:space:]]+github\.com/jfrog/'; then |
| 88 | + log_info " Commenting: ${line}" |
| 89 | + echo "// ${line}" >> "$tmp" |
| 90 | + else |
| 91 | + echo "$line" >> "$tmp" |
| 92 | + fi |
| 93 | + done < "$GOMOD" |
| 94 | + mv "$tmp" "$GOMOD" |
| 95 | + log_ok "Replace directives commented out" |
| 96 | +} |
| 97 | + |
| 98 | +update_dep() { |
| 99 | + local key="$1" |
| 100 | + local entry |
| 101 | + entry=$(resolve_dep "$key") || { log_error "Unknown dependency: ${key} (known: ${ALL_KEYS})"; return 1; } |
| 102 | + local module="${entry%%|*}" |
| 103 | + local ref="${entry##*|}" |
| 104 | + log_info "Updating ${BOLD}${key}${RESET} → ${module}@${ref}" |
| 105 | + if GOPROXY="$JFROG_DEPS_GOPROXY" go get "${module}@${ref}"; then |
| 106 | + log_ok "${key} updated" |
| 107 | + else |
| 108 | + log_error "Failed to update ${key}" |
| 109 | + return 1 |
| 110 | + fi |
| 111 | +} |
| 112 | + |
| 113 | +# --- Main --- |
| 114 | + |
| 115 | +explicit_all=false |
| 116 | +specific_deps=() |
| 117 | + |
| 118 | +while [[ $# -gt 0 ]]; do |
| 119 | + case "$1" in |
| 120 | + -a|--all) explicit_all=true; shift ;; |
| 121 | + --keep-replace) COMMENT_REPLACE=false; shift ;; |
| 122 | + -h|--help) usage; exit 0 ;; |
| 123 | + -*) log_error "Unknown option: $1"; usage; exit 1 ;; |
| 124 | + *) specific_deps+=("$1"); shift ;; |
| 125 | + esac |
| 126 | +done |
| 127 | + |
| 128 | +# Comment out active replace directives before updating |
| 129 | +if [[ "$COMMENT_REPLACE" == true ]]; then |
| 130 | + comment_out_jfrog_replaces |
| 131 | + echo |
| 132 | +fi |
| 133 | + |
| 134 | +failed=0 |
| 135 | +keys_to_update="" |
| 136 | + |
| 137 | +if ((${#specific_deps[@]} > 0)); then |
| 138 | + if [[ "$explicit_all" == true ]]; then |
| 139 | + log_warn "${BOLD}--all${RESET} is ignored when dependency names are listed" |
| 140 | + fi |
| 141 | + log_info "Updating ${BOLD}${specific_deps[*]}${RESET}…" |
| 142 | + echo |
| 143 | + keys_to_update="${specific_deps[*]}" |
| 144 | +else |
| 145 | + log_info "Updating ${BOLD}all${RESET} JFrog dependencies…" |
| 146 | + echo |
| 147 | + keys_to_update="$ALL_KEYS" |
| 148 | +fi |
| 149 | + |
| 150 | +for dep in $keys_to_update; do |
| 151 | + update_dep "$dep" || ((failed++)) || true |
| 152 | +done |
| 153 | + |
| 154 | +echo |
| 155 | +if [[ $failed -gt 0 ]]; then |
| 156 | + log_warn "${failed} update(s) failed" |
| 157 | +else |
| 158 | + log_ok "All updates succeeded" |
| 159 | +fi |
| 160 | + |
| 161 | +log_info "Running go mod tidy…" |
| 162 | +GOPROXY="$JFROG_DEPS_GOPROXY" go mod tidy |
| 163 | +log_ok "go mod tidy done" |
| 164 | + |
| 165 | +log_info "Running go vet ./…" |
| 166 | +if ! go vet ./...; then |
| 167 | + log_error "go vet failed" |
| 168 | + exit 1 |
| 169 | +fi |
| 170 | +log_ok "go vet passed" |
| 171 | + |
| 172 | +exit "$failed" |
0 commit comments