Skip to content

Commit

Permalink
All set to add rules for .kraftignore
Browse files Browse the repository at this point in the history
Signed-off-by: Md Sahil <[email protected]>
  • Loading branch information
MdSahil-oss committed Nov 20, 2023
1 parent 01c3c7f commit 1603cdb
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 2 deletions.
24 changes: 24 additions & 0 deletions initrd/directory.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,35 @@ func (initrd *directory) Build(ctx context.Context) (string, error) {
writer := cpio.NewWriter(f)
defer writer.Close()

ignoringItems, err := getKraftignoreItems(initrd)
if err != nil {
return "", err
}

// **If the function returns the special value SkipDir, Walk skips the current directory (path if info.IsDir() is true,
// otherwise path's parent directory). If the function returns the special value SkipAll, Walk skips all remaining files and directories.
// Otherwise, if the function returns a non-nil error, Walk stops entirely and returns that error.
// Returning `nil` from the funtion make WalkDir() to work as `continue`.**
if err := filepath.WalkDir(initrd.path, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return fmt.Errorf("received error before parsing path: %w", err)
}

if len(ignoringItems) > 0 {
str, err := isExistInKraftignoreFile(path, ignoringItems)
if err != nil {
return err
}
switch str {
case "SkipDir":
return filepath.SkipDir
case "SkipAll":
return filepath.SkipAll
case "Exist":
return nil
}
}

internal := strings.TrimPrefix(path, filepath.Clean(initrd.path))
internal = filepath.ToSlash(internal)
if internal == "" {
Expand Down
63 changes: 63 additions & 0 deletions initrd/kraftignore.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package initrd

import (
"bufio"
"errors"
"os"
"path/filepath"
"strings"
)

// kraftignore filename
const KraftignoreFileName = ".kraftignore"

func getKraftignoreItems(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)
defer kraftignoreFile.Close()

if err != nil {
return []string{}, err
}
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
}
ignoringItems = append(ignoringItems, item)
}
}
return ignoringItems, nil
}

func isExistInKraftignoreFile(path string, kraftignoreItems []string) (string, error) {
// Write code here
return "", nil
}
17 changes: 15 additions & 2 deletions initrd/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@
// You may not use this file except in compliance with the License.
package initrd

import (
"path/filepath"
)

type InitrdOptions struct {
output string
cacheDir string
output string
cacheDir string
kraftignorePath string
}

type InitrdOption func(*InitrdOptions) error
Expand All @@ -29,3 +34,11 @@ func WithCacheDir(dir string) InitrdOption {
return nil
}
}

// WithKraftignorePath sets the path for .kraftignore.
func WithKraftignorePath(dir string) InitrdOption {
return func(opts *InitrdOptions) error {
opts.kraftignorePath = filepath.Join(dir, KraftignoreFileName)
return nil
}
}

0 comments on commit 1603cdb

Please sign in to comment.