Skip to content

Commit

Permalink
add service include regex option
Browse files Browse the repository at this point in the history
  • Loading branch information
aksiksi committed Nov 6, 2023
1 parent d30142f commit f62253e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
12 changes: 12 additions & 0 deletions cmd/nixose/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"log"
"os"
"regexp"
"strings"
"time"

Expand All @@ -18,6 +19,7 @@ var envFilesOnly = flag.Bool("env_files_only", false, "only use env files in the
var output = flag.String("output", "", "path to output Nix file")
var project = flag.String("project", "", "project name used as a prefix for generated resources")
var projectSeparator = flag.String("project_separator", nixose.DefaultProjectSeparator, "seperator for project prefix")
var serviceInclude = flag.String("service_include", "", "regex pattern for services to include")
var autoStart = flag.Bool("auto_start", true, "control auto-start setting for containers")
var runtime = flag.String("runtime", "podman", `"podman" or "docker"`)
var useComposeLogDriver = flag.Bool("use_compose_log_driver", false, "if set, always use the Compose log driver.")
Expand All @@ -43,13 +45,23 @@ func main() {
log.Fatalf("Invalid --runtime: %q", *runtime)
}

var serviceIncludeRegexp *regexp.Regexp
if *serviceInclude != "" {
pat, err := regexp.Compile(*serviceInclude)
if err != nil {
log.Fatalf("Failed to parse -service_include pattern %q: %v", *serviceInclude, err)
}
serviceIncludeRegexp = pat
}

start := time.Now()
g := nixose.Generator{
Project: nixose.NewProjectWithSeparator(*project, *projectSeparator),
Runtime: containerRuntime,
Paths: paths,
EnvFiles: envFiles,
EnvFilesOnly: *envFilesOnly,
ServiceInclude: serviceIncludeRegexp,
AutoStart: *autoStart,
UseComposeLogDriver: *useComposeLogDriver,
}
Expand Down
5 changes: 5 additions & 0 deletions compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ type Generator struct {
Runtime ContainerRuntime
Paths []string
EnvFiles []string
ServiceInclude *regexp.Regexp
AutoStart bool
EnvFilesOnly bool
UseComposeLogDriver bool
Expand Down Expand Up @@ -326,6 +327,10 @@ func (g *Generator) buildNixContainer(service types.ServiceConfig) (*NixContaine
func (g *Generator) buildNixContainers(composeProject *types.Project) ([]*NixContainer, error) {
var containers []*NixContainer
for _, s := range composeProject.Services {
if g.ServiceInclude != nil && !g.ServiceInclude.MatchString(s.Name) {
log.Printf("Skipping service %q due to include regex %q", s.Name, g.ServiceInclude.String())
continue
}
c, err := g.buildNixContainer(s)
if err != nil {
return nil, fmt.Errorf("failed to build container for service %q: %w", s.Name, err)
Expand Down

0 comments on commit f62253e

Please sign in to comment.