Skip to content
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

profiles: Add --detach/-d flag #57

Merged
merged 1 commit into from
Feb 24, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 21 additions & 11 deletions cmd/cli/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/urfave/cli/v3"
)

var selfcall bool
var detach bool

func startCommand() *cli.Command {
cmd := &cli.Command{
Expand Down Expand Up @@ -43,16 +43,19 @@ qubesome start -git https://github.com/qubesome/sample-dotfiles i3
&cli.StringFlag{
Name: "runner",
Destination: &runner,
Usage: "sets what runner to be used, this will override the value set at the qubesome.config. Options: docker or podman.",
},
&cli.BoolFlag{
Name: "interactive",
Aliases: []string{"i"},
Destination: &interactive,
Usage: "enables interactive mode, which runs the profile container but holds any windows manager execution. Use this for troubleshooting.",
},
&cli.BoolFlag{
Sources: cli.EnvVars("QUBESOME_SELFCALL"),
Destination: &selfcall,
Hidden: true,
Name: "detach",
Aliases: []string{"d"},
Destination: &detach,
Usage: "start the profile process in the background. This cannot be used in conjunction with --interactive nor --debug.",
},
},
Arguments: []cli.Argument{
Expand All @@ -66,11 +69,18 @@ qubesome start -git https://github.com/qubesome/sample-dotfiles i3
Action: func(ctx context.Context, cmd *cli.Command) error {
// When a profile is started, it starts an inception server
// so that the containerised Windows Manager is able to execute
// new container workloads. This self-calls qubesome and leave it
// running so that the main process exits right away.
if !debug && !interactive && !selfcall {
cmd := exec.Command(os.Args[0], os.Args[1:]...) //nolint
cmd.Env = append(cmd.Env, "QUBESOME_SELFCALL=true")
// new container workloads.
// Running on detached mode makes a background call to qubesome,
// leaving it running so that the main process can exit right away.
if !debug && !interactive && detach {
var args []string
for _, arg := range os.Args[1:] {
if arg == "-d" || arg == "-detach" {
continue
}
args = append(args, arg)
}
cmd := exec.Command(os.Args[0], args...) //nolint
cmd.Env = append(cmd.Env, os.Environ()...)
cmd.Stdout = nil
cmd.Stderr = nil
Expand All @@ -80,10 +90,10 @@ qubesome start -git https://github.com/qubesome/sample-dotfiles i3
}

if err := cmd.Start(); err != nil {
return fmt.Errorf("failed to daemonise profile start: %w", err)
return fmt.Errorf("failed to run profile start in detach mode: %w", err)
}

fmt.Printf("[%d] profile start as daemon\n", cmd.Process.Pid)
fmt.Printf("[%d] %q profile start detached\n", cmd.Process.Pid, targetProfile)
os.Exit(0)
}

Expand Down