|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Colors for better readability |
| 4 | +GREEN='\033[0;32m' |
| 5 | +BLUE='\033[0;34m' |
| 6 | +YELLOW='\033[1;33m' |
| 7 | +NC='\033[0m' # No Color |
| 8 | + |
| 9 | +# Default output directory |
| 10 | +DEFAULT_OUTPUT_DIR="../postman-export/workspaces" |
| 11 | + |
| 12 | +# Get the directory where the script is located |
| 13 | +SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" |
| 14 | +# Get the parent directory (project root) |
| 15 | +PROJECT_ROOT="$(dirname "$SCRIPT_DIR")" |
| 16 | + |
| 17 | +# Display a welcome message |
| 18 | +echo -e "${GREEN}=================================${NC}" |
| 19 | +echo -e "${GREEN}Postman Workspace Admins Report${NC}" |
| 20 | +echo -e "${GREEN}=================================${NC}" |
| 21 | + |
| 22 | +# Function to check if the API key is set |
| 23 | +check_api_key() { |
| 24 | + if [ -z "$POSTMAN_API_KEY" ]; then |
| 25 | + echo -e "${YELLOW}Warning: POSTMAN_API_KEY environment variable is not set.${NC}" |
| 26 | + echo -e "Would you like to set it now? (y/n)" |
| 27 | + read -r set_api_key |
| 28 | + |
| 29 | + if [[ "$set_api_key" == "y" || "$set_api_key" == "Y" ]]; then |
| 30 | + echo -e "Enter your Postman API key:" |
| 31 | + read -r api_key |
| 32 | + export POSTMAN_API_KEY="$api_key" |
| 33 | + echo -e "${GREEN}API key set for this session.${NC}" |
| 34 | + |
| 35 | + echo -e "Would you like to save this API key to your shell profile? (y/n)" |
| 36 | + read -r save_api_key |
| 37 | + |
| 38 | + if [[ "$save_api_key" == "y" || "$save_api_key" == "Y" ]]; then |
| 39 | + if [[ "$SHELL" == *"zsh"* ]]; then |
| 40 | + PROFILE_FILE="$HOME/.zshrc" |
| 41 | + elif [[ "$SHELL" == *"bash"* ]]; then |
| 42 | + if [[ "$OSTYPE" == "darwin"* ]]; then |
| 43 | + PROFILE_FILE="$HOME/.bash_profile" |
| 44 | + else |
| 45 | + PROFILE_FILE="$HOME/.bashrc" |
| 46 | + fi |
| 47 | + else |
| 48 | + echo -e "${YELLOW}Could not determine your shell profile. API key will not be saved.${NC}" |
| 49 | + return |
| 50 | + fi |
| 51 | + |
| 52 | + echo "" >> "$PROFILE_FILE" |
| 53 | + echo "# Postman API Key" >> "$PROFILE_FILE" |
| 54 | + echo "export POSTMAN_API_KEY=\"$api_key\"" >> "$PROFILE_FILE" |
| 55 | + echo -e "${GREEN}API key saved to $PROFILE_FILE${NC}" |
| 56 | + fi |
| 57 | + else |
| 58 | + echo -e "${YELLOW}You'll need to set the POSTMAN_API_KEY environment variable to use this script.${NC}" |
| 59 | + exit 1 |
| 60 | + fi |
| 61 | + fi |
| 62 | +} |
| 63 | + |
| 64 | +# Function to get output directory |
| 65 | +get_output_dir() { |
| 66 | + echo "" |
| 67 | + echo -e "${BLUE}Would you like to use a custom output directory? (y/n) ${NC}" |
| 68 | + echo -e "Default: ${DEFAULT_OUTPUT_DIR}" |
| 69 | + read -r use_custom_dir |
| 70 | + |
| 71 | + if [[ "$use_custom_dir" == "y" || "$use_custom_dir" == "Y" ]]; then |
| 72 | + echo -n "Enter output directory path: " |
| 73 | + read -r output_dir |
| 74 | + |
| 75 | + # Check if the path is absolute or relative |
| 76 | + if [[ "$output_dir" != /* ]]; then |
| 77 | + # Convert to absolute path |
| 78 | + output_dir="$PROJECT_ROOT/$output_dir" |
| 79 | + fi |
| 80 | + |
| 81 | + echo -e "Using output directory: ${YELLOW}$output_dir${NC}" |
| 82 | + else |
| 83 | + output_dir="$PROJECT_ROOT/postman-export/workspaces" |
| 84 | + echo -e "Using default output directory: ${YELLOW}$output_dir${NC}" |
| 85 | + fi |
| 86 | +} |
| 87 | + |
| 88 | +# Check for dependencies |
| 89 | +check_dependencies() { |
| 90 | + if ! command -v node &> /dev/null; then |
| 91 | + echo -e "${YELLOW}Node.js is not installed. Please install Node.js before continuing.${NC}" |
| 92 | + exit 1 |
| 93 | + fi |
| 94 | + |
| 95 | + # Switch to project root to check for package.json |
| 96 | + cd "$PROJECT_ROOT" |
| 97 | + |
| 98 | + if [ ! -f package.json ]; then |
| 99 | + echo -e "${YELLOW}Warning: package.json not found. Are you in the correct directory?${NC}" |
| 100 | + fi |
| 101 | + |
| 102 | + # Check if dependencies are installed |
| 103 | + if [ ! -d "node_modules" ]; then |
| 104 | + echo -e "${YELLOW}Node modules not found. Installing dependencies...${NC}" |
| 105 | + npm install |
| 106 | + fi |
| 107 | + |
| 108 | + # Check specifically for Handlebars |
| 109 | + if ! grep -q "handlebars" package.json; then |
| 110 | + echo -e "${YELLOW}Handlebars dependency not found in package.json. Installing...${NC}" |
| 111 | + npm install handlebars |
| 112 | + fi |
| 113 | +} |
| 114 | + |
| 115 | +# Function to open the HTML report in the default browser |
| 116 | +open_report() { |
| 117 | + echo "" |
| 118 | + echo -e "${BLUE}Would you like to open the HTML report in your browser? (y/n) ${NC}" |
| 119 | + read -r open_browser |
| 120 | + |
| 121 | + if [[ "$open_browser" == "y" || "$open_browser" == "Y" ]]; then |
| 122 | + report_path="$output_dir/admin-report.html" |
| 123 | + |
| 124 | + if [[ "$OSTYPE" == "darwin"* ]]; then |
| 125 | + # macOS |
| 126 | + open "$report_path" |
| 127 | + elif [[ "$OSTYPE" == "linux-gnu"* ]]; then |
| 128 | + # Linux |
| 129 | + xdg-open "$report_path" |
| 130 | + elif [[ "$OSTYPE" == "cygwin" || "$OSTYPE" == "msys" || "$OSTYPE" == "win32" ]]; then |
| 131 | + # Windows |
| 132 | + start "$report_path" |
| 133 | + else |
| 134 | + echo -e "${YELLOW}Couldn't determine how to open the report on your system.${NC}" |
| 135 | + echo -e "Report is located at: ${report_path}" |
| 136 | + fi |
| 137 | + fi |
| 138 | +} |
| 139 | + |
| 140 | +# Main script execution |
| 141 | +echo -e "${BLUE}This script will generate a report of all team workspaces and their admin users.${NC}" |
| 142 | +check_dependencies |
| 143 | +check_api_key |
| 144 | +get_output_dir |
| 145 | + |
| 146 | +# Run the workspace admin report |
| 147 | +echo -e "${GREEN}Fetching workspace data and generating report...${NC}" |
| 148 | +cd "$PROJECT_ROOT" && node get-workspace-admins/get-team-workspaces.js "$output_dir" |
| 149 | + |
| 150 | +# Check if the report was generated successfully |
| 151 | +if [ $? -eq 0 ]; then |
| 152 | + echo -e "${GREEN}Report generated successfully!${NC}" |
| 153 | + echo -e "JSON data saved to: ${YELLOW}$output_dir/workspace-admins-report.json${NC}" |
| 154 | + echo -e "HTML report saved to: ${YELLOW}$output_dir/admin-report.html${NC}" |
| 155 | + |
| 156 | + # Ask if the user wants to open the report |
| 157 | + open_report |
| 158 | +else |
| 159 | + echo -e "${YELLOW}Error generating the report. Please check the console output for details.${NC}" |
| 160 | +fi |
| 161 | + |
| 162 | +exit 0 |
0 commit comments