Skip to content

Commit

Permalink
fix: allow .dockerignore for custom and default runtimes
Browse files Browse the repository at this point in the history
  • Loading branch information
davemooreuws committed Mar 18, 2024
1 parent 5c14009 commit 7f3f944
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
5 changes: 4 additions & 1 deletion pkg/project/runtime/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"testing"

"github.com/google/go-cmp/cmp"
"github.com/spf13/afero"
)

func TestGenerate(t *testing.T) {
Expand All @@ -30,6 +31,8 @@ func TestGenerate(t *testing.T) {
jsFile, _ := os.ReadFile("javascript.dockerfile")
jvmFile, _ := os.ReadFile("jvm.dockerfile")

fs := afero.NewOsFs()

tests := []struct {
name string
handler string
Expand Down Expand Up @@ -63,7 +66,7 @@ func TestGenerate(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
rt, err := NewBuildContext(tt.handler, "", map[string]string{}, []string{}, nil)
rt, err := NewBuildContext(tt.handler, "", map[string]string{}, []string{}, fs)
if err != nil {
t.Error(err)
}
Expand Down
44 changes: 42 additions & 2 deletions pkg/project/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"path/filepath"
"strings"

"github.com/samber/lo"
"github.com/spf13/afero"
)

Expand All @@ -47,6 +48,31 @@ const (

var commonIgnore = []string{".nitric/", "!.nitric/*.yaml", ".git/", ".idea/", ".vscode/", ".github/", "*.dockerfile", "*.dockerignore"}

func getDockerIgnores(dockerIgnorePath string, fs afero.Fs) ([]string, error) {
// Check if the file exists
exists, err := afero.Exists(fs, dockerIgnorePath)
if err != nil {
return nil, err
}

if exists {
// Read the file
content, err := afero.ReadFile(fs, dockerIgnorePath)
if err != nil {
return nil, err
}

// Split the content into lines
lines := lo.Filter[string](strings.Split(string(content), "\n"), func(line string, index int) bool {
return strings.TrimSpace(line) != ""
})

return lines, nil
}

return nil, nil
}

func customBuildContext(entrypointFilePath string, dockerfilePath string, buildArgs map[string]string, additionalIgnores []string, fs afero.Fs) (*RuntimeBuildContext, error) {
// Get the dockerfile contents
// dockerfilePath
Expand All @@ -55,8 +81,6 @@ func customBuildContext(entrypointFilePath string, dockerfilePath string, buildA
return nil, err
}

// Get the ignore file contents

// ensure build args exists
if buildArgs == nil {
buildArgs = map[string]string{}
Expand Down Expand Up @@ -181,11 +205,27 @@ func dartBuildContext(entrypointFilePath string, additionalIgnores []string) (*R
// if a dockerfile path is provided a custom runtime is assumed, otherwise the entrypoint file is used for automatic detection of language runtime.
func NewBuildContext(entrypointFilePath string, dockerfilePath string, buildArgs map[string]string, additionalIgnores []string, fs afero.Fs) (*RuntimeBuildContext, error) {
if dockerfilePath != "" {
dockerIgnorePath := fmt.Sprintf("%s.dockerignore", dockerfilePath)

dockerIgnores, err := getDockerIgnores(dockerIgnorePath, fs)
if err != nil {
return nil, err
}

additionalIgnores = append(additionalIgnores, dockerIgnores...)

return customBuildContext(entrypointFilePath, dockerfilePath, buildArgs, additionalIgnores, fs)
}

ext := filepath.Ext(entrypointFilePath)

dockerIgnores, err := getDockerIgnores(".dockerignore", fs)
if err != nil {
return nil, err
}

additionalIgnores = append(additionalIgnores, dockerIgnores...)

switch ext {
case ".cs":
return csharpBuildContext(entrypointFilePath, additionalIgnores)
Expand Down

0 comments on commit 7f3f944

Please sign in to comment.