Skip to content

Commit

Permalink
Fix test command to make it work as string / slice
Browse files Browse the repository at this point in the history
see #8
  • Loading branch information
metal3d committed Apr 1, 2022
1 parent f99f146 commit f8dcd20
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 12 deletions.
39 changes: 32 additions & 7 deletions compose/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,18 +170,18 @@ func parseCommand(s *Service) {
}

// following the command type, it can be a "slice" or a simple sting, so we need to check it
switch s.RawCommand.(type) {
switch v := s.RawCommand.(type) {
case string:
// use shlex to parse the command
command, err := shlex.Split(s.RawCommand.(string))
command, err := shlex.Split(v)
if err != nil {
log.Fatal(err)
}
s.Command = command
case []string:
s.Command = s.RawCommand.([]string)
s.Command = v
case []interface{}:
for _, v := range s.RawCommand.([]interface{}) {
for _, v := range v {
s.Command = append(s.Command, v.(string))
}
default:
Expand All @@ -196,11 +196,11 @@ func parseEnvFiles(s *Service) {
return
}
envfiles := make([]string, 0)
switch s.RawEnvFiles.(type) {
switch v := s.RawEnvFiles.(type) {
case []string:
envfiles = s.RawEnvFiles.([]string)
envfiles = v
case []interface{}:
for _, v := range s.RawEnvFiles.([]interface{}) {
for _, v := range v {
envfiles = append(envfiles, v.(string))
}
default:
Expand All @@ -209,3 +209,28 @@ func parseEnvFiles(s *Service) {
}
s.EnvFiles = envfiles
}

func parseHealthCheck(s *Service) {
// HealthCheck command can be a string or slice of strings
if s.HealthCheck.RawTest == nil {
return
}
switch v := s.HealthCheck.RawTest.(type) {
case string:
var err error
s.HealthCheck.Test, err = shlex.Split(v)
if err != nil {
log.Fatal(err)
}
case []string:
s.HealthCheck.Test = v

case []interface{}:
for _, v := range v {
s.HealthCheck.Test = append(s.HealthCheck.Test, v.(string))
}
default:
log.Printf("%+v %T", s.HealthCheck.RawTest, s.HealthCheck.RawTest)
log.Fatal("HealthCheck type not supported")
}
}
11 changes: 6 additions & 5 deletions compose/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ func NewCompose() *Compose {

// HealthCheck manage generic type to handle TCP, HTTP and TCP health check.
type HealthCheck struct {
Test []string `yaml:"test"`
Interval string `yaml:"interval"`
Timeout string `yaml:"timeout"`
Retries int `yaml:"retries"`
StartPeriod string `yaml:"start_period"`
Test []string `yaml:"-"`
RawTest interface{} `yaml:"test"`
Interval string `yaml:"interval"`
Timeout string `yaml:"timeout"`
Retries int `yaml:"retries"`
StartPeriod string `yaml:"start_period"`
}

// Service represent a "service" in a docker-compose file.
Expand Down

0 comments on commit f8dcd20

Please sign in to comment.