Skip to content

Commit

Permalink
fix vars set on yaml arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
jonchenn committed Oct 24, 2023
1 parent 668296c commit 3dfeb9d
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 13 deletions.
3 changes: 2 additions & 1 deletion solutions_builder/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from .set import set_app
from .vars import vars_app
from .cli_utils import *
from .cli_constants import DEBUG

__version__ = importlib.metadata.version("solutions-builder")
DEFAULT_DEPLOY_PROFILE = "default-deploy"
Expand Down Expand Up @@ -236,7 +237,7 @@ def main():
print()

except Exception as e:
if os.getenv("DEBUG", False):
if DEBUG:
traceback.print_exc()
print_error(e)

Expand Down
18 changes: 18 additions & 0 deletions solutions_builder/cli/cli_constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright 2023 Google LLC

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

# https://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os

# Global DEBUG flag for all CLI routes.
DEBUG = (os.environ.get("SB_DEBUG", "").lower() == "true")
8 changes: 6 additions & 2 deletions solutions_builder/cli/cli_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@
limitations under the License.
"""

import os, yaml, typer, subprocess, re
import os
import yaml
import typer
import subprocess
import re


def confirm(msg, skip=False, default=True):
Expand Down Expand Up @@ -139,7 +143,7 @@ def list_subfolders(path):


def check_git_url(url):
regex_str = "((git|ssh|http(s)?)|(git@[\w\.]+))(:(//)?)([\w\.@\:/\-~]+)(\.git)(/)?"
regex_str = "((git|ssh|http(s)?)|(git@[\\w\\.]+))(:(//)?)([\\w\\.\\@\\:/\\-~]+)(\\.git)(/)?"
regex = re.compile(regex_str)
match = regex.match(url)
return match is not None
Expand Down
24 changes: 14 additions & 10 deletions solutions_builder/cli/vars.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,39 +20,40 @@
from typing_extensions import Annotated
from copier import run_auto
from .cli_utils import *
from .cli_constants import DEBUG


vars_app = typer.Typer()

INCLUDE_PATTERNS = [
"*.yaml", "*.yml", "*.env", "*.tfvars", "*.tf", "*.sh", "*.md"
"*.yaml", "*.yml", "*.env", "*.tfvars", "*.tf", "*.sh"
]
EXCLUDE_PATTERNS = ["**/.terraform/**/*.*", "**/node_modules/**/*.*", "**/.venv/**/*.*"]

# Replace a variable with a given text content.
def replace_var_to_template(var_name, text, custom_template=False, debug=False):
# This pattern matches lines with sb-var anchor in the comment at the end.
def replace_var_to_template(var_name, text, custom_template=False):
# Regex test: https://regex101.com/r/XtnJQI/4
# match_pattern matches lines with sb-var anchor in the comment at the end.
# For example:
# PROJECT_ID: 12345 # sb-var:project_id
# GCP_REGION = "us-central1" # sb-var:gcp_region
match_pattern = f"^([^\\r]*[:|=][\\s\-]*)([\"\']?)([^\"^\']*)([\"\']?)\\s*#\\s*sb-var:{var_name}"
match_pattern = f"(\\s*[\":=-][ ]*)(-[ ]*)?([\"\']?)([^\"^\'^\r^\n]*)([\"\']?)\\s*#\\s*sb-var:{var_name}"

# This output patterh print the jinja2 template for the specific variable name.
# output_pattern prints the jinja2 template for the specific variable name.
# For example:
# PROJECT_ID: {{project_id}} # sb-var:project_id
output_pattern = f"\\1\\2{{{{{var_name}}}}}\\4 # sb-var:{var_name}"
output_pattern = f"\\1\\2\\3{{{{{var_name}}}}}\\5 # sb-var:{var_name}"

# In addition, if custom_template is true, the pattern will extend to the custom
# template string at the end of the anchor. For example:
# BUCKET_NAME: my-project-bucket # sb-var:project_id:{{project_id}}-bucket
if custom_template:
match_pattern = match_pattern + ":(.*)"
output_pattern = f"\\1\\2\\5\\4 # sb-var:{var_name}:\\5"

if debug:
print(f"match_pattern = {match_pattern}")
output_pattern = f"\\1\\2\\3\\6\\5 # sb-var:{var_name}:\\6"

# Replace with regex pattern and returns new text and count of changes.
text, count = re.subn(match_pattern, output_pattern, text)

return (text, count)

def restore_template_in_comment(var_name, var_value, text):
Expand Down Expand Up @@ -109,6 +110,9 @@ def apply_var_to_folder(solution_path, var_name, var_value):

modified_files_list = []
for filename in list(file_set):
if DEBUG:
print(filename)

with open(filename, "r") as file:
# Replace variable
filedata = file.read()
Expand Down
40 changes: 40 additions & 0 deletions solutions_builder/cli/vars_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,43 @@ def test_replace_var_to_value_custom_template():
# FIXME: it double-counted the changes, as both simple template and custom
# template both counts.
assert count == 2

def test_replace_with_multiple_occurances():
text = """
env:
PROJECT_ID: old-value-1 # sb-var:project_id
PROJECT_ID_2: old-value-2 # sb-var:project_id
"""
text, count = replace_var_to_value("project_id", "fake-id", text)
assert text == """
env:
PROJECT_ID: fake-id # sb-var:project_id
PROJECT_ID_2: fake-id # sb-var:project_id
"""
assert count == 2


def test_replace_with_yaml_arrays():
text = """
array:
- not-replaced-yet # sb-var:project_id
"""
text, count = replace_var_to_value("project_id", "fake-id", text)
assert text == """
array:
- fake-id # sb-var:project_id
"""
assert count == 1

text = """
array:
- "not-replaced-yet" # sb-var:project_id
- "not-replaced-yet" # sb-var:project_id
"""
text, count = replace_var_to_value("project_id", "fake-id", text)
assert text == """
array:
- "fake-id" # sb-var:project_id
- "fake-id" # sb-var:project_id
"""
assert count == 2

0 comments on commit 3dfeb9d

Please sign in to comment.