-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add file/should/have_permission expression, replace some panics…
… with errors
- Loading branch information
Showing
22 changed files
with
287 additions
and
85 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
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
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
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
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
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
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,76 @@ | ||
package should | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"goarkitect/internal/arch/rule" | ||
"os" | ||
"path/filepath" | ||
"regexp" | ||
) | ||
|
||
var ( | ||
ErrInvalidPermissions = errors.New( | ||
"permissions must only contain the following characters: 'd', 'r', 'w', 'x', '-'", | ||
) | ||
) | ||
|
||
func HavePermissions(want string, opts ...Option) *havePermissionsExpression { | ||
rx := regexp.MustCompile("[d-][rwx-]{9}") | ||
|
||
errs := make([]error, 0) | ||
if !rx.MatchString(want) { | ||
errs = append(errs, ErrInvalidPermissions) | ||
} | ||
|
||
expr := &havePermissionsExpression{ | ||
want: want, | ||
baseExpression: baseExpression{ | ||
errors: errs, | ||
}, | ||
} | ||
|
||
for _, opt := range opts { | ||
opt.apply(&expr.options) | ||
} | ||
|
||
return expr | ||
} | ||
|
||
type havePermissionsExpression struct { | ||
baseExpression | ||
|
||
want string | ||
} | ||
|
||
func (e havePermissionsExpression) Evaluate(rb rule.Builder) []rule.Violation { | ||
return e.evaluate(rb, e.doEvaluate, e.getViolation) | ||
} | ||
|
||
func (e havePermissionsExpression) doEvaluate(rb rule.Builder, filePath string) bool { | ||
info, err := os.Stat(filePath) | ||
if err != nil { | ||
rb.AddError(err) | ||
|
||
return true | ||
} | ||
|
||
return e.want != info.Mode().String() | ||
} | ||
|
||
func (e havePermissionsExpression) getViolation(filePath string) rule.Violation { | ||
iNodeType := "file" | ||
if info, _ := os.Stat(filePath); info.IsDir() { | ||
iNodeType = "directory" | ||
} | ||
|
||
format := "%s '%s' does not have permissions matching '%s'" | ||
|
||
if e.options.negated { | ||
format = "%s '%s' does have permissions matching '%s'" | ||
} | ||
|
||
return rule.NewViolation( | ||
fmt.Sprintf(format, iNodeType, filepath.Base(filePath), e.want), | ||
) | ||
} |
Oops, something went wrong.