Skip to content

Commit

Permalink
Replaced direct os call for mkdir with executor
Browse files Browse the repository at this point in the history
Replaced direct os call for mkdir with executor in MaybeCreateDirWithParents
During a dry run, the executor will log dir creation instead of creating
it.
Removed definition of MaybeCreateDir from util/util.go

Fixes: BUG1064009
Fixes: BUG1064011
  • Loading branch information
arjrishabh committed Feb 13, 2025
1 parent c8046be commit ad71b0d
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 22 deletions.
4 changes: 2 additions & 2 deletions impl/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,12 +256,12 @@ func download(srcURL string, targetDir string,
// We walk through all the entries of pathMap and then copy files matching
// the glob to the the corresponding destDirPath.
// Note that we make sure destDirPath is created with parents before copying.
func filterAndCopy(pathMap map[string]string, errPrefix util.ErrPrefix) error {
func filterAndCopy(pathMap map[string]string, executor executor.Executor, errPrefix util.ErrPrefix) error {
for destDirPath, srcGlob := range pathMap {
// Don't create arch directory unless there's RPMs to be copied.
filesToCopy, _ := filepath.Glob(srcGlob)
if filesToCopy != nil {
if err := util.MaybeCreateDirWithParents(destDirPath, errPrefix); err != nil {
if err := util.MaybeCreateDirWithParents(destDirPath, executor, errPrefix); err != nil {
return err
}
if err := util.CopyToDestDir(srcGlob, destDirPath, errPrefix); err != nil {
Expand Down
8 changes: 4 additions & 4 deletions impl/create_srpm.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (bldr *srpmBuilder) fetchUpstream() error {
// First fetch upstream source
downloadDir := getDownloadDir(bldr.pkgSpec.Name)

if err := util.MaybeCreateDirWithParents(downloadDir, bldr.errPrefix); err != nil {
if err := util.MaybeCreateDirWithParents(downloadDir, bldr.executor, bldr.errPrefix); err != nil {
return err
}

Expand Down Expand Up @@ -247,7 +247,7 @@ func (bldr *srpmBuilder) setupRpmbuildTreeNonSrpm() error {
// Now copy tarball upstream sources to SOURCES
rpmbuildDir := getRpmbuildDir(bldr.pkgSpec.Name)
rpmbuildSourcesDir := filepath.Join(rpmbuildDir, "SOURCES")
if err := util.MaybeCreateDirWithParents(rpmbuildSourcesDir, bldr.errPrefix); err != nil {
if err := util.MaybeCreateDirWithParents(rpmbuildSourcesDir, bldr.executor, bldr.errPrefix); err != nil {
return err
}

Expand All @@ -264,7 +264,7 @@ func (bldr *srpmBuilder) setupRpmbuildTreeNonSrpm() error {
}

rpmbuildSpecsDir := filepath.Join(rpmbuildDir, "SPECS")
if err := util.MaybeCreateDirWithParents(rpmbuildSpecsDir, bldr.errPrefix); err != nil {
if err := util.MaybeCreateDirWithParents(rpmbuildSpecsDir, bldr.executor, bldr.errPrefix); err != nil {
return err
}

Expand Down Expand Up @@ -487,7 +487,7 @@ func (bldr *srpmBuilder) copyResultsToDestDir() error {

pkgSrpmsDestDir := getPkgSrpmsDestDir(bldr.pkgSpec.Name)
if err := util.MaybeCreateDirWithParents(
pkgSrpmsDestDir, bldr.errPrefix); err != nil {
pkgSrpmsDestDir, bldr.executor, bldr.errPrefix); err != nil {
return err
}

Expand Down
4 changes: 2 additions & 2 deletions impl/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func (bldr *mockBuilder) setupDeps() error {
bldr.errPrefix, strings.Join(missingDeps, ","), depsDir)
}

if copyErr := filterAndCopy(pathMap, bldr.errPrefix); copyErr != nil {
if copyErr := filterAndCopy(pathMap, bldr.executor, bldr.errPrefix); copyErr != nil {
return copyErr
}
createRepoErr := bldr.executor.Exec("createrepo", mockDepsDir)
Expand Down Expand Up @@ -270,7 +270,7 @@ func (bldr *mockBuilder) copyResultsToDestDir() error {
fmt.Sprintf("*.%s.rpm", rpmArch))
pathMap[pkgRpmsDestDirForArch] = globPattern
}
copyErr := filterAndCopy(pathMap, bldr.errPrefix)
copyErr := filterAndCopy(pathMap, bldr.executor, bldr.errPrefix)
if copyErr != nil {
return copyErr
}
Expand Down
4 changes: 2 additions & 2 deletions impl/mock_cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func (cfgBldr *mockCfgBuilder) populateTemplateData() error {
}

mockCfgDir := getMockCfgDir(pkg, arch)
if err := util.MaybeCreateDirWithParents(mockCfgDir, cfgBldr.errPrefix); err != nil {
if err := util.MaybeCreateDirWithParents(mockCfgDir, cfgBldr.executor, cfgBldr.errPrefix); err != nil {
return err
}

Expand All @@ -140,7 +140,7 @@ func (cfgBldr *mockCfgBuilder) populateTemplateData() error {
func (cfgBldr *mockCfgBuilder) prep() error {
arch := cfgBldr.arch
mockCfgDir := getMockCfgDir(cfgBldr.pkg, arch)
if err := util.MaybeCreateDirWithParents(mockCfgDir, cfgBldr.errPrefix); err != nil {
if err := util.MaybeCreateDirWithParents(mockCfgDir, cfgBldr.executor, cfgBldr.errPrefix); err != nil {
return err
}

Expand Down
2 changes: 2 additions & 0 deletions impl/mock_cfg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/stretchr/testify/require"

"code.arista.io/eos/tools/eext/dnfconfig"
"code.arista.io/eos/tools/eext/executor"
"code.arista.io/eos/tools/eext/manifest"
"code.arista.io/eos/tools/eext/testutil"
"code.arista.io/eos/tools/eext/util"
Expand Down Expand Up @@ -83,6 +84,7 @@ func testMockConfig(t *testing.T, chained bool) {
buildSpec: &manifestObj.Package[0].Build,
dnfConfig: dnfConfig,
dependencyList: dependencyList,
executor: &executor.OsExecutor{},
},
}

Expand Down
15 changes: 3 additions & 12 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"path/filepath"
"strings"

"code.arista.io/eos/tools/eext/executor"
"github.com/spf13/viper"
"golang.org/x/sys/unix"
)
Expand Down Expand Up @@ -90,20 +91,10 @@ func CheckPath(path string, checkDir bool, checkWritable bool) error {
return nil
}

// MaybeCreateDir creates a directory with permissions 0775
// Pre-existing directories are left untouched.
func MaybeCreateDir(dirPath string, errPrefix ErrPrefix) error {
err := os.Mkdir(dirPath, 0775)
if err != nil && !os.IsExist(err) {
return fmt.Errorf("%s: Error '%s' creating %s", errPrefix, err, dirPath)
}
return nil
}

// MaybeCreateDirWithParents creates a directory at dirPath if one
// doesn't already exist. It also creates any parent directories.
func MaybeCreateDirWithParents(dirPath string, errPrefix ErrPrefix) error {
if err := RunSystemCmd("mkdir", "-p", dirPath); err != nil {
func MaybeCreateDirWithParents(dirPath string, executor executor.Executor, errPrefix ErrPrefix) error {
if err := executor.Exec("mkdir", "-p", dirPath); err != nil {
return fmt.Errorf("%sError '%s' trying to create directory %s with parents",
errPrefix, err, dirPath)
}
Expand Down

0 comments on commit ad71b0d

Please sign in to comment.