Skip to content

Commit

Permalink
propagate errors during Compose handling
Browse files Browse the repository at this point in the history
  • Loading branch information
aksiksi committed Nov 5, 2023
1 parent 6faae73 commit ee6651e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
22 changes: 14 additions & 8 deletions compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,10 @@ func (g *Generator) Run(ctx context.Context) (*NixContainerConfig, error) {
return nil, err
}

containers := g.buildNixContainers(composeProject)
containers, err := g.buildNixContainers(composeProject)
if err != nil {
return nil, err
}
networks := g.buildNixNetworks(composeProject, containers)
volumes := g.buildNixVolumes(composeProject, containers)

Expand Down Expand Up @@ -204,7 +207,7 @@ func (g *Generator) postProcessContainers(containers []*NixContainer) {
}
}

func (g *Generator) buildNixContainer(service types.ServiceConfig) *NixContainer {
func (g *Generator) buildNixContainer(service types.ServiceConfig) (*NixContainer, error) {
var name string
if service.ContainerName != "" {
name = service.ContainerName
Expand All @@ -214,8 +217,7 @@ func (g *Generator) buildNixContainer(service types.ServiceConfig) *NixContainer

systemdConfig, err := parseRestartPolicyAndSystemdLabels(&service)
if err != nil {
// TODO(aksiksi): Return error here instead of panicing.
panic(err)
return nil, err
}

c := &NixContainer{
Expand Down Expand Up @@ -301,18 +303,22 @@ func (g *Generator) buildNixContainer(service types.ServiceConfig) *NixContainer
c.ExtraOptions = append(c.ExtraOptions, mapToRepeatedKeyValFlag("--log-opt", logging.Options)...)
}

return c
return c, nil
}

func (g *Generator) buildNixContainers(composeProject *types.Project) []*NixContainer {
func (g *Generator) buildNixContainers(composeProject *types.Project) ([]*NixContainer, error) {
var containers []*NixContainer
for _, s := range composeProject.Services {
containers = append(containers, g.buildNixContainer(s))
c, err := g.buildNixContainer(s)
if err != nil {
return nil, fmt.Errorf("failed to build container for service %q: %w", s.Name, err)
}
containers = append(containers, c)
}
slices.SortFunc(containers, func(c1, c2 *NixContainer) int {
return cmp.Compare(c1.Name, c2.Name)
})
return containers
return containers, nil
}

func (g *Generator) buildNixNetworks(composeProject *types.Project, containers []*NixContainer) []*NixNetwork {
Expand Down
1 change: 1 addition & 0 deletions nixose.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ func (c NixContainerConfig) String() string {
}
nixTemplates := template.Must(nixTemplates.Funcs(execTemplateFuncMap).ParseFS(templateFS, "templates/*.tmpl"))
if err := nixTemplates.ExecuteTemplate(&s, "main.nix.tmpl", c); err != nil {
// This should never be hit under normal operation.
panic(err)
}
return s.String()
Expand Down

0 comments on commit ee6651e

Please sign in to comment.