Skip to content

Commit

Permalink
Add docker-compose up/down
Browse files Browse the repository at this point in the history
  • Loading branch information
g41797 committed Oct 8, 2023
1 parent 33789a3 commit 422ccbb
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions sidecar/starter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package sidecar

import (
"fmt"
"os"
"os/exec"
"path"
)

func StartServices() (stopServices func(), err error) {

confFolder, err := ConfFolder()
if err != nil {
return nil, err
}

composePath := path.Join(confFolder, "docker-compose.yml")

return StartServicesWithCompose(composePath)
}

func StartServicesWithCompose(composePath string) (stopServices func(), err error) {
_, err = os.ReadFile(composePath)

if err != nil {
fmt.Println("docker-compose.yml does not exist. Please start required services manually")
return func() {}, nil
}

go newCompositeCmd(composePath, "up").Run()

return newCompositeCmd(composePath, "down").Run, nil
}

type compositeCmd struct {
composePath string
command string
}

func newCompositeCmd(composePath, command string) *compositeCmd {
result := compositeCmd{command: command, composePath: composePath}
return &result
}

func (cmd *compositeCmd) Run() {
osCMD := exec.Command("docker")
osCMD.Args = append(osCMD.Args, "compose")
osCMD.Args = append(osCMD.Args, "-f")
osCMD.Args = append(osCMD.Args, cmd.composePath)
osCMD.Args = append(osCMD.Args, cmd.command)
osCMD.Run()
return
}

0 comments on commit 422ccbb

Please sign in to comment.