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 Oct 16, 2024
1 parent e59c041 commit ff296ba
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 5 deletions.
32 changes: 27 additions & 5 deletions cli/pack/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,14 +277,30 @@ func getDestAppDir(bundleEnvPath, appName string,

// copyApplications copies applications from current env to the result bundle.
func copyApplications(bundleEnvPath string, packCtx *PackCtx,
cliOpts, newOpts *config.CliOpts) error {
var err error
cliOpts, newOpts *config.CliOpts, toIgnore map[string]struct{}) error {
for appName, instances := range packCtx.AppsInfo {
if len(instances) == 0 {
return fmt.Errorf("application %q does not have any instances", appName)
}
inst := instances[0]
appPath := inst.AppDir

projectPath := filepath.Dir(packCtx.configFilePath)
relAppPath, err := filepath.Rel(projectPath, appPath)
if err != nil {
return err
}
relAppPathUnix := filepath.ToSlash(relAppPath)

ignore, err := shouldIgnore(relAppPathUnix, toIgnore)
if err != nil {
return err
}
if ignore {
log.Infof("Application %s found in .packignore, skipping it...", appName)
continue
}

if inst.IsFileApp {
appPath = inst.InstanceScript
resolvedAppPath, err := filepath.EvalSymlinks(appPath)
Expand Down Expand Up @@ -363,6 +379,12 @@ func prepareBundle(cmdCtx *cmdcontext.CmdCtx, packCtx *PackCtx,
packCtx.AppList = getAppNamesToPack(packCtx)
log.Infof("Apps to pack: %s", strings.Join(packCtx.AppList, " "))

projectPath := filepath.Dir(packCtx.configFilePath)
ignorePatterns, err := readPackIgnore(projectPath)
if err != nil {
return "", fmt.Errorf("failed to read .packignore: %v", err)
}

if bundleEnvPath, err = updateEnvPath(bundleEnvPath, packCtx, cliOpts); err != nil {
return "", err
}
Expand All @@ -373,12 +395,12 @@ func prepareBundle(cmdCtx *cmdcontext.CmdCtx, packCtx *PackCtx,
return "", fmt.Errorf("error copying binaries: %s", err)
}

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

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

for _, appName := range packCtx.AppList {
for _, inst := range appsInfo[appName] {
Expand Down
61 changes: 61 additions & 0 deletions cli/pack/ignore.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package pack

import (
"bufio"
"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.HasSuffix(pattern, "/") {
if strings.HasPrefix(filePath, pattern) {
return true, nil
}
continue
}

match, err := filepath.Match(pattern, filePath)
if err != nil {
return false, err
}
if match {
return true, nil
}
}
return false, nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
app.lua
instances_enabled/app1.lua
instances_enabled/app1
Empty file.

0 comments on commit ff296ba

Please sign in to comment.