Skip to content

Commit

Permalink
fix: addressed comments
Browse files Browse the repository at this point in the history
Signed-off-by: Asish Kumar <[email protected]>
  • Loading branch information
officialasishkumar committed May 11, 2024
1 parent 7239ec4 commit 4b99ed8
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 9 deletions.
2 changes: 1 addition & 1 deletion pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,7 @@ func (c *KpmClient) Package(kclPkg *pkg.KclPkg, tarPath string, vendorMode bool)
}

// Tar the current kcl package into a "*.tar" file.
err := utils.TarDir(kclPkg.HomePath, kclPkg.GetPkgInclude(), kclPkg.GetPkgExclude(), tarPath)
err := utils.TarDir(kclPkg.HomePath, tarPath, kclPkg.GetPkgInclude(), kclPkg.GetPkgExclude())
if err != nil {
return reporter.NewErrorEvent(reporter.FailedPackage, err, "failed to package the kcl module")
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/package/toml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func TestUnMarshalTOML(t *testing.T) {
assert.Equal(t, modfile.Pkg.Edition, "v0.0.1")
assert.Equal(t, modfile.Pkg.Version, "v0.0.1")
assert.Equal(t, modfile.Pkg.Include, []string{"src/", "README.md", "LICENSE"})
assert.Equal(t, modfile.Pkg.Exclude, []string{"target/", ".git/", "*.log"})
assert.Equal(t, modfile.Pkg.Exclude, []string{"target/", ".git/", "*.log"})
assert.Equal(t, len(modfile.Dependencies.Deps), 2)
assert.NotEqual(t, modfile.Dependencies.Deps["MyKcl1"], nil)
assert.Equal(t, modfile.Dependencies.Deps["MyKcl1"].Name, "MyKcl1")
Expand Down
17 changes: 11 additions & 6 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,7 @@ func Exists(path string) (bool, error) {
// todo: Consider using the OCI tarball as the standard tar format.
var ignores = []string{".git", ".tar"}

func TarDir(srcDir string, include []string, exclude []string, tarPath string) error {

func TarDir(srcDir string, tarPath string, include []string, exclude []string) error {
fw, err := os.Create(tarPath)
if err != nil {
log.Fatal(err)
Expand All @@ -137,8 +136,6 @@ func TarDir(srcDir string, include []string, exclude []string, tarPath string) e
tw := tar.NewWriter(fw)
defer tw.Close()

fmt.Println(exclude)

err = filepath.Walk(srcDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
Expand All @@ -150,14 +147,22 @@ func TarDir(srcDir string, include []string, exclude []string, tarPath string) e
}
}

getNewPattern := func(ex string) string {
newPath := ex
if !strings.HasPrefix(ex, srcDir + "/") {
newPath = srcDir + "/" + ex
}
return newPath
}

for _, ex := range exclude {
if strings.Contains(path, ex) {
if matched, _ := filepath.Match(getNewPattern(ex), path); matched {
return nil
}
}

for _, inc := range include {
if !strings.Contains(path, inc) {
if matched, _ := filepath.Match(getNewPattern(inc), path); !matched {
return nil
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func TestTarDir(t *testing.T) {
os.Remove(tarPath)
}
emptyArrayOfStrings := []string{}
err = TarDir(filepath.Join(testDir, "test_src"), emptyArrayOfStrings, emptyArrayOfStrings, tarPath)
err = TarDir(filepath.Join(testDir, "test_src"), tarPath, emptyArrayOfStrings, emptyArrayOfStrings)
assert.Equal(t, err, nil)

_, err = os.Stat(tarPath)
Expand Down

0 comments on commit 4b99ed8

Please sign in to comment.