Skip to content

Commit

Permalink
feat #169: Add --detach option
Browse files Browse the repository at this point in the history
  • Loading branch information
F1bonacc1 committed Aug 16, 2024
1 parent e3d9281 commit 3937434
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 7 deletions.
33 changes: 33 additions & 0 deletions src/cmd/project_runner.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
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 @@ -136,3 +138,34 @@ 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)
}
13 changes: 9 additions & 4 deletions src/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,20 @@ var (
pcFlags.PcThemeChanged = cmd.Flags().Changed(flagTheme)
pcFlags.SortColumnChanged = cmd.Flags().Changed(flagSort)
},
Run: run,
Run: func(cmd *cobra.Command, args []string) {
runProjectCmd([]string{})
},
}
)

func run(cmd *cobra.Command, args []string) {
func runProjectCmd(args []string) {
defer func() {
_ = logFile.Close()
}()
runner := getProjectRunner([]string{}, false, "", []string{})
if *pcFlags.IsDetached {
runInDetachedMode()
}
runner := getProjectRunner(args, *pcFlags.NoDependencies, "", []string{})
err := waitForProjectAndServer(!*pcFlags.IsTuiEnabled, runner)
handleErrorAndExit(err)
}
Expand Down Expand Up @@ -90,7 +95,7 @@ func init() {
_ = rootCmd.Flags().MarkDeprecated("keep-tui", "use --keep-project instead")

if runtime.GOOS != "windows" {
//rootCmd.Flags().BoolVarP(pcFlags.IsDetached, "detached", "D", *pcFlags.IsDetached, "run process-compose in detached mode")
rootCmd.Flags().BoolVarP(pcFlags.IsDetached, "detached", "D", *pcFlags.IsDetached, "run process-compose in detached mode")
rootCmd.PersistentFlags().StringVarP(pcFlags.UnixSocketPath, "unix-socket", "u", config.GetUnixSocketPath(), "path to unix socket (env: "+config.EnvVarUnixSocketPath+")")
rootCmd.PersistentFlags().BoolVarP(pcFlags.IsUnixSocket, "use-uds", "U", *pcFlags.IsUnixSocket, "use unix domain sockets instead of tcp")
}
Expand Down
8 changes: 5 additions & 3 deletions src/cmd/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"github.com/f1bonacc1/process-compose/src/admitter"
"github.com/spf13/cobra"
"runtime"
)

// upCmd represents the up command
Expand All @@ -13,9 +14,7 @@ var upCmd = &cobra.Command{
If one or more process names are passed as arguments,
will start them and their dependencies only`,
Run: func(cmd *cobra.Command, args []string) {
runner := getProjectRunner(args, *pcFlags.NoDependencies, "", []string{})
err := waitForProjectAndServer(!*pcFlags.IsTuiEnabled, runner)
handleErrorAndExit(err)
runProjectCmd(args)
},
}

Expand All @@ -38,6 +37,9 @@ func init() {
upCmd.Flags().AddFlag(commonFlags.Lookup(flagSort))
upCmd.Flags().AddFlag(commonFlags.Lookup(flagTheme))

if runtime.GOOS != "windows" {
upCmd.Flags().AddFlag(rootCmd.Flags().Lookup("detached"))
}
_ = upCmd.Flags().MarkDeprecated("keep-tui", "use --keep-project instead")

}
2 changes: 2 additions & 0 deletions src/config/Flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ type Flags struct {
OutputFormat *string
DisableDotEnv *bool
IsTuiFullScreen *bool
IsDetached *bool
}

// NewFlags returns new configuration flags.
Expand Down Expand Up @@ -99,6 +100,7 @@ func NewFlags() *Flags {
OutputFormat: toPtr(""),
DisableDotEnv: toPtr(getDisableDotEnvDefault()),
IsTuiFullScreen: toPtr(getTuiFullScreenDefault()),
IsDetached: toPtr(false),
}
}

Expand Down

0 comments on commit 3937434

Please sign in to comment.