Skip to content

Commit 9cbfa4c

Browse files
Enhancement | Catch and handle HCL parsing errors (#296)
* catch hcl parsing errors * unnecessary * Update leverage/modules/auth.py Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * black * fix test --------- Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
1 parent 11774f0 commit 9cbfa4c

File tree

14 files changed

+39
-13
lines changed

14 files changed

+39
-13
lines changed

leverage/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""
22
Binbash Leverage Command-line tool.
33
"""
4+
45
# pylint: disable=wrong-import-position
56

67
__version__ = "0.0.0"

leverage/_internals.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""
22
Definitions for internal use of the cli.
33
"""
4+
45
from functools import wraps
56

67
import click

leverage/_utils.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
"""
22
General use utilities.
33
"""
4+
5+
from pathlib import Path
46
from subprocess import run
57
from subprocess import PIPE
68

9+
import hcl2
10+
import lark
711
from click.exceptions import Exit
812
from configupdater import ConfigUpdater
913
from docker import DockerClient
@@ -112,6 +116,20 @@ def __init__(self, exit_code: int, error_description: str):
112116
super(ExitError, self).__init__(exit_code)
113117

114118

119+
def parse_tf_file(file: Path):
120+
"""
121+
Open and parse an HCL file.
122+
In case of a parsing error, raise a user-friendly error.
123+
"""
124+
with open(file) as f:
125+
try:
126+
parsed = hcl2.load(f)
127+
except lark.exceptions.UnexpectedInput:
128+
raise ExitError(1, f"There is a parsing error with the {f.name} file. Please review it.")
129+
else:
130+
return parsed
131+
132+
115133
class ContainerSession:
116134
"""
117135
Handle the start/stop cycle of a container.

leverage/conf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""
22
Env variables loading utility.
33
"""
4+
45
from pathlib import Path
56

67
from yaenv.core import Env

leverage/leverage.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""
22
Binbash Leverage Command-line tool.
33
"""
4+
45
import click
56

67
from leverage import __version__

leverage/logger.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""
22
Logging utilities.
33
"""
4+
45
import logging
56
from functools import wraps
67

leverage/modules/auth.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22
from pathlib import Path
33
from configparser import NoSectionError, NoOptionError
44

5-
import hcl2
65
import boto3
7-
from configupdater import ConfigUpdater
86
from botocore.exceptions import ClientError
7+
from configupdater import ConfigUpdater
98

109
from leverage import logger
11-
from leverage._utils import key_finder, ExitError, get_or_create_section
10+
from leverage._utils import key_finder, ExitError, get_or_create_section, parse_tf_file
1211

1312

1413
class SkipProfile(Exception):
@@ -66,8 +65,7 @@ def get_profiles(cli):
6665
# these are files from the layer we are currently on
6766
for name in ("config.tf", "locals.tf"):
6867
try:
69-
with open(name) as tf_file:
70-
tf_config = hcl2.load(tf_file)
68+
tf_config = parse_tf_file(Path(name))
7169
except FileNotFoundError:
7270
continue
7371

@@ -76,8 +74,7 @@ def get_profiles(cli):
7674
raw_profiles.update(set(key_finder(tf_config, "profile", "lookup")))
7775

7876
# the profile value from <layer>/config/backend.tfvars
79-
with open(cli.paths.local_backend_tfvars) as backend_config_file:
80-
backend_config = hcl2.load(backend_config_file)
77+
backend_config = parse_tf_file(cli.paths.local_backend_tfvars)
8178
tf_profile = backend_config["profile"]
8279

8380
return tf_profile, raw_profiles

leverage/modules/credentials.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""
22
Credentials managing module.
33
"""
4+
45
import csv
56
import json
67
import re

leverage/modules/project.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""
22
Module for managing Leverage projects.
33
"""
4+
45
import re
56
from pathlib import Path
67
from shutil import copy2

leverage/modules/run.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""
22
Tasks running module.
33
"""
4+
45
import re
56

67
import click

0 commit comments

Comments
 (0)