-
Notifications
You must be signed in to change notification settings - Fork 14
fix windows build & run #27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
f443270
49e6547
ae27a91
fb2b1bf
dd4d55f
9c63675
7d77b9f
79d1a21
2cc681b
b0a5279
621718f
0a1b14e
250b40d
2cd243e
4fbb599
fcc20d5
2daf12a
a292b01
e492bd2
6faab65
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,37 @@ | ||||||||||||||||||||||
| package root | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| import ( | ||||||||||||||||||||||
| "fmt" | ||||||||||||||||||||||
| "os" | ||||||||||||||||||||||
| "os/exec" | ||||||||||||||||||||||
| ) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| func prepareDaemonCommand() (*exec.Cmd, error) { | ||||||||||||||||||||||
| // Get the current executable path | ||||||||||||||||||||||
| executable, err := os.Executable() | ||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||
| return nil, fmt.Errorf("failed to get executable path: %w", err) | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| // Get current working directory | ||||||||||||||||||||||
| workDir, err := os.Getwd() | ||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||
| return nil, fmt.Errorf("failed to get working directory: %w", err) | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| // Build command arguments, excluding the daemon flag | ||||||||||||||||||||||
| args := os.Args[1:] | ||||||||||||||||||||||
| var newArgs []string | ||||||||||||||||||||||
| for _, arg := range args { | ||||||||||||||||||||||
| if arg != "--daemon" { | ||||||||||||||||||||||
| newArgs = append(newArgs, arg) | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
Comment on lines
+25
to
+29
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The current logic for removing the
Suggested change
|
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| // Create the command | ||||||||||||||||||||||
| cmd := exec.Command(executable, newArgs...) | ||||||||||||||||||||||
| cmd.Dir = workDir | ||||||||||||||||||||||
| cmd.Env = os.Environ() | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| return cmd, nil | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| //go:build !windows | ||
|
|
||
| package root | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "syscall" | ||
| ) | ||
|
|
||
| // daemonize forks the process to run in the background | ||
| func daemonize() error { | ||
| cmd, err := prepareDaemonCommand() | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // Set up process attributes for daemon behavior | ||
| cmd.SysProcAttr = &syscall.SysProcAttr{ | ||
| Setsid: true, // Create new session | ||
| } | ||
|
|
||
| // Start the process | ||
| if err := cmd.Start(); err != nil { | ||
| return fmt.Errorf("failed to start daemon process: %w", err) | ||
| } | ||
|
|
||
| // Exit the parent process | ||
| os.Exit(0) | ||
|
|
||
| // This line should never be reached, but Go requires it | ||
| return nil | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| //go:build windows | ||
|
|
||
| package root | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "syscall" | ||
| ) | ||
|
|
||
| // daemonize forks the process to run in the background | ||
| func daemonize() error { | ||
| cmd, err := prepareDaemonCommand() | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // On Windows, use DETACHED_PROCESS flag to run in the background | ||
| // without being attached to the parent's console. | ||
| cmd.SysProcAttr = &syscall.SysProcAttr{CreationFlags: syscall.DETACHED_PROCESS} | ||
|
|
||
| // Start the process | ||
| if err := cmd.Start(); err != nil { | ||
| return fmt.Errorf("failed to start daemon process: %w", err) | ||
| } | ||
|
|
||
| // Exit the parent process | ||
| os.Exit(0) | ||
|
|
||
| // This line should never be reached, but Go requires it | ||
| return nil | ||
| } | ||
|
zx9597446 marked this conversation as resolved.
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| //go:build !windows | ||
|
|
||
| package command | ||
|
|
||
| import ( | ||
| "strings" | ||
|
|
||
| "github.com/inercia/MCPShell/pkg/common" | ||
| ) | ||
|
|
||
| // getShellCommandArgs returns the correct arguments for different shell types on Unix systems | ||
| func getShellCommandArgs(shell string, command string) (string, []string) { | ||
| shellLower := strings.ToLower(shell) | ||
|
|
||
| // Check if this is a PowerShell (might be available on Unix via PowerShell Core) | ||
| if strings.Contains(shellLower, "powershell") || | ||
| strings.HasSuffix(shellLower, "powershell.exe") || | ||
| strings.HasSuffix(shellLower, "pwsh.exe") { | ||
| return shell, []string{"-Command", command} | ||
| } | ||
|
|
||
| // For Unix-like systems and default fallback | ||
| return shell, []string{"-c", command} | ||
| } | ||
|
|
||
| // shouldUseUnixTimeoutCommand returns whether to use the Unix-style timeout command | ||
| func shouldUseUnixTimeoutCommand() bool { | ||
| return common.CheckExecutableExists("timeout") | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| //go:build windows | ||
|
|
||
| package command | ||
|
|
||
| import ( | ||
| "runtime" | ||
| "strings" | ||
| ) | ||
|
|
||
| // getShellCommandArgs returns the correct arguments for different shell types on Windows | ||
| func getShellCommandArgs(shell string, command string) (string, []string) { | ||
| shellLower := strings.ToLower(shell) | ||
|
|
||
| // Check if this is a cmd shell (Windows) | ||
| if strings.Contains(shellLower, "cmd") || | ||
| strings.HasSuffix(shellLower, "cmd.exe") || | ||
| (shell == "" && runtime.GOOS == "windows") { // Default to cmd on Windows if no shell specified | ||
| return shell, []string{"/c", command} | ||
| } | ||
|
|
||
| // Check if this is a PowerShell | ||
| if strings.Contains(shellLower, "powershell") || | ||
| strings.HasSuffix(shellLower, "powershell.exe") || | ||
| strings.HasSuffix(shellLower, "pwsh.exe") { | ||
| return shell, []string{"-Command", command} | ||
| } | ||
|
|
||
| // For WSL, we might have bash or other Unix shells | ||
| // For Unix-like systems and default fallback | ||
| return shell, []string{"-c", command} | ||
| } | ||
|
|
||
| // shouldUseUnixTimeoutCommand returns whether to use the Unix-style timeout command | ||
| func shouldUseUnixTimeoutCommand() bool { | ||
| // On Windows, we don't use Unix-style timeout command even if a 'timeout' command exists | ||
| // because Windows 'timeout' is for pausing, not for limiting execution time | ||
| return false | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To use
strings.HasPrefixas suggested in another comment for more robust flag parsing, you'll need to import thestringspackage.