1+ #! /bin/bash
2+ 
3+ #  Function to print usage
4+ print_usage () {
5+     echo  " Usage: $0  <component-name> <new-version>" 
6+     echo  " Example: $0  container-toolkit 1.17.9" 
7+     exit  1
8+ }
9+ 
10+ #  Check if yq is installed
11+ check_yq () {
12+     if  !  command  -v yq & >  /dev/null;  then 
13+         echo  " Error: yq is not installed" 
14+         echo  " Please install yq first. On macOS, you can use: brew install yq" 
15+         exit  1
16+     fi 
17+ }
18+ 
19+ #  Validate input parameters
20+ if  [[ $#  -ne  2 ]];  then 
21+     print_usage
22+ fi 
23+ 
24+ COMPONENT=" $1 " 
25+ NEW_VERSION=" $2 " 
26+ 
27+ #  Check if yq is available
28+ check_yq
29+ 
30+ #  Validate component exists both as a directory and in repo.toml
31+ if  [[ !  -d  " $COMPONENT "   ]];  then 
32+     echo  " Error: Component directory '${COMPONENT} ' not found" 
33+     exit  1
34+ fi 
35+ 
36+ #  Check if the version field exists and get current version
37+ CURRENT_VERSION=$( yq " .repo_docs.projects.${COMPONENT} .version"   repo.toml) 
38+ if  [[ " $CURRENT_VERSION "   ==  " null"   ]];  then 
39+     echo  " Error: Component '${COMPONENT} ' version field not found in repo.toml" 
40+     exit  1
41+ fi 
42+ 
43+ #  Update versions.json if it exists
44+ VERSIONS_JSON=" $COMPONENT /versions.json" 
45+ if  [[ -f  " $VERSIONS_JSON "   ]];  then 
46+     echo  " Updating $VERSIONS_JSON ..." 
47+     yq -p=json -o=json -i ' . = [{"preferred": "true", "url": "../' " ${NEW_VERSION} " ' ", "version": "' " ${NEW_VERSION} " ' "}] + [(.[0] | del(.preferred))] + .[1:]'   " $VERSIONS_JSON " 
48+     if  [[ $?  -ne  0 ]];  then 
49+         echo  " Error: Failed to update $VERSIONS_JSON " 
50+         exit  1
51+     fi 
52+ fi 
53+ 
54+ #  Update version in repo.toml using sed
55+ echo  " Updating version in repo.toml..." 
56+ #  Remove quotes from current version for sed
57+ CURRENT_VERSION=${CURRENT_VERSION// \" / } 
58+ if  [[ $( uname)   ==  " Darwin"   ]];  then 
59+     #  macOS requires an empty string for -i
60+     sed -i ' '   " s/version = \" ${CURRENT_VERSION} \" /version = \" ${NEW_VERSION} \" /"   repo.toml
61+ else 
62+     #  Linux version of sed
63+     sed -i " s/version = \" ${CURRENT_VERSION} \" /version = \" ${NEW_VERSION} \" /"   repo.toml
64+ fi 
65+ 
66+ if  [[ $?  -ne  0 ]];  then 
67+     echo  " Error: Failed to update repo.toml" 
68+     exit  1
69+ fi 
70+ 
71+ echo  " Successfully updated version to ${NEW_VERSION}  for ${COMPONENT} " 
0 commit comments