Skip to content

Commit

Permalink
Bash default backend with configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
F1bonacc1 committed Dec 16, 2022
1 parent b80066b commit df0022b
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 16 deletions.
20 changes: 18 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -569,11 +569,11 @@ The following discovery order is used: `compose.yml, compose.yaml, process-compo

##### ✅ Linux

The default backend is `bash`. You can define a different backend with a `SHELL` environment variable.
The default backend is `bash`. You can define a different backend with a `COMPOSE_SHELL` environment variable.

##### ✅ Windows

The default backend is `cmd`. You can define a different backend with a `SHELL` environment variable.
The default backend is `cmd`. You can define a different backend with a `COMPOSE_SHELL` environment variable.

```yaml
process1:
Expand All @@ -590,7 +590,23 @@ process2:

##### ✅ macOS

The default backend is `bash`. You can define a different backend with a `COMPOSE_SHELL` environment variable.

##### ✅ Configurable Backend

For cases where you process compose requires a non default or transferable backend definition, setting an environment variable won't do. For that you can configure it directly in the `process-compose.yaml` file:

```yaml
version: "0.5"
shell:
shell_command: "python3"
shell_argument: "-m"
processes:
http:
command: "server.py"
```

**Note**: please make sure that the `shell.shell_command` value is in your `$PATH`

## How to Contribute

Expand Down
3 changes: 3 additions & 0 deletions process-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,6 @@ processes:
environment:
- 'ABC=222'
log_location: ./pc.log
shell:
shell_command: "zsh"
shell_argument: "-c"
14 changes: 8 additions & 6 deletions src/app/config.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
package app

import (
"github.com/f1bonacc1/process-compose/src/command"
"sync"

"github.com/f1bonacc1/process-compose/src/health"
"github.com/f1bonacc1/process-compose/src/pclog"
)

type Project struct {
Version string `yaml:"version"`
LogLocation string `yaml:"log_location,omitempty"`
LogLevel string `yaml:"log_level,omitempty"`
LogLength int `yaml:"log_length,omitempty"`
Processes Processes `yaml:"processes"`
Environment []string `yaml:"environment,omitempty"`
Version string `yaml:"version"`
LogLocation string `yaml:"log_location,omitempty"`
LogLevel string `yaml:"log_level,omitempty"`
LogLength int `yaml:"log_length,omitempty"`
Processes Processes `yaml:"processes"`
Environment []string `yaml:"environment,omitempty"`
ShellConfig *command.ShellConfig `yaml:"shell,omitempty"`

runningProcesses map[string]*Process
processStates map[string]*ProcessState
Expand Down
9 changes: 6 additions & 3 deletions src/app/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type Process struct {
startTime time.Time
liveProber *health.Prober
readyProber *health.Prober
shellConfig command.ShellConfig
}

func NewProcess(
Expand All @@ -57,7 +58,8 @@ func NewProcess(
procConf ProcessConfig,
procState *ProcessState,
procLog *pclog.ProcessLogBuffer,
replica int) *Process {
replica int,
shellConfig command.ShellConfig) *Process {
colNumeric := rand.Intn(int(color.FgHiWhite)-int(color.FgHiBlack)) + int(color.FgHiBlack)

proc := &Process{
Expand All @@ -71,6 +73,7 @@ func NewProcess(
done: false,
replica: replica,
logBuffer: procLog,
shellConfig: shellConfig,
procStateChan: make(chan string, 1),
procReadyChan: make(chan string, 1),
}
Expand Down Expand Up @@ -138,7 +141,7 @@ func (p *Process) run() int {

func (p *Process) getProcessStarter() func() error {
return func() error {
p.command = command.BuildCommand(p.getCommand())
p.command = command.BuildCommandShellArg(p.shellConfig, p.getCommand())
p.command.Env = p.getProcessEnvironment()
p.command.Dir = p.procConf.WorkingDir
p.setProcArgs()
Expand Down Expand Up @@ -254,7 +257,7 @@ func (p *Process) doConfiguredStop(params ShutDownParams) error {
defer cancel()
defer p.notifyDaemonStopped()

cmd := command.BuildCommandContext(ctx, params.ShutDownCommand)
cmd := command.BuildCommandShellArgContext(ctx, p.shellConfig, params.ShutDownCommand)
cmd.Env = p.getProcessEnvironment()

if err := cmd.Run(); err != nil {
Expand Down
13 changes: 12 additions & 1 deletion src/app/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package app
import (
"errors"
"fmt"
"github.com/f1bonacc1/process-compose/src/command"
"github.com/f1bonacc1/process-compose/src/pclog"
"os"
"path/filepath"
Expand All @@ -23,6 +24,7 @@ const (
var PROJ *Project

func (p *Project) init() {
p.setConfigDefaults()
p.initProcessStates()
p.initProcessLogs()
p.deprecationCheck()
Expand Down Expand Up @@ -65,7 +67,7 @@ func (p *Project) runProcess(proc ProcessConfig) {
log.Error().Msgf("Error: Can't get log: %s using empty buffer", err.Error())
procLog = pclog.NewLogBuffer(0)
}
process := NewProcess(p.Environment, procLogger, proc, p.GetProcessState(proc.Name), procLog, 1)
process := NewProcess(p.Environment, procLogger, proc, p.GetProcessState(proc.Name), procLog, 1, *p.ShellConfig)
p.addRunningProcess(process)
p.wg.Add(1)
go func() {
Expand Down Expand Up @@ -149,6 +151,15 @@ func (p *Project) deprecationCheck() {
}
}

func (p *Project) setConfigDefaults() {
if p.ShellConfig == nil {
p.ShellConfig = command.DefaultShellConfig()
}
log.Info().Msgf("Global shell command: %s %s", p.ShellConfig.ShellCommand, p.ShellConfig.ShellArgument)
command.ValidateShellConfig(*p.ShellConfig)

}

func (p *Project) GetProcessState(name string) *ProcessState {
if procState, ok := p.processStates[name]; ok {
proc := p.getRunningProcess(name)
Expand Down
31 changes: 27 additions & 4 deletions src/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package command

import (
"context"
"github.com/rs/zerolog/log"
"os"
"os/exec"
"runtime"
Expand All @@ -11,12 +12,20 @@ func BuildCommand(shellCmd string) *exec.Cmd {
return exec.Command(getRunnerShell(), getRunnerArg(), shellCmd)
}

func BuildCommandShellArg(shell ShellConfig, cmd string) *exec.Cmd {
return exec.Command(shell.ShellCommand, shell.ShellArgument, cmd)
}

func BuildCommandContext(ctx context.Context, shellCmd string) *exec.Cmd {
return exec.CommandContext(ctx, getRunnerShell(), getRunnerArg(), shellCmd)
}

func BuildCommandShellArgContext(ctx context.Context, shell ShellConfig, cmd string) *exec.Cmd {
return exec.CommandContext(ctx, shell.ShellCommand, shell.ShellArgument, cmd)
}

func getRunnerShell() string {
shell, ok := os.LookupEnv("SHELL")
shell, ok := os.LookupEnv("COMPOSE_SHELL")
if !ok {
if runtime.GOOS == "windows" {
shell = "cmd"
Expand All @@ -28,9 +37,23 @@ func getRunnerShell() string {
}

func getRunnerArg() string {
arg := "-c"
if runtime.GOOS == "windows" {
return "/C"
} else {
return "-c"
arg = "/C"
}
return arg
}

func DefaultShellConfig() *ShellConfig {
return &ShellConfig{
ShellCommand: getRunnerShell(),
ShellArgument: getRunnerArg(),
}
}

func ValidateShellConfig(shell ShellConfig) {
_, err := exec.LookPath(shell.ShellCommand)
if err != nil {
log.Fatal().Msgf("Couldn't find %s", shell.ShellCommand)
}
}
6 changes: 6 additions & 0 deletions src/command/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package command

type ShellConfig struct {
ShellCommand string `yaml:"shell_command"`
ShellArgument string `yaml:"shell_argument"`
}

0 comments on commit df0022b

Please sign in to comment.