Skip to content

Commit ff296ba

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 e59c041 commit ff296ba

File tree

8 files changed

+95
-5
lines changed

8 files changed

+95
-5
lines changed

cli/pack/common.go

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -277,14 +277,30 @@ func getDestAppDir(bundleEnvPath, appName string,
277277

278278
// copyApplications copies applications from current env to the result bundle.
279279
func copyApplications(bundleEnvPath string, packCtx *PackCtx,
280-
cliOpts, newOpts *config.CliOpts) error {
281-
var err error
280+
cliOpts, newOpts *config.CliOpts, toIgnore map[string]struct{}) error {
282281
for appName, instances := range packCtx.AppsInfo {
283282
if len(instances) == 0 {
284283
return fmt.Errorf("application %q does not have any instances", appName)
285284
}
286285
inst := instances[0]
287286
appPath := inst.AppDir
287+
288+
projectPath := filepath.Dir(packCtx.configFilePath)
289+
relAppPath, err := filepath.Rel(projectPath, appPath)
290+
if err != nil {
291+
return err
292+
}
293+
relAppPathUnix := filepath.ToSlash(relAppPath)
294+
295+
ignore, err := shouldIgnore(relAppPathUnix, toIgnore)
296+
if err != nil {
297+
return err
298+
}
299+
if ignore {
300+
log.Infof("Application %s found in .packignore, skipping it...", appName)
301+
continue
302+
}
303+
288304
if inst.IsFileApp {
289305
appPath = inst.InstanceScript
290306
resolvedAppPath, err := filepath.EvalSymlinks(appPath)
@@ -363,6 +379,12 @@ func prepareBundle(cmdCtx *cmdcontext.CmdCtx, packCtx *PackCtx,
363379
packCtx.AppList = getAppNamesToPack(packCtx)
364380
log.Infof("Apps to pack: %s", strings.Join(packCtx.AppList, " "))
365381

382+
projectPath := filepath.Dir(packCtx.configFilePath)
383+
ignorePatterns, err := readPackIgnore(projectPath)
384+
if err != nil {
385+
return "", fmt.Errorf("failed to read .packignore: %v", err)
386+
}
387+
366388
if bundleEnvPath, err = updateEnvPath(bundleEnvPath, packCtx, cliOpts); err != nil {
367389
return "", err
368390
}
@@ -373,12 +395,12 @@ func prepareBundle(cmdCtx *cmdcontext.CmdCtx, packCtx *PackCtx,
373395
return "", fmt.Errorf("error copying binaries: %s", err)
374396
}
375397

376-
if err = copyApplications(bundleEnvPath, packCtx, cliOpts, newOpts); err != nil {
398+
if err = copyApplications(bundleEnvPath, packCtx, cliOpts, newOpts, ignorePatterns); err != nil {
377399
return "", fmt.Errorf("error copying applications: %s", err)
378400
}
379401

380402
if packCtx.Archive.All {
381-
if err = copyArtifacts(*packCtx, bundleEnvPath, newOpts, packCtx.AppsInfo); err != nil {
403+
if err = copyArtifacts(*packCtx, bundleEnvPath, newOpts, packCtx.AppsInfo, ignorePatterns); err != nil {
382404
return "", fmt.Errorf("failed copying artifacts: %s", err)
383405
}
384406
}
@@ -425,7 +447,7 @@ func copyAppSrc(packCtx *PackCtx, cliOpts *config.CliOpts, srcAppPath, dstAppPat
425447
// copyArtifacts copies all artifacts from the current bundle configuration
426448
// to the passed package structure from the passed path.
427449
func copyArtifacts(packCtx PackCtx, basePath string, newOpts *config.CliOpts,
428-
appsInfo map[string][]running.InstanceCtx) error {
450+
appsInfo map[string][]running.InstanceCtx, toIgnore map[string]struct{}) error {
429451

430452
for _, appName := range packCtx.AppList {
431453
for _, inst := range appsInfo[appName] {

cli/pack/ignore.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package pack
2+
3+
import (
4+
"bufio"
5+
"os"
6+
"path/filepath"
7+
"strings"
8+
)
9+
10+
// readPackIgnore reads the .packignore file and returns a slice of ignore patterns.
11+
func readPackIgnore(projectPath string) (map[string]struct{}, error) {
12+
ignoreFilePath := filepath.Join(projectPath, ".packignore")
13+
file, err := os.Open(ignoreFilePath)
14+
if err != nil {
15+
if os.IsNotExist(err) {
16+
return map[string]struct{}{}, nil
17+
}
18+
return nil, err
19+
}
20+
defer file.Close()
21+
22+
patterns := make(map[string]struct{})
23+
scanner := bufio.NewScanner(file)
24+
for scanner.Scan() {
25+
line := scanner.Text()
26+
line = strings.TrimSpace(line)
27+
if line == "" || strings.HasPrefix(line, "#") {
28+
continue
29+
}
30+
patterns[line] = struct{}{}
31+
}
32+
33+
if err := scanner.Err(); err != nil {
34+
return nil, err
35+
}
36+
return patterns, nil
37+
}
38+
39+
// shouldIgnore checks if the given file path matches any of the ignore patterns.
40+
func shouldIgnore(path string, patterns map[string]struct{}) (bool, error) {
41+
for pattern := range patterns {
42+
pattern = filepath.ToSlash(pattern)
43+
filePath := filepath.ToSlash(path)
44+
45+
if strings.HasSuffix(pattern, "/") {
46+
if strings.HasPrefix(filePath, pattern) {
47+
return true, nil
48+
}
49+
continue
50+
}
51+
52+
match, err := filepath.Match(pattern, filePath)
53+
if err != nil {
54+
return false, err
55+
}
56+
if match {
57+
return true, nil
58+
}
59+
}
60+
return false, nil
61+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
app.lua
2+
instances_enabled/app1.lua
3+
instances_enabled/app1

test/integration/pack/test_bundles/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+
../bundle1/app2
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../bundle1/instances_enabled
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../bundle1/modules
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../bundle1/tt.yaml

0 commit comments

Comments
 (0)