Skip to content

Commit

Permalink
Hooks configuration as list
Browse files Browse the repository at this point in the history
  • Loading branch information
marcinhlybin committed Oct 14, 2024
1 parent c7ac283 commit 864905c
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 33 deletions.
11 changes: 8 additions & 3 deletions .docker-env/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ vscode_default_service: app
vscode_default_dir: /app

# Scripts to run before and after
pre_start_hook: .docker-env/pre-start.sh
post_start_hook: .docker-env/post-start.sh
post_stop_hook: .docker-env/post-stop.sh
pre_start_hooks:
- .docker-env/pre-start.sh

post_start_hooks:
- .docker-env/post-start.sh

post_stop_hooks:
- .docker-env/post-stop.sh
36 changes: 24 additions & 12 deletions cmd/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,23 +62,35 @@ func initializeRegistry(cfg *config.Config) (*registry.DockerProjectRegistry, er
return registry.NewDockerProjectRegistry(cfg), nil
}

func (app *App) RunPreStartHook() error {
func (app *App) RunPreStartHooks() error {
p, cfg := app.Project, app.Config
path := cfg.PreStartHook
hook := addons.NewPreStartHook(path, p.Name, p.ServiceName)
return hook.Run()
for _, path := range cfg.PreStartHooks {
hook := addons.NewPreStartHook(path, p.Name, p.ServiceName)
if err := hook.Run(); err != nil {
return err
}
}
return nil
}

func (app *App) RunPostStartHook() error {
func (app *App) RunPostStartHooks() error {
p, cfg := app.Project, app.Config
path := cfg.PostStartHook
hook := addons.NewPostStartHook(path, p.Name, p.ServiceName)
return hook.Run()
for _, path := range cfg.PostStartHooks {
hook := addons.NewPostStartHook(path, p.Name, p.ServiceName)
if err := hook.Run(); err != nil {
return err
}
}
return nil
}

func (app *App) RunPostStopHook() error {
func (app *App) RunPostStopHooks() error {
p, cfg := app.Project, app.Config
path := cfg.PostStopHook
hook := addons.NewPostStopHook(path, p.Name, p.ServiceName)
return hook.Run()
for _, path := range cfg.PostStopHooks {
hook := addons.NewPostStopHook(path, p.Name, p.ServiceName)
if err := hook.Run(); err != nil {
return err
}
}
return nil
}
4 changes: 2 additions & 2 deletions cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func startAction(c *cli.Context) error {
// Pre-start hooks
withHooks := !c.Bool("no-hooks")
if withHooks && !p.IsRunning() {
if err := app.RunPreStartHook(); err != nil {
if err := app.RunPreStartHooks(); err != nil {
return err
}
}
Expand All @@ -77,7 +77,7 @@ func startAction(c *cli.Context) error {

// Post-start hooks
if withHooks && !p.IsRunning() {
if err := app.RunPostStartHook(); err != nil {
if err := app.RunPostStartHooks(); err != nil {
return err
}
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func stopAction(c *cli.Context) error {
// Post-stop hooks
withHooks := !c.Bool("no-hooks")
if withHooks {
if err := app.RunPostStopHook(); err != nil {
if err := app.RunPostStopHooks(); err != nil {
return err
}
}
Expand Down
33 changes: 21 additions & 12 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package config
import (
"fmt"
"os"
"strings"

"gopkg.in/yaml.v3"
)
Expand All @@ -30,9 +29,9 @@ type Config struct {
AwsLogin bool `yaml:"aws_login"`
AwsRegion string `yaml:"aws_region"`
AwsRepository string `yaml:"aws_repository"`
PreStartHook string `yaml:"pre_start_hook"`
PostStartHook string `yaml:"post_start_hook"`
PostStopHook string `yaml:"post_stop_hook"`
PreStartHooks []string `yaml:"pre_start_hooks"`
PostStartHooks []string `yaml:"post_start_hooks"`
PostStopHooks []string `yaml:"post_stop_hooks"`
RequiredVars []string `yaml:"required_vars"`
ShowExecutedCommands bool `yaml:"show_executed_commands"`
}
Expand All @@ -49,9 +48,9 @@ func NewConfig() *Config {
TerminalDefaultCommand: "/bin/bash",
VscodeDefaultService: "app",
VscodeDefaultDir: "/",
PreStartHook: "",
PostStartHook: "",
PostStopHook: "",
PreStartHooks: []string{},
PostStartHooks: []string{},
PostStopHooks: []string{},
RequiredVars: []string{},
ShowExecutedCommands: true,
}
Expand Down Expand Up @@ -106,12 +105,15 @@ func (c *Config) ShowConfig() error {
fmt.Println("Compose default profile:", c.ComposeDefaultProfile)
fmt.Println("Compose sidecar profile:", c.ComposeSidecarProfile)
fmt.Println()
fmt.Println("Env files:", strings.Join(c.EnvFiles, ", "))
fmt.Println("Required vars:", strings.Join(c.RequiredVars, ", "))
printList("Env files:", c.EnvFiles)
fmt.Println()
fmt.Println("Pre-start hook:", c.PreStartHook)
fmt.Println("Post-start hook:", c.PostStartHook)
fmt.Println("Post-stop hook:", c.PostStopHook)
printList("Required vars:", c.RequiredVars)
fmt.Println()
printList("Pre-start hooks:", c.PreStartHooks)
fmt.Println()
printList("Post-start hooks:", c.PostStartHooks)
fmt.Println()
printList("Post-stop hooks:", c.PostStopHooks)
fmt.Println()
fmt.Println("AWS login:", c.AwsLogin)
fmt.Println("AWS region:", c.AwsRegion)
Expand All @@ -125,3 +127,10 @@ func (c *Config) ShowConfig() error {
fmt.Println("Show executed commands:", c.ShowExecutedCommands)
return nil
}

func printList(title string, items []string) {
fmt.Println(title)
for _, item := range items {
fmt.Println(" -", item)
}
}
3 changes: 0 additions & 3 deletions project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,6 @@ func (p *Project) IsServiceDefined() bool {
}

func (p *Project) IsRunning() bool {
if p.Status == "" {
panic("Internal error: Checking if project is running without status set")
}
return strings.Contains(p.Status, "running")
}

Expand Down

0 comments on commit 864905c

Please sign in to comment.