-
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
ef8590f
commit d6461e2
Showing
3 changed files
with
143 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,103 @@ | ||
package initrd | ||
|
||
import ( | ||
"bufio" | ||
"context" | ||
"errors" | ||
"io/fs" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"kraftkit.sh/log" | ||
) | ||
|
||
// kraftignore filename | ||
const KraftignoreFileName = ".kraftignore" | ||
|
||
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() { | ||
err = kraftignoreFile.Close() | ||
}() | ||
|
||
kraftignoreScanner := bufio.NewScanner(kraftignoreFile) | ||
kraftignoreScanner.Split(bufio.ScanLines) | ||
var kraftignoreFileLines, ignoringItems []string | ||
|
||
for kraftignoreScanner.Scan() { | ||
kraftignoreFileLines = append(kraftignoreFileLines, kraftignoreScanner.Text()) | ||
} | ||
|
||
for _, 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, | ||
" 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, "/") | ||
|
||
ignoringItems = append(ignoringItems, item) | ||
} | ||
} | ||
|
||
return ignoringItems, err | ||
} | ||
|
||
func isExistInKraftignoreFile(internal string, pathInfo fs.DirEntry, kraftignoreItems []string) string { | ||
for _, ignoringItem := range kraftignoreItems { | ||
if internal == ignoringItem { | ||
if pathInfo.IsDir() { | ||
return "SkipDir" | ||
} | ||
return "Exist" | ||
} | ||
} | ||
return "" | ||
} | ||
|
||
func hasGlobPatterns(item string) bool { | ||
if 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