-
-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for foreground processes
- Loading branch information
Showing
11 changed files
with
268 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package app | ||
|
||
import "github.com/f1bonacc1/process-compose/src/types" | ||
|
||
type ProjectOpts struct { | ||
project *types.Project | ||
processesToRun []string | ||
noDeps bool | ||
mainProcess string | ||
mainProcessArgs []string | ||
isTuiOn bool | ||
} | ||
|
||
func (p *ProjectOpts) WithProject(project *types.Project) *ProjectOpts { | ||
p.project = project | ||
return p | ||
} | ||
|
||
func (p *ProjectOpts) WithProcessesToRun(processesToRun []string) *ProjectOpts { | ||
p.processesToRun = processesToRun | ||
return p | ||
} | ||
func (p *ProjectOpts) WithNoDeps(noDeps bool) *ProjectOpts { | ||
p.noDeps = noDeps | ||
return p | ||
} | ||
|
||
func (p *ProjectOpts) WithMainProcess(mainProcess string) *ProjectOpts { | ||
p.mainProcess = mainProcess | ||
return p | ||
} | ||
|
||
func (p *ProjectOpts) WithMainProcessArgs(mainProcessArgs []string) *ProjectOpts { | ||
p.mainProcessArgs = mainProcessArgs | ||
return p | ||
} | ||
|
||
func (p *ProjectOpts) WithIsTuiOn(isTuiOn bool) *ProjectOpts { | ||
p.isTuiOn = isTuiOn | ||
return p | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package tui | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/f1bonacc1/process-compose/src/types" | ||
"github.com/rs/zerolog/log" | ||
"os" | ||
"os/exec" | ||
"os/signal" | ||
"syscall" | ||
) | ||
|
||
func (pv *pcView) startProcess() { | ||
name := pv.getSelectedProcName() | ||
info, err := pv.project.GetProcessInfo(name) | ||
if err != nil { | ||
pv.showError(err.Error()) | ||
return | ||
} | ||
if info.IsForeground { | ||
pv.runForeground(info) | ||
return | ||
} | ||
err = pv.project.StartProcess(name) | ||
if err != nil { | ||
pv.showError(err.Error()) | ||
} | ||
} | ||
|
||
func (pv *pcView) runForeground(info *types.ProcessConfig) bool { | ||
pv.halt() | ||
defer pv.resume() | ||
return pv.appView.Suspend(func() { | ||
err := pv.execute(info) | ||
if err != nil { | ||
log.Err(err).Msgf("Command failed") | ||
} | ||
}) | ||
} | ||
|
||
func (pv *pcView) execute(info *types.ProcessConfig) error { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer func() { | ||
cancel() | ||
clearScreen() | ||
}() | ||
sigChan := make(chan os.Signal, 1) | ||
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM) | ||
go func(cancel context.CancelFunc) { | ||
select { | ||
case sig := <-sigChan: | ||
log.Debug().Msgf("Command canceled with signal %#v", sig) | ||
cancel() | ||
case <-ctx.Done(): | ||
log.Debug().Msgf("Foreground process context canceled") | ||
} | ||
}(cancel) | ||
|
||
cmd := exec.CommandContext(ctx, info.Executable, info.Args...) | ||
log.Debug().Str("exec", info.Executable).Strs("args", info.Args).Msg("running start") | ||
cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr | ||
err := cmd.Run() | ||
log.Debug().Str("exec", info.Executable).Strs("args", info.Args).Msg("running end") | ||
|
||
return err | ||
} | ||
|
||
func clearScreen() { | ||
fmt.Print("\033[H\033[2J") | ||
} |
Oops, something went wrong.