Skip to content

Commit b93703e

Browse files
committed
pack: added .packignore
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)
1 parent 1cb65d9 commit b93703e

File tree

42 files changed

+250
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+250
-0
lines changed

cli/pack/common.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,16 @@ func prepareBundle(cmdCtx *cmdcontext.CmdCtx, packCtx *PackCtx,
383383
}
384384
}
385385

386+
projectPath := filepath.Dir(packCtx.configFilePath)
387+
ignorePatterns, err := readPackIgnore(projectPath)
388+
if err != nil {
389+
return "", fmt.Errorf("failed to read .packignore: %v", err)
390+
}
391+
392+
if err = removeIgnoredFiles(bundleEnvPath, ignorePatterns); err != nil {
393+
return "", fmt.Errorf("failed to remove ignored files: %v", err)
394+
}
395+
386396
if buildRocks {
387397
err = buildAppRocks(cmdCtx, packCtx, cliOpts, bundleEnvPath)
388398
if err != nil && !os.IsNotExist(err) {

cli/pack/common_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,6 +1068,26 @@ func Test_prepareBundle(t *testing.T) {
10681068
{assert.FileExists, "app/tt.yaml"},
10691069
},
10701070
},
1071+
{
1072+
name: "Packing env with packignore",
1073+
params: params{
1074+
configPath: "testdata/bundle_with_packignore/tt.yaml",
1075+
tntExecutable: tntExecutable,
1076+
packCtx: PackCtx{Name: "app"},
1077+
},
1078+
wantErr: false,
1079+
checks: []check{
1080+
{assert.DirExists, "instances.enabled"},
1081+
{assert.NoFileExists, "instances.enabled/app"},
1082+
1083+
{assert.NoDirExists, "modules"},
1084+
1085+
{assert.DirExists, "app2"},
1086+
{assert.NoDirExists, "app2/var"},
1087+
1088+
{assert.NoFileExists, "app.lua"},
1089+
},
1090+
},
10711091
}
10721092

10731093
for _, tt := range tests {

cli/pack/ignore.go

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package pack
2+
3+
import (
4+
"bufio"
5+
"fmt"
6+
"os"
7+
"path/filepath"
8+
"strings"
9+
)
10+
11+
// readPackIgnore reads the .packignore file and returns a slice of ignore patterns.
12+
func readPackIgnore(projectPath string) (map[string]struct{}, error) {
13+
ignoreFilePath := filepath.Join(projectPath, ".packignore")
14+
file, err := os.Open(ignoreFilePath)
15+
if err != nil {
16+
if os.IsNotExist(err) {
17+
return map[string]struct{}{}, nil
18+
}
19+
return nil, err
20+
}
21+
defer file.Close()
22+
23+
patterns := make(map[string]struct{})
24+
scanner := bufio.NewScanner(file)
25+
for scanner.Scan() {
26+
line := scanner.Text()
27+
line = strings.TrimSpace(line)
28+
if line == "" || strings.HasPrefix(line, "#") {
29+
continue
30+
}
31+
patterns[line] = struct{}{}
32+
}
33+
34+
if err := scanner.Err(); err != nil {
35+
return nil, err
36+
}
37+
return patterns, nil
38+
}
39+
40+
// shouldIgnore checks if the given file path matches any of the ignore patterns.
41+
func shouldIgnore(path string, patterns map[string]struct{}) (bool, error) {
42+
for pattern := range patterns {
43+
pattern = filepath.ToSlash(pattern)
44+
filePath := filepath.ToSlash(path)
45+
46+
if strings.HasSuffix(pattern, "/") {
47+
if strings.HasPrefix(filePath, pattern) {
48+
return true, nil
49+
}
50+
continue
51+
}
52+
53+
match, err := filepath.Match(pattern, filePath)
54+
if err != nil {
55+
return false, err
56+
}
57+
if match {
58+
return true, nil
59+
}
60+
}
61+
return false, nil
62+
}
63+
64+
// removeIgnoredFiles walks through the directory and removes files or directories
65+
// that match the ignore patterns.
66+
func removeIgnoredFiles(bundleEnvPath string, patterns map[string]struct{}) error {
67+
return filepath.Walk(bundleEnvPath, func(path string, info os.FileInfo, err error) error {
68+
if err != nil {
69+
return err
70+
}
71+
72+
relPath, err := filepath.Rel(bundleEnvPath, path)
73+
if err != nil {
74+
return err
75+
}
76+
77+
relPathUnix := filepath.ToSlash(relPath)
78+
79+
ignore, err := shouldIgnore(relPathUnix, patterns)
80+
if err != nil {
81+
return err
82+
}
83+
84+
if ignore {
85+
if info.IsDir() {
86+
err = os.RemoveAll(path)
87+
if err != nil {
88+
return fmt.Errorf("failed to remove directory %q: %v", path, err)
89+
}
90+
return filepath.SkipDir
91+
} else {
92+
err = os.Remove(path)
93+
if err != nil {
94+
return fmt.Errorf("failed to remove file %q: %v", path, err)
95+
}
96+
}
97+
}
98+
return nil
99+
})
100+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
instances.enabled/app
2+
modules
3+
app2/var
4+
app.lua

cli/pack/testdata/bundle_with_packignore/app.lua

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../../test/integration/pack/test_bundles/bundle1/app2
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../../test/integration/pack/test_bundles/bundle1/instances_enabled
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../../test/integration/pack/test_bundles/bundle1/modules
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
env:
2+
bin_dir: bin
3+
inc_dir: include
4+
instances_enabled: .
5+
tarantoolctl_layout: false
6+
app:
7+
run_dir: var/run
8+
log_dir: var/log
9+
wal_dir: var/lib
10+
memtx_dir: var/lib
11+
vinyl_dir: var/lib
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
instances.enabled/app1
2+
modules
3+
app2/var
4+
app.lua

0 commit comments

Comments
 (0)