From 561b8fb637511399c9cd5c2885e66ffd09c906cc Mon Sep 17 00:00:00 2001 From: Chiran Fernando Date: Sun, 18 May 2025 16:11:04 +0530 Subject: [PATCH] Add eof --- internal/subprocess/manager.go | 76 +++++++++++++------------- internal/subprocess/manager_unix.go | 2 +- internal/subprocess/manager_windows.go | 2 +- 3 files changed, 40 insertions(+), 40 deletions(-) diff --git a/internal/subprocess/manager.go b/internal/subprocess/manager.go index 6230fe0..902a517 100644 --- a/internal/subprocess/manager.go +++ b/internal/subprocess/manager.go @@ -4,14 +4,14 @@ import ( "fmt" "os" "os/exec" + "runtime" + "strings" "sync" "syscall" "time" - "strings" - "runtime" "github.com/wso2/open-mcp-auth-proxy/internal/config" - "github.com/wso2/open-mcp-auth-proxy/internal/logging" + logger "github.com/wso2/open-mcp-auth-proxy/internal/logging" ) // Manager handles starting and graceful shutdown of subprocesses @@ -32,39 +32,39 @@ func NewManager() *Manager { // EnsureDependenciesAvailable checks and installs required package executors func EnsureDependenciesAvailable(command string) error { - // Always ensure npx is available regardless of the command - if _, err := exec.LookPath("npx"); err != nil { - // npx is not available, check if npm is installed - if _, err := exec.LookPath("npm"); err != nil { - return fmt.Errorf("npx not found and npm not available; please install Node.js from https://nodejs.org/") - } - - // Try to install npx using npm - logger.Info("npx not found, attempting to install...") - var cmd *exec.Cmd - if runtime.GOOS == "windows" { - cmd = exec.Command("npm.cmd", "install", "-g", "npx") - } else { - cmd = exec.Command("npm", "install", "-g", "npx") - } - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr - - if err := cmd.Run(); err != nil { - return fmt.Errorf("failed to install npx: %w", err) - } - - logger.Info("npx installed successfully") - } - - // Check if uv is needed based on the command - if strings.Contains(command, "uv ") { - if _, err := exec.LookPath("uv"); err != nil { - return fmt.Errorf("command requires uv but it's not installed; please install it following instructions at https://github.com/astral-sh/uv") - } - } - - return nil + // Always ensure npx is available regardless of the command + if _, err := exec.LookPath("npx"); err != nil { + // npx is not available, check if npm is installed + if _, err := exec.LookPath("npm"); err != nil { + return fmt.Errorf("npx not found and npm not available; please install Node.js from https://nodejs.org/") + } + + // Try to install npx using npm + logger.Info("npx not found, attempting to install...") + var cmd *exec.Cmd + if runtime.GOOS == "windows" { + cmd = exec.Command("npm.cmd", "install", "-g", "npx") + } else { + cmd = exec.Command("npm", "install", "-g", "npx") + } + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + + if err := cmd.Run(); err != nil { + return fmt.Errorf("failed to install npx: %w", err) + } + + logger.Info("npx installed successfully") + } + + // Check if uv is needed based on the command + if strings.Contains(command, "uv ") { + if _, err := exec.LookPath("uv"); err != nil { + return fmt.Errorf("command requires uv but it's not installed; please install it following instructions at https://github.com/astral-sh/uv") + } + } + + return nil } // SetShutdownDelay sets the maximum time to wait for graceful shutdown @@ -168,7 +168,7 @@ func (m *Manager) IsRunning() bool { // Shutdown gracefully terminates the subprocess func (m *Manager) Shutdown() { m.mutex.Lock() - processToTerminate := m.process // Local copy of the process reference + processToTerminate := m.process // Local copy of the process reference processGroupToTerminate := m.processGroup m.mutex.Unlock() @@ -315,4 +315,4 @@ func (m *Manager) Shutdown() { case <-time.After(m.shutdownDelay): logger.Warn("Subprocess termination timed out") } -} \ No newline at end of file +} diff --git a/internal/subprocess/manager_unix.go b/internal/subprocess/manager_unix.go index 2f3dc35..03ae1a8 100644 --- a/internal/subprocess/manager_unix.go +++ b/internal/subprocess/manager_unix.go @@ -20,4 +20,4 @@ func getProcessGroup(pid int) (int, error) { // killProcessGroup kills a process group on Unix systems func killProcessGroup(pgid int, signal syscall.Signal) error { return syscall.Kill(-pgid, signal) -} \ No newline at end of file +} diff --git a/internal/subprocess/manager_windows.go b/internal/subprocess/manager_windows.go index 30cb2c8..a039897 100644 --- a/internal/subprocess/manager_windows.go +++ b/internal/subprocess/manager_windows.go @@ -24,4 +24,4 @@ func killProcessGroup(pgid int, signal syscall.Signal) error { // On Windows, we'll use the process handle directly // This function shouldn't be called on Windows, but we provide it for compatibility return nil -} \ No newline at end of file +}