Add a -v flag to gen.sh to use `lucicfg validate`

Because `gen.sh` runs `main.star` multiple times with different vars
set, it's not easy to manually construct a `lucicfg validate` command
line to validate the configs. This flag makes it easy to validate all
the projects in a single command.

Bug: 76644
Change-Id: I87bc9751953f490e4ac3fd0b654027faa851b972
Reviewed-on: https://fuchsia-review.googlesource.com/c/infra/config/+/530282
Reviewed-by: Marc-Antoine Ruel <maruel@google.com>
diff --git a/gen.sh b/gen.sh
index 729c33e..f5bd7b4 100755
--- a/gen.sh
+++ b/gen.sh
@@ -3,6 +3,8 @@
 # Use of this source code is governed by a BSD-style license that can be
 # found in the LICENSE file.
 
+set -eu -o pipefail
+
 declare -A repos_and_builders=(
     ["cobalt"]="tricium-cobalt"
     ["fuchsia"]="tricium"
@@ -11,7 +13,26 @@
     ["topaz"]="tricium"
 )
 
+validate=false
+
+while getopts v opt; do
+    case "$opt" in
+        v)
+            # Use the -v flag to run `lucicfg validate` instead of `lucicfg
+            # generate`.
+            validate=true
+            ;;
+        \?)
+            exit 2
+            ;;
+    esac
+done
+
 for repo in "${!repos_and_builders[@]}"; do
     builder="${repos_and_builders[$repo]}"
-    lucicfg generate -var repo="$repo" -var builder="$builder" main.star
+    cmd="generate"
+    if $validate; then
+        cmd="validate"
+    fi
+    lucicfg "$cmd" -var repo="$repo" -var builder="$builder" main.star
 done