Skip to content

Commit

Permalink
Merge pull request #9 from aksiksi/ignore-missing-env-files
Browse files Browse the repository at this point in the history
Flag to ignore missing env files
  • Loading branch information
aksiksi authored Feb 15, 2024
2 parents a0b1d99 + 33e9bba commit 2b37034
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 3 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ build:
test:
go test -v

coverage:
go test -v -covermode=count

flake:
nix build -L .#packages.x86_64-linux.default

Expand All @@ -13,4 +16,4 @@ nixos-test:
./nixos-test/update.sh
nix build -L .#checks.x86_64-linux.integrationTest

.PHONY: build flake nixos-test test
.PHONY: build coverage flake nixos-test test
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ Usage of compose2nix:
only use env file(s) in the NixOS container definitions.
-generate_unused_resources
if set, unused resources (e.g., networks) will be generated even if no containers use them.
-ignore_missing_env_files
if set, missing env files will be ignored.
-include_env_files
include env files in the NixOS container definition.
-inputs string
Expand Down
3 changes: 2 additions & 1 deletion compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type Generator struct {
EnvFiles []string
IncludeEnvFiles bool
EnvFilesOnly bool
IgnoreMissingEnvFiles bool
ServiceInclude *regexp.Regexp
AutoStart bool
UseComposeLogDriver bool
Expand All @@ -70,7 +71,7 @@ type Generator struct {
}

func (g *Generator) Run(ctx context.Context) (*NixContainerConfig, error) {
env, err := ReadEnvFiles(g.EnvFiles, !g.EnvFilesOnly)
env, err := ReadEnvFiles(g.EnvFiles, !g.EnvFilesOnly, g.IgnoreMissingEnvFiles)
if err != nil {
return nil, err
}
Expand Down
14 changes: 13 additions & 1 deletion helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"bufio"
"fmt"
"log"
"os"
"slices"
"strings"
Expand Down Expand Up @@ -35,13 +36,24 @@ func mapToRepeatedFlag(flagName string, m map[string]string) []string {
return flags
}

func ReadEnvFiles(envFiles []string, mergeWithEnv bool) (env []string, _ error) {
// ReadEnvFiles reads the given set of env files into a list of KEY=VAL entries.
//
// If mergeWithEnv is set, the running env is merged with the provided env files. Any
// duplicate variables will be overridden by the running env.
//
// If ignoreMissing is set, any missing env files will be ignored. This is useful for cases
// where an env file is not available during conversion to Nix.
func ReadEnvFiles(envFiles []string, mergeWithEnv, ignoreMissing bool) (env []string, _ error) {
for _, p := range envFiles {
if strings.TrimSpace(p) == "" {
continue
}
f, err := os.Open(p)
if err != nil {
if ignoreMissing {
log.Printf("Ignoring mising env file %s...", p)
continue
}
return nil, fmt.Errorf("failed to open file %s: %w", p, err)
}
defer f.Close()
Expand Down
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var inputs = flag.String("inputs", "docker-compose.yml", "one or more comma-sepa
var envFiles = flag.String("env_files", "", "one or more comma-separated paths to .env file(s).")
var includeEnvFiles = flag.Bool("include_env_files", false, "include env files in the NixOS container definition.")
var envFilesOnly = flag.Bool("env_files_only", false, "only use env file(s) in the NixOS container definitions.")
var ignoreMissingEnvFiles = flag.Bool("ignore_missing_env_files", false, "if set, missing env files will be ignored.")
var output = flag.String("output", "docker-compose.nix", "path to output Nix file.")
var project = flag.String("project", "", "project name used as a prefix for generated resources. this overrides any top-level \"name\" set in the Compose file(s).")
var serviceInclude = flag.String("service_include", "", "regex pattern for services to include.")
Expand Down Expand Up @@ -78,6 +79,7 @@ func main() {
EnvFiles: envFiles,
IncludeEnvFiles: *includeEnvFiles,
EnvFilesOnly: *envFilesOnly,
IgnoreMissingEnvFiles: *ignoreMissingEnvFiles,
ServiceInclude: serviceIncludeRegexp,
AutoStart: *autoStart,
UseComposeLogDriver: *useComposeLogDriver,
Expand Down
16 changes: 16 additions & 0 deletions nix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,3 +299,19 @@ func TestDocker_EnvFilesOnly(t *testing.T) {
t.Errorf("output diff: %s\n", diff)
}
}

func TestDocker_IgnoreMissingEnvFiles(t *testing.T) {
ctx := context.Background()
composePath, envFilePath, _ := getPaths(t)
g := Generator{
Runtime: ContainerRuntimeDocker,
Inputs: []string{composePath},
EnvFiles: []string{path.Join(t.TempDir(), "bad-path"), envFilePath},
IncludeEnvFiles: true,
EnvFilesOnly: true,
IgnoreMissingEnvFiles: true,
}
if _, err := g.Run(ctx); err != nil {
t.Fatal(err)
}
}

0 comments on commit 2b37034

Please sign in to comment.