-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Md Sahil <[email protected]>
- Loading branch information
1 parent
f641dcb
commit 791a7ad
Showing
3 changed files
with
164 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package initrd | ||
|
||
import ( | ||
"bufio" | ||
"context" | ||
"errors" | ||
"fmt" | ||
"io/fs" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"kraftkit.sh/log" | ||
) | ||
|
||
// kraftignore filename | ||
const KraftignoreFileName = ".kraftignore" | ||
|
||
type IgnoringFileType string | ||
|
||
const ( | ||
Exist = IgnoringFileType("Exist") | ||
NotExist = IgnoringFileType("NotExist") | ||
SkipDir = IgnoringFileType("SkipDir") | ||
) | ||
|
||
func getKraftignoreItems(ctx context.Context, initrd *directory) ([]string, error) { | ||
if initrd.opts.kraftignorePath == "" { | ||
cwd, err := os.Getwd() | ||
if err != nil { | ||
return []string{}, err | ||
} | ||
initrd.opts.kraftignorePath = filepath.Join(cwd, KraftignoreFileName) | ||
} | ||
|
||
if _, err := os.Stat(initrd.opts.kraftignorePath); errors.Is(err, os.ErrNotExist) { | ||
return []string{}, nil | ||
} else if err != nil { | ||
return []string{}, err | ||
} | ||
|
||
kraftignoreFile, err := os.Open(initrd.opts.kraftignorePath) | ||
if err != nil { | ||
return []string{}, err | ||
} | ||
|
||
defer func() { | ||
kraftIgnoreErr := kraftignoreFile.Close() | ||
if kraftIgnoreErr != nil { | ||
if err != nil { | ||
err = fmt.Errorf("%s\n%s", err, kraftIgnoreErr) | ||
} else { | ||
err = kraftIgnoreErr | ||
} | ||
} | ||
}() | ||
|
||
kraftignoreScanner := bufio.NewScanner(kraftignoreFile) | ||
kraftignoreScanner.Split(bufio.ScanLines) | ||
var kraftignoreFileLines, ignoringItems []string | ||
|
||
for kraftignoreScanner.Scan() { | ||
kraftignoreFileLines = append(kraftignoreFileLines, kraftignoreScanner.Text()) | ||
} | ||
|
||
for lineNum, line := range kraftignoreFileLines { | ||
line = strings.TrimSpace(line) | ||
if line == "" || strings.HasPrefix(line, "#") { | ||
continue | ||
} | ||
items := strings.Split(line, " ") | ||
for _, item := range items { | ||
item = strings.TrimSpace(item) | ||
if item == "" { | ||
continue | ||
} | ||
|
||
if hasGlobPatterns(item) { | ||
log.G(ctx). | ||
WithField("file", initrd.opts.kraftignorePath). | ||
Warn("contains a glob pattern ", item, | ||
" at line ", lineNum, | ||
" which is not supported by Kraftkit") | ||
continue | ||
} | ||
|
||
if strings.HasPrefix(item, "./") { | ||
item = strings.TrimPrefix(item, ".") | ||
} else if !strings.HasPrefix(item, "/") { | ||
item = "/" + item | ||
} | ||
|
||
item = strings.TrimSuffix(item, string(filepath.Separator)) | ||
|
||
ignoringItems = append(ignoringItems, item) | ||
} | ||
} | ||
|
||
return ignoringItems, err | ||
} | ||
|
||
func isExistInKraftignoreFile(internal string, pathInfo fs.DirEntry, kraftignoreItems []string) IgnoringFileType { | ||
for _, ignoringItem := range kraftignoreItems { | ||
if internal == ignoringItem { | ||
if pathInfo.IsDir() { | ||
return SkipDir | ||
} | ||
return Exist | ||
} | ||
} | ||
return NotExist | ||
} | ||
|
||
func hasGlobPatterns(item string) bool { | ||
if strings.Contains(item, "*") || strings.Contains(item, "?") || | ||
strings.Contains(item, "!") || strings.Contains(item, "[") || | ||
strings.Contains(item, "{") { | ||
return true | ||
} | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters