Skip to content

Commit 59f9ce3

Browse files
committed
Fixed bug
1 parent 13fff05 commit 59f9ce3

2 files changed

Lines changed: 60 additions & 15 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ Ensure your code is always clean and adheres to best practices by integrating `c
6565

6666
```yaml
6767
repos:
68-
- repo: https://github.com/your-username/cfn-inline-lambda-linter
68+
- repo: https://github.com/saad1998/cfn-inline-lambda-linter
6969
rev: v0.1.0 # Replace with the latest version
7070
hooks:
7171
- id: cfn-inline-lambda-linter

cfn_inline_lambda_linter/linter.py

Lines changed: 59 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,13 @@ def findLambdaResources(resources):
6565

6666

6767

68-
def extractLambdaCode(resources, dict_to_check, args=None):
68+
def extractLambdaCode(resources, parameters, dict_to_check, args=None):
6969
"""
7070
Processes Lambda resources and checks their inline code for syntax errors using flake8.
7171
7272
Args:
7373
resources (dict): Dictionary containing resource definitions.
74+
parameters (dict): Dictionary containing parameter definitions.
7475
dict_to_check (dict): Dictionary of resources to check.
7576
args (str): Template dictionary (not used directly in this function).
7677
@@ -86,24 +87,67 @@ def extractLambdaCode(resources, dict_to_check, args=None):
8687
try:
8788
if "ZipFile" not in resources[i]["Properties"]["Code"]:
8889
raise KeyError(f"'ZipFile' key missing in resource '{i}' code properties.")
89-
9090
lambda_code = resources[i]["Properties"]["Code"]["ZipFile"]
9191
programming_lang = resources[i]["Properties"]["Runtime"]
9292
if not isinstance(lambda_code, str):
9393
raise ValueError(f"Expected a string for 'ZipFile' content in resource '{i}', got {type(lambda_code)}.")
9494

9595
print(Fore.CYAN + f"🔍 Checking resource '{i}' for code linting..." + Style.RESET_ALL)
96-
if "python" in programming_lang:
97-
if args is None:
98-
process = subprocess.Popen(
99-
['python', '-m', 'flake8', "-"],
100-
stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE
101-
)
96+
97+
if "!Ref" in programming_lang or "Fn::Ref" in programming_lang:
98+
print(programming_lang)
99+
param = programming_lang.strip().split()[-1]
100+
print(param)
101+
if param in parameters:
102+
val = parameters[param]["Default"]
103+
if "python" in val:
104+
if args is None:
105+
process = subprocess.Popen(
106+
['python', '-m', 'flake8', "-"],
107+
stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE
108+
)
109+
else:
110+
process = subprocess.Popen(
111+
['python', '-m', 'flake8', "-"] + args.split(),
112+
stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE
113+
)
114+
else:
115+
raise ValueError(f"❌ Found a programming language that is not supported at the moment")
116+
else:
117+
raise KeyError(f"❌ Parameter definition for parameter {param} is missing")
118+
elif ("!Ref" not in programming_lang or "Fn::Ref" not in programming_lang) and ("python" not in programming_lang):
119+
if programming_lang in parameters:
120+
val = parameters[programming_lang]["Default"]
121+
if "python" in val:
122+
if args is None:
123+
process = subprocess.Popen(
124+
['python', '-m', 'flake8', "-"],
125+
stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE
126+
)
127+
else:
128+
process = subprocess.Popen(
129+
['python', '-m', 'flake8', "-"] + args.split(),
130+
stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE
131+
)
132+
else:
133+
raise ValueError(f"❌ Found a programming language that is not supported at the moment")
102134
else:
103-
process = subprocess.Popen(
104-
['python', '-m', 'flake8', "-"] + args.split(),
105-
stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE
106-
)
135+
raise KeyError(f"❌ Parameter definition for parameter {param} is missing")
136+
else:
137+
if "python" in programming_lang:
138+
if args is None:
139+
process = subprocess.Popen(
140+
['python', '-m', 'flake8', "-"],
141+
stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE
142+
)
143+
else:
144+
process = subprocess.Popen(
145+
['python', '-m', 'flake8', "-"] + args.split(),
146+
stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE
147+
)
148+
else:
149+
raise ValueError(f"❌ Found a programming language that is not supported at the moment")
150+
107151
stdout, stderr = process.communicate(input=lambda_code.encode())
108152

109153
if process.returncode not in [0, 1]: # 0: No issues, 1: Linting errors
@@ -167,6 +211,7 @@ def linter(fileName, args=None):
167211
try:
168212
template = readFile(fileName)
169213
resources = template["Resources"]
214+
parameters = template["Parameters"]
170215
dict_to_check = findLambdaResources(resources)
171216
print(Fore.CYAN + f"📂 Found {len(dict_to_check)} resources to check." + Style.RESET_ALL)
172217
except Exception as e:
@@ -176,9 +221,9 @@ def linter(fileName, args=None):
176221
# Extract and lint Lambda code
177222
try:
178223
if args is None:
179-
error_dict = extractLambdaCode(resources, dict_to_check, args=None)
224+
error_dict = extractLambdaCode(resources, parameters, dict_to_check, args=None)
180225
else:
181-
error_dict = extractLambdaCode(resources, dict_to_check, args)
226+
error_dict = extractLambdaCode(resources, parameters,dict_to_check, args)
182227
except Exception as e:
183228
print(Fore.RED + f"❌ Error during Lambda code extraction: {e}" + Style.RESET_ALL)
184229
sys.exit(1)

0 commit comments

Comments
 (0)