diff --git a/cmd/nixose/main.go b/cmd/nixose/main.go index 4bf6d23..e76a15f 100644 --- a/cmd/nixose/main.go +++ b/cmd/nixose/main.go @@ -6,6 +6,7 @@ import ( "fmt" "log" "os" + "regexp" "strings" "time" @@ -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.") @@ -43,6 +45,15 @@ 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), @@ -50,6 +61,7 @@ func main() { Paths: paths, EnvFiles: envFiles, EnvFilesOnly: *envFilesOnly, + ServiceInclude: serviceIncludeRegexp, AutoStart: *autoStart, UseComposeLogDriver: *useComposeLogDriver, } diff --git a/compose.go b/compose.go index 21a0272..ef6debb 100644 --- a/compose.go +++ b/compose.go @@ -154,6 +154,7 @@ type Generator struct { Runtime ContainerRuntime Paths []string EnvFiles []string + ServiceInclude *regexp.Regexp AutoStart bool EnvFilesOnly bool UseComposeLogDriver bool @@ -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)