Skip to content

Commit

Permalink
pack: added .packignore
Browse files Browse the repository at this point in the history
In some cases, there are files that may be useful, but should not
be part of artifact. The ability to add files and directories to
the .packignore file has been added, which allows you to ignore
these files and directories when packing.

Closes: [812](#812)
  • Loading branch information
Japsty committed Nov 18, 2024
1 parent f87a566 commit 5e2ca12
Show file tree
Hide file tree
Showing 69 changed files with 360 additions and 0 deletions.
23 changes: 23 additions & 0 deletions cli/pack/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ const (
versionLuaFileName = "VERSION.lua"

rocksManifestPath = ".rocks/share/tarantool/rocks/manifest"

ignoreFile = ".packignore"
)

var (
Expand Down Expand Up @@ -285,6 +287,17 @@ func copyApplications(bundleEnvPath string, packCtx *PackCtx,
}
inst := instances[0]
appPath := inst.AppDir
if !inst.IsFileApp {
ignorePatterns, err := readPackIgnore(appPath)
if err != nil {
return fmt.Errorf("failed to read %s: %s", ignoreFile, err)
}

if err = removeIgnoredFiles(appPath, ignorePatterns); err != nil {
return fmt.Errorf("failed to remove ignored files: %s", err)
}
}

if inst.IsFileApp {
appPath = inst.InstanceScript
resolvedAppPath, err := filepath.EvalSymlinks(appPath)
Expand Down Expand Up @@ -383,6 +396,16 @@ func prepareBundle(cmdCtx *cmdcontext.CmdCtx, packCtx *PackCtx,
}
}

projectPath := cmdCtx.Cli.ConfigDir
ignorePatterns, err := readPackIgnore(projectPath)
if err != nil {
return "", fmt.Errorf("failed to read .packignore: %s", err)
}

if err = removeIgnoredFiles(bundleEnvPath, ignorePatterns); err != nil {
return "", fmt.Errorf("failed to remove ignored files: %s", err)
}

if buildRocks {
err = buildAppRocks(cmdCtx, packCtx, cliOpts, bundleEnvPath)
if err != nil && !os.IsNotExist(err) {
Expand Down
20 changes: 20 additions & 0 deletions cli/pack/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1068,6 +1068,26 @@ func Test_prepareBundle(t *testing.T) {
{assert.FileExists, "app/tt.yaml"},
},
},
{
name: "Packing env with packignore",
params: params{
configPath: "testdata/bundle_with_packignore/tt.yaml",
tntExecutable: tntExecutable,
packCtx: PackCtx{Name: "app"},
},
wantErr: false,
checks: []check{
{assert.DirExists, "instances.enabled"},
{assert.NoFileExists, "instances.enabled/app"},

{assert.NoDirExists, "modules"},

{assert.DirExists, "app2"},
{assert.NoDirExists, "app2/var"},

{assert.NoFileExists, "app.lua"},
},
},
}

for _, tt := range tests {
Expand Down
97 changes: 97 additions & 0 deletions cli/pack/ignore.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package pack

import (
"bufio"
"fmt"
"os"
"path/filepath"
"strings"
)

// readPackIgnore reads the .packignore file and returns a slice of ignore patterns.
func readPackIgnore(projectPath string) (map[string]struct{}, error) {
ignoreFilePath := filepath.Join(projectPath, ".packignore")
file, err := os.Open(ignoreFilePath)
if err != nil {
if os.IsNotExist(err) {
return map[string]struct{}{}, nil
}
return nil, err
}
defer file.Close()

patterns := make(map[string]struct{})
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
line = strings.TrimSpace(line)
if line == "" || strings.HasPrefix(line, "#") {
continue
}
patterns[line] = struct{}{}
}

if err := scanner.Err(); err != nil {
return nil, err
}
return patterns, nil
}

// shouldIgnore checks if the given file path matches any of the ignore patterns.
func shouldIgnore(path string, patterns map[string]struct{}) (bool, error) {
for pattern := range patterns {
pattern = filepath.ToSlash(pattern)
filePath := filepath.ToSlash(path)

if strings.HasPrefix(filePath, pattern) {
return true, nil
}

match, err := filepath.Match(pattern, filePath)
if err != nil {
return false, err
}
if match {
return true, nil
}
}
return false, nil
}

// removeIgnoredFiles walks through the directory and removes files or directories
// that match the ignore patterns.
func removeIgnoredFiles(bundleEnvPath string, patterns map[string]struct{}) error {
return filepath.Walk(bundleEnvPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}

relPath, err := filepath.Rel(bundleEnvPath, path)
if err != nil {
return err
}

relPathUnix := filepath.ToSlash(relPath)

ignore, err := shouldIgnore(relPathUnix, patterns)
if err != nil {
return err
}

if ignore {
if info.IsDir() {
err = os.RemoveAll(path)
if err != nil {
return fmt.Errorf("failed to remove directory %q: %s", path, err)
}
return filepath.SkipDir
} else {
err = os.Remove(path)
if err != nil {
return fmt.Errorf("failed to remove file %q: %s", path, err)
}
}
}
return nil
})
}
4 changes: 4 additions & 0 deletions cli/pack/testdata/bundle_with_packignore/.packignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
instances.enabled/app
modules
app2/var
app.lua
Empty file.
1 change: 1 addition & 0 deletions cli/pack/testdata/bundle_with_packignore/app2
1 change: 1 addition & 0 deletions cli/pack/testdata/bundle_with_packignore/instances_enabled
1 change: 1 addition & 0 deletions cli/pack/testdata/bundle_with_packignore/modules
11 changes: 11 additions & 0 deletions cli/pack/testdata/bundle_with_packignore/tt.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
env:
bin_dir: bin
inc_dir: include
instances_enabled: .
tarantoolctl_layout: false
app:
run_dir: var/run
log_dir: var/log
wal_dir: var/lib
memtx_dir: var/lib
vinyl_dir: var/lib
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package = 'app2'
version = 'scm-1'
source = {
url = '/dev/null',
}
-- Put any modules your app depends on here
dependencies = {
'tarantool',
'lua >= 5.1',
}
build = {
type = 'none';
}

Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
inst1:

inst2:
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
testfile
testdir
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package = 'app2'
version = 'scm-1'
source = {
url = '/dev/null',
}
-- Put any modules your app depends on here
dependencies = {
'tarantool',
'lua >= 5.1',
}
build = {
type = 'none';
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
inst1:

inst2:
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
env:
instances_enabled: instances_enabled
restart_on_failure: true
modules:
directory: modules
app:
run_dir: var/run
log_dir: var/log
wal_dir: var/lib
memtx_dir: var/lib
vinyl_dir: var/lib
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
modules
app.lua
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package = 'app2'
version = 'scm-1'
source = {
url = '/dev/null',
}
-- Put any modules your app depends on here
dependencies = {
'tarantool',
'lua >= 5.1',
}
build = {
type = 'none';
}

Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
inst1:

inst2:
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package = 'app2'
version = 'scm-1'
source = {
url = '/dev/null',
}
-- Put any modules your app depends on here
dependencies = {
'tarantool',
'lua >= 5.1',
}
build = {
type = 'none';
}

Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
inst1:

inst2:
Empty file.
11 changes: 11 additions & 0 deletions test/integration/pack/test_bundles/bundle_with_packignore/tt.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
env:
instances_enabled: instances_enabled
restart_on_failure: true
modules:
directory: modules
app:
run_dir: var/run
log_dir: var/log
wal_dir: var/lib
memtx_dir: var/lib
vinyl_dir: var/lib
Loading

0 comments on commit 5e2ca12

Please sign in to comment.