diff --git a/enc.py b/enc.py index efd616c..b6a9c50 100644 --- a/enc.py +++ b/enc.py @@ -1,25 +1,32 @@ -#!/usr/bin/env python3 - -import yaml -import os +#!/usr/bin/env python import sys +import yaml -# Path to your ENC data directory -enc_data_dir = '/opt/puppetlabs/enc/data' +def main(): + # Get the hostname from the command line arguments + if len(sys.argv) != 2: + print('Usage: {} '.format(sys.argv[0]), file=sys.stderr) + sys.exit(1) + hostname = sys.argv[1] -# Get the hostname from command line argument -hostname = sys.argv[1] + # Construct the file path + file_path = '/opt/puppetlabs/enc/data/{}.yaml'.format(hostname) -# Construct the file path -file_path = os.path.join(enc_data_dir, f'{hostname}.yaml') + # Try to open and read the YAML file + try: + with open(file_path, 'r') as f: + file_content = f.read() + try: + # Parse the YAML file + yaml.safe_load(file_content) + # If correctly parsed, print the raw content + print(file_content) + except yaml.YAMLError as e: + print('Error in {}: {}'.format(file_path, e), file=sys.stderr) + sys.exit(1) + except IOError as e: + print('Could not open {}: {}'.format(file_path, e), file=sys.stderr) + sys.exit(1) -# Check if the file exists -if os.path.exists(file_path): - # If the file exists, open it and load the YAML data - with open(file_path, 'r') as file: - data = yaml.safe_load(file) - print(yaml.dump(data)) -else: - # If the file does not exist, print an error message and exit - sys.stderr.write(f"Error: File {file_path} does not exist.\n") - sys.exit(1) +if __name__ == '__main__': + main()