Skip to content

Commit

Permalink
Clean up validate method
Browse files Browse the repository at this point in the history
  • Loading branch information
SBGoods committed Dec 15, 2022
1 parent 7166f1f commit acafaa0
Showing 1 changed file with 19 additions and 29 deletions.
48 changes: 19 additions & 29 deletions internal/localtypes/file_permission_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,49 +29,39 @@ func NewFilePermissionType() FilePermissionType {
// expressed in numeric notation.
// See: https://en.wikipedia.org/wiki/File-system_permissions#Numeric_notation
func (f FilePermissionType) Validate(ctx context.Context, value tftypes.Value, path path.Path) diag.Diagnostics {
var diags diag.Diagnostics

if value.IsNull() {
//return diag.Diagnostics{
// // diag.NewAttributeErrorDiagnostic(path,
// // "Invalid File Permission String Value",
// // "The File Permission value cannot be null"),
// //}
return nil
return diags
}

if !value.IsKnown() {
//return diag.Diagnostics{
// diag.NewAttributeErrorDiagnostic(path,
// "Invalid File Permission String Value",
// "The File Permission value cannot be unknown"),
//}
return nil
return diags
}

var fp string
err := value.As(&fp)
if err != nil {
return diag.Diagnostics{
diag.NewAttributeErrorDiagnostic(path,
"Invalid File Permission String Value",
"An unexpected error occurred while converting the file permission to a string value"+
"Error: "+err.Error()),
}
diags.Append(diag.NewAttributeErrorDiagnostic(path,
"Invalid File Permission String Value",
"An unexpected error occurred while converting the file permission to a string value"+
"Error: "+err.Error()))
return diags
}

if len(fp) < 3 || len(fp) > 4 {
return diag.Diagnostics{
diag.NewAttributeErrorDiagnostic(path,
"Invalid File Permission String Value",
"bad mode permission: string length should be 3 or 4 digits: "+fp),
}
diags.Append(diag.NewAttributeErrorDiagnostic(path,
"Invalid File Permission String Value",
"bad mode permission: string length should be 3 or 4 digits: "+fp))
return diags
}

fileMode, err := strconv.ParseInt(fp, 8, 64)
if err != nil || fileMode > 0777 || fileMode < 0 {
return diag.Diagnostics{
diag.NewAttributeErrorDiagnostic(path,
"Invalid File Permission String Value",
"bad mode permission: string must be expressed in octal numeric notation: "+fp),
}
diags.Append(diag.NewAttributeErrorDiagnostic(path,
"Invalid File Permission String Value",
"bad mode permission: string must be expressed in octal numeric notation: "+fp))
return diags
}
return nil
return diags
}

0 comments on commit acafaa0

Please sign in to comment.