浏览代码

remove hardcoded paths

Claude 9 月之前
父节点
当前提交
8b18d6b400
共有 2 个文件被更改,包括 46 次插入2 次删除
  1. 10 0
      .env.example
  2. 36 2
      src/config.js

+ 10 - 0
.env.example

@@ -16,6 +16,16 @@ WEBHOOK_ISSUES_ENABLED=false
 WEBHOOK_ISSUE_COMMENT_ENABLED=false
 WEBHOOK_GLOBAL_ENABLED=false
 
+# Path Configuration
+# Base path for agent-manager installation (defaults to current working directory)
+AGENT_MANAGER_PATH=/home/claude/agent-manager
+
+# Workspace path for command execution (used in commands.json)
+WORKSPACE_PATH=/workspace
+
+# Scripts path (defaults to ${AGENT_MANAGER_PATH}/scripts if not set)
+SCRIPTS_PATH=/home/claude/agent-manager/scripts
+
 # MCP Server Configuration
 # Path to gogs-mcp server (used in headless Claude Code mode)
 GOGS_MCP_PATH=/data/gogs-mcp/dist/index.js

+ 36 - 2
src/config.js

@@ -110,6 +110,30 @@ export class Config {
     };
   }
 
+  /**
+   * Substitute environment variables in a string
+   * Replaces ${VAR_NAME} or $VAR_NAME with values from environment
+   */
+  substituteEnvVars(str) {
+    if (!str || typeof str !== 'string') {
+      return str;
+    }
+
+    // Replace ${VAR_NAME} pattern
+    str = str.replace(/\$\{([A-Z_][A-Z0-9_]*)\}/g, (match, varName) => {
+      return this.get(varName, match);
+    });
+
+    // Replace $VAR_NAME pattern (only at start or after /)
+    str = str.replace(/(^|\/)(\$[A-Z_][A-Z0-9_]*)/g, (match, prefix, varName) => {
+      const cleanVarName = varName.substring(1); // Remove $
+      const value = this.get(cleanVarName);
+      return value !== undefined ? prefix + value : match;
+    });
+
+    return str;
+  }
+
   /**
    * Get command configurations for a specific event type
    * Returns array of command configs from commands.json, or legacy config from .env
@@ -117,7 +141,12 @@ export class Config {
   getCommandConfig(eventType) {
     // Check if we have JSON commands for this event type
     if (this.commands.commands && this.commands.commands[eventType]) {
-      return this.commands.commands[eventType];
+      // Substitute environment variables in command paths
+      return this.commands.commands[eventType].map(cmd => ({
+        ...cmd,
+        command: this.substituteEnvVars(cmd.command),
+        cwd: this.substituteEnvVars(cmd.cwd)
+      }));
     }
 
     // Fallback to .env configuration (for backward compatibility)
@@ -150,7 +179,12 @@ export class Config {
   getGlobalCommandConfig() {
     // Check if we have JSON commands for global
     if (this.commands.commands && this.commands.commands.global) {
-      return this.commands.commands.global;
+      // Substitute environment variables in command paths
+      return this.commands.commands.global.map(cmd => ({
+        ...cmd,
+        command: this.substituteEnvVars(cmd.command),
+        cwd: this.substituteEnvVars(cmd.cwd)
+      }));
     }
 
     // Fallback to .env configuration (for backward compatibility)