🔍 Code Extractor

function main_v19

Maturity: 44

A validation function that checks SharePoint configuration settings from environment variables and provides diagnostic feedback on their validity.

File:
/tf/active/vicechatdev/SPFCsync/validate_config.py
Lines:
79 - 128
Complexity:
simple

Purpose

This function serves as a configuration validator for SharePoint integration. It loads environment variables from a .env file, validates the SharePoint URL, Azure Client ID, and Azure Client Secret, then provides comprehensive feedback on configuration status with actionable next steps. It's designed to be run as a standalone diagnostic tool before attempting SharePoint connections.

Source Code

def main():
    """Main validation function."""
    print("SharePoint Configuration Validator")
    print("=" * 40)
    
    # Load environment variables
    env_vars = load_env_file()
    if env_vars is None:
        return 1
    
    print("✅ .env file loaded successfully")
    print()
    
    # Validate SharePoint URL
    sharepoint_url = env_vars.get('SHAREPOINT_SITE_URL', '')
    url_valid, url_message = validate_sharepoint_url(sharepoint_url)
    print(f"SharePoint URL: {'✅' if url_valid else '❌'} {url_message}")
    if sharepoint_url and sharepoint_url != "https://your-tenant.sharepoint.com/sites/your-site":
        print(f"  Current value: {sharepoint_url}")
    
    # Validate Azure Client ID
    client_id = env_vars.get('AZURE_CLIENT_ID', '')
    id_valid, id_message = validate_azure_client_id(client_id)
    print(f"Azure Client ID: {'✅' if id_valid else '❌'} {id_message}")
    
    # Validate Azure Client Secret
    client_secret = env_vars.get('AZURE_CLIENT_SECRET', '')
    secret_valid, secret_message = validate_azure_client_secret(client_secret)
    print(f"Azure Client Secret: {'✅' if secret_valid else '❌'} {secret_message}")
    
    print()
    
    # Overall status
    all_valid = url_valid and id_valid and secret_valid
    
    if all_valid:
        print("🎉 All SharePoint configuration looks good!")
        print()
        print("Next steps:")
        print("1. Run connection test: python test_connections.py")
        print("2. If test passes, try one-time sync: python main.py --once")
        return 0
    else:
        print("❌ Configuration issues found. Please fix the above issues.")
        print()
        print("Setup help:")
        print("1. See SHAREPOINT_SETUP.md for detailed instructions")
        print("2. Update your .env file with correct values")
        print("3. Run this validator again")
        return 1

Return Value

Returns an integer exit code: 0 if all validations pass (SharePoint URL, Azure Client ID, and Azure Client Secret are all valid), or 1 if any validation fails or if the .env file cannot be loaded. This follows standard Unix exit code conventions where 0 indicates success.

Required Imports

import os
import sys
import re
from urllib.parse import urlparse

Usage Example

# Assuming all required validation functions are defined in the same module
# and a .env file exists with SharePoint configuration

if __name__ == '__main__':
    exit_code = main()
    sys.exit(exit_code)

# Or simply call directly:
# main()

# Expected .env file format:
# SHAREPOINT_SITE_URL=https://your-tenant.sharepoint.com/sites/your-site
# AZURE_CLIENT_ID=your-client-id-guid
# AZURE_CLIENT_SECRET=your-client-secret

Best Practices

  • Run this function before attempting any SharePoint connections to ensure configuration is valid
  • Use the return code to determine if setup is complete (0 = success, 1 = failure)
  • Follow the printed next steps after successful validation
  • Ensure all helper functions (load_env_file, validate_sharepoint_url, validate_azure_client_id, validate_azure_client_secret) are properly implemented
  • The function expects validation functions to return tuples of (bool, str) where bool indicates validity and str contains the message
  • This function is designed for interactive use with console output and should be run from command line
  • Keep the .env file secure as it contains sensitive Azure credentials

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function main_v22 76.0% similar

    Orchestrates a comprehensive SharePoint connection diagnostic tool that validates Azure AD authentication and SharePoint access by running multiple tests and reporting results.

    From: /tf/active/vicechatdev/SPFCsync/diagnose_sharepoint.py
  • function main_v25 74.3% similar

    Entry point function that tests SharePoint REST API connectivity by loading configuration, validating settings, and executing connection tests.

    From: /tf/active/vicechatdev/SPFCsync/test_rest_client.py
  • function main_v13 74.2% similar

    Main test function that validates SharePoint folder structure connectivity and configuration, comparing actual folders against expected structure.

    From: /tf/active/vicechatdev/SPFCsync/test_folder_structure.py
  • function test_sharepoint_connection 70.2% similar

    Tests the connection to a SharePoint site by attempting to instantiate a SharePointClient with Azure credentials and configuration settings.

    From: /tf/active/vicechatdev/SPFCsync/test_connections.py
  • function main_v26 69.8% similar

    Diagnostic function that tests SharePoint tenant configuration by checking Microsoft Graph API access and provides recommendations based on the results.

    From: /tf/active/vicechatdev/SPFCsync/check_tenant_config.py
← Back to Browse