Skip to content

Commit

Permalink
feat(config): add hooks structure to service configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
yarlson committed Jan 13, 2025
1 parent 116c385 commit ce524f3
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type Service struct {
Forwards []string `yaml:"forwards"`
Recreate bool `yaml:"recreate"`
LocalPorts []int
Hooks *Hooks
}

type HealthCheck struct {
Expand Down Expand Up @@ -82,6 +83,11 @@ type Volume struct {
Path string `yaml:"path" validate:"required,unix_path"`
}

type Hooks struct {
Pre string `yaml:"pre"`
Post string `yaml:"post"`
}

func ParseConfig(data []byte) (*Config, error) {
// Load any .env file from the current directory
_ = godotenv.Load()
Expand Down
101 changes: 101 additions & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,104 @@ volumes:
assert.Contains(suite.T(), err.Error(), "validation error")
assert.Contains(suite.T(), err.Error(), "Config.Dependencies[0].Volumes[0]")
}

func (suite *ConfigTestSuite) TestParseConfig_WithHooks() {
yamlData := []byte(`
project:
name: "test-project"
domain: "example.com"
email: "[email protected]"
servers:
- host: "example.com"
port: 22
user: "user"
ssh_key: "~/.ssh/id_rsa"
services:
- name: "web"
image: "nginx:latest"
port: 80
routes:
- path: "/"
hooks:
pre: "echo 'local pre-hook'"
post: "echo 'local post-hook'"
`)

config, err := ParseConfig(yamlData)

assert.NoError(suite.T(), err)
assert.NotNil(suite.T(), config)
assert.NotNil(suite.T(), config.Services[0].Hooks)

// Test Pre Hooks
assert.NotNil(suite.T(), config.Services[0].Hooks.Pre)
assert.Equal(suite.T(), "echo 'local pre-hook'", config.Services[0].Hooks.Pre)

// Test Post Hooks
assert.NotNil(suite.T(), config.Services[0].Hooks.Post)
assert.Equal(suite.T(), "echo 'local post-hook'", config.Services[0].Hooks.Post)
}

func (suite *ConfigTestSuite) TestParseConfig_PartialHooks() {
yamlData := []byte(`
project:
name: "test-project"
domain: "example.com"
email: "[email protected]"
servers:
- host: "example.com"
port: 22
user: "user"
ssh_key: "~/.ssh/id_rsa"
services:
- name: "web"
image: "nginx:latest"
port: 80
routes:
- path: "/"
hooks:
pre: "echo 'only local pre-hook'"
`)

config, err := ParseConfig(yamlData)

assert.NoError(suite.T(), err)
assert.NotNil(suite.T(), config)
assert.NotNil(suite.T(), config.Services[0].Hooks)

// Test Pre Hooks
assert.NotNil(suite.T(), config.Services[0].Hooks.Pre)
assert.Equal(suite.T(), "echo 'only local pre-hook'", config.Services[0].Hooks.Pre)

// Test Post Hooks
assert.NotNil(suite.T(), config.Services[0].Hooks.Post)
assert.Equal(suite.T(), "", config.Services[0].Hooks.Post)
}

func (suite *ConfigTestSuite) TestParseConfig_InvalidHookFormat() {
yamlData := []byte(`
project:
name: "test-project"
domain: "example.com"
email: "[email protected]"
servers:
- host: "example.com"
port: 22
user: "user"
ssh_key: "~/.ssh/id_rsa"
services:
- name: "web"
image: "nginx:latest"
port: 80
routes:
- path: "/"
hooks:
pre:
invalid_field: true
`)

config, err := ParseConfig(yamlData)

assert.Error(suite.T(), err)
assert.Nil(suite.T(), config)
}

0 comments on commit ce524f3

Please sign in to comment.