Skip to content

Commit

Permalink
fix: disable detached on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
F1bonacc1 committed Aug 17, 2024
1 parent cb9a688 commit 771066f
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 33 deletions.
33 changes: 0 additions & 33 deletions src/cmd/project_runner.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package cmd

import (
"fmt"
"github.com/f1bonacc1/process-compose/src/app"
"github.com/f1bonacc1/process-compose/src/config"
"github.com/f1bonacc1/process-compose/src/loader"
"github.com/f1bonacc1/process-compose/src/tui"
"github.com/rs/zerolog/log"
"os"
"os/exec"
"os/signal"
"syscall"
)
Expand Down Expand Up @@ -139,34 +137,3 @@ func quiet() func() {
os.Stderr = serr
}
}

func runInDetachedMode() {
log.Info().Msg("Running in detached mode")
fmt.Println("Starting Process Compose in detached mode. Use 'process-compose attach' to connect to it or 'process-compose down' to stop it")
//remove detached flag
for i, arg := range os.Args {
if arg == "-D" || arg == "--detached" {
os.Args = append(os.Args[:i], os.Args[i+1:]...)
break
}
}
// Prepare to launch the background process
os.Args = append(os.Args, "-t=false")
cmd := exec.Command(os.Args[0], os.Args[1:]...)
cmd.SysProcAttr = &syscall.SysProcAttr{
Setsid: true, // Detach from terminal
}

// Redirect standard file descriptors to /dev/null
cmd.Stdin = nil
cmd.Stdout, _ = os.OpenFile("/dev/null", os.O_RDWR, 0)
cmd.Stderr, _ = os.OpenFile("/dev/null", os.O_RDWR, 0)

// Start the process in the background
if err := cmd.Start(); err != nil {
panic(err)
}

// Exit the parent process
os.Exit(0)
}
42 changes: 42 additions & 0 deletions src/cmd/project_runner_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//go:build !windows

package cmd

import (
"fmt"
"github.com/rs/zerolog/log"
"os"
"os/exec"
"syscall"
)

func runInDetachedMode() {
log.Info().Msg("Running in detached mode")
fmt.Println("Starting Process Compose in detached mode. Use 'process-compose attach' to connect to it or 'process-compose down' to stop it")
//remove detached flag
for i, arg := range os.Args {
if arg == "-D" || arg == "--detached" {
os.Args = append(os.Args[:i], os.Args[i+1:]...)
break
}
}
// Prepare to launch the background process
os.Args = append(os.Args, "-t=false")
cmd := exec.Command(os.Args[0], os.Args[1:]...)
cmd.SysProcAttr = &syscall.SysProcAttr{
Setsid: true, // Detach from terminal
}

// Redirect standard file descriptors to /dev/null
cmd.Stdin = nil
cmd.Stdout, _ = os.OpenFile("/dev/null", os.O_RDWR, 0)
cmd.Stderr, _ = os.OpenFile("/dev/null", os.O_RDWR, 0)

// Start the process in the background
if err := cmd.Start(); err != nil {
panic(err)
}

// Exit the parent process
os.Exit(0)
}
7 changes: 7 additions & 0 deletions src/cmd/project_runner_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package cmd

import "github.com/rs/zerolog/log"

func runInDetachedMode() {
log.Fatal().Msg("Running in detached mode is not supported on Windows")
}

0 comments on commit 771066f

Please sign in to comment.