Skip to content

Commit

Permalink
Code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
andyone committed Oct 9, 2023
1 parent 1cfa31b commit 1d44516
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 28 deletions.
20 changes: 7 additions & 13 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,26 +223,20 @@ func validateOptions() {
errsDir := options.GetS(OPT_ERROR_DIR)

if errsDir != "" {
switch {
case !fsutil.IsExist(errsDir):
printErrorAndExit("Directory %s doesn't exist", errsDir)

case !fsutil.IsDir(errsDir):
printErrorAndExit("Object %s is not a directory", errsDir)
err := fsutil.ValidatePerms("DW", errsDir)

case !fsutil.IsWritable(errsDir):
printErrorAndExit("Directory %s is not writable", errsDir)
if err != nil {
printErrorAndExit(err.Error())
}
}

wrkDir := options.GetS(OPT_DIR)

if wrkDir != "" {
switch {
case !fsutil.IsExist(wrkDir):
printErrorAndExit("Directory %s doesn't exist", wrkDir)
case !fsutil.IsDir(wrkDir):
printErrorAndExit("Object %s is not a directory", wrkDir)
err := fsutil.ValidatePerms("DR", wrkDir)

if err != nil {
printErrorAndExit(err.Error())
}
}
}
Expand Down
15 changes: 1 addition & 14 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ var tagRegex = regexp.MustCompile(`^\+?command:([a-zA-Z_0-9_-]+)`)

// Parse parse bibop suite
func Parse(file string) (*recipe.Recipe, error) {
err := checkRecipeFile(file)
err := fsutil.ValidatePerms("FRS", file)

if err != nil {
return nil, err
Expand All @@ -52,19 +52,6 @@ func Parse(file string) (*recipe.Recipe, error) {

// ////////////////////////////////////////////////////////////////////////////////// //

// checkRecipeFile check recipe file
func checkRecipeFile(file string) error {
if !fsutil.CheckPerms("FR", file) {
return fmt.Errorf("File %s doesn't exist or not readable", file)
}

if !fsutil.IsNonEmpty(file) {
return fmt.Errorf("File %s is empty", file)
}

return nil
}

// parseRecipeFile parce recipe file
func parseRecipeFile(file string) (*recipe.Recipe, error) {
fd, err := os.Open(file)
Expand Down
2 changes: 1 addition & 1 deletion parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var _ = Suite(&ParseSuite{})
func (s *ParseSuite) TestGlobalErrors(c *C) {
recipe, err := Parse("../testdata/test0.recipe")

c.Assert(err, DeepEquals, errors.New("File ../testdata/test0.recipe doesn't exist or not readable"))
c.Assert(err, DeepEquals, errors.New("File ../testdata/test0.recipe doesn't exist or not accessible"))
c.Assert(recipe, IsNil)

recipe, err = Parse("../testdata/test2.recipe")
Expand Down

0 comments on commit 1d44516

Please sign in to comment.