Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] .jermignore for ignoring files during packaging #3

Merged
merged 11 commits into from
Sep 18, 2023
2 changes: 2 additions & 0 deletions assets/tests/.jermignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
testfile1
testfile2
Empty file added assets/tests/jerm.json
Empty file.
Empty file added assets/tests/testfile1
Empty file.
Empty file added assets/tests/testfile2
Empty file.
46 changes: 35 additions & 11 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config

import (
"bufio"
"context"
"encoding/json"
"fmt"
Expand All @@ -13,23 +14,25 @@ import (
)

const (
Dev Stage = "dev"
Production Stage = "production"
Staging Stage = "staging"
DefaultRegion = "us-west-2"
Dev Stage = "dev"
Production Stage = "production"
Staging Stage = "staging"
DefaultRegion = "us-west-2"
jermIgnoreFile = ".jermignore"
)

type Stage string

// Config is the Jerm configuration details
type Config struct {
Name string `json:"name"`
Stage string `json:"stage"`
Bucket string `json:"bucket"`
Region string `json:"region"`
Lambda *Lambda `json:"lambda"`
Dir string `json:"dir"`
Entry string `json:"entry"`
Name string `json:"name"`
Stage string `json:"stage"`
Bucket string `json:"bucket"`
Region string `json:"region"`
Lambda *Lambda `json:"lambda"`
Ignore []string `json:"ignore"`
Dir string `json:"dir"`
Entry string `json:"entry"`
}

// Defaults extracts the default configuration
Expand Down Expand Up @@ -101,6 +104,27 @@ func (c *Config) init() error {
return nil
}

func ReadIgnoredFiles(file string) ([]string, error) {
f, err := os.Open(file)
if err != nil {
return nil, err
}
defer f.Close()

fileScanner := bufio.NewScanner(f)
fileScanner.Split(bufio.ScanLines)
var fileLines []string

for fileScanner.Scan() {
if strings.TrimSpace(fileScanner.Text()) == "" {
continue
}
fileLines = append(fileLines, strings.TrimSpace(fileScanner.Text()))
}

return fileLines, nil
}

// ReadConfig reads a configuration file
func ReadConfig(path string) (*Config, error) {
data, err := os.ReadFile(path)
Expand Down
8 changes: 8 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,11 @@ func TestReadConfigAlwaysReturnsConfig(t *testing.T) {
cfg, _ := ReadConfig("")
assert.NotNil(cfg)
}

func TestIgnoredFiles(t *testing.T) {
assert := assert.New(t)
files, err := ReadIgnoredFiles("../assets/tests/.jermignore")
expected := []string{"testfile1", "testfile2"}
assert.Nil(err)
assert.Equal(expected, files)
}
27 changes: 18 additions & 9 deletions config/python.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,12 @@ func (p *Python) Build(config *Config) (string, error) {
return "", err
}

err = p.copyNecessaryFilesToTempDir(config.Dir, tempDir)
err = p.copyNecessaryFilesToTempDir(config.Dir, tempDir, jermIgnoreFile)
if err != nil {
return "", err
}

err = p.copyNecessaryFilesToTempDir(sitePackages, tempDir)
err = p.copyNecessaryFilesToTempDir(sitePackages, tempDir, jermIgnoreFile)
if err != nil {
return "", err
}
Expand All @@ -141,21 +141,30 @@ func (p *Python) Build(config *Config) (string, error) {
}

// Copies files from src to dest
func (p *Python) copyNecessaryFilesToTempDir(src, dest string) error {
func (p *Python) copyNecessaryFilesToTempDir(src, dest, ignoreFile string) error {
log.Debug("copying necessary Python files...")

ignoredFiles := defaultIgnoredGlobs
files, err := ReadIgnoredFiles(ignoreFile)
if err == nil {
ignoredFiles = append(ignoredFiles, files...)
}

opt := copy.Options{
Skip: func(srcinfo os.FileInfo, src, dest string) (bool, error) {
for _, glob := range defaultIgnoredGlobs {
matchFile := strings.HasSuffix(src, glob) ||
strings.HasPrefix(src, glob) || src == glob
if matchFile {
return matchFile, nil
for _, ignoredFile := range ignoredFiles {
match, _ := filepath.Match(ignoredFile, srcinfo.Name())
matchedFile := srcinfo.Name() == ignoredFile || match ||
strings.HasSuffix(srcinfo.Name(), ignoredFile) ||
strings.HasPrefix(srcinfo.Name(), ignoredFile)
if matchedFile {
return matchedFile, nil
}
}
return false, nil
},
}
err := copy.Copy(src, dest, opt)
err = copy.Copy(src, dest, opt)
if err != nil {
return err
}
Expand Down
37 changes: 37 additions & 0 deletions config/python_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package config

import (
"os"
"testing"

"github.com/spatocode/jerm/internal/utils"
"github.com/stretchr/testify/assert"
)

func TestIgnoredFilesWhileCopying(t *testing.T) {
assert := assert.New(t)
jermJson := "../assets/jerm.json"
jermIgnore := "../assets/.jermignore"

pr := NewPythonRuntime()
p := pr.(*Python)
err := p.copyNecessaryFilesToTempDir("../assets/tests", "../assets", "../assets/tests/.jermignore")
testfile1Exists := utils.FileExists("../assets/testfile1")
testfile2Exists := utils.FileExists("../assets/testfile2")
jermJsonExists := utils.FileExists(jermJson)
jermIgnoreExists := utils.FileExists(jermIgnore)

assert.Nil(err)
assert.False(testfile1Exists)
assert.False(testfile2Exists)
assert.True(jermJsonExists)
assert.True(jermIgnoreExists)

cleanup([]string{jermJson, jermIgnore})
}

func cleanup(files []string) {
for _, file := range files {
os.Remove(file)
}
}
Loading