gitlab.com/evatix-go/core@v1.3.55/chmodhelper/newAttributeCreator.go (about)

     1  package chmodhelper
     2  
     3  import "gitlab.com/evatix-go/core/errcore"
     4  
     5  type newAttributeCreator struct{}
     6  
     7  func (it newAttributeCreator) Create(
     8  	isRead, isWrite, isExecute bool,
     9  ) Attribute {
    10  	return Attribute{
    11  		IsRead:    isRead,
    12  		IsWrite:   isWrite,
    13  		IsExecute: isExecute,
    14  	}
    15  }
    16  
    17  func (it newAttributeCreator) Default(
    18  	isRead, isWrite, isExecute bool,
    19  ) Attribute {
    20  	return Attribute{
    21  		IsRead:    isRead,
    22  		IsWrite:   isWrite,
    23  		IsExecute: isExecute,
    24  	}
    25  }
    26  
    27  // UsingRwxString
    28  //
    29  // Length must be 3
    30  // "rwx" should be put for attributes.
    31  //
    32  // Examples:
    33  //  - read enable all disable    : "r--"
    34  //  - write enable all disable   : "-w-"
    35  //  - execute enable all disable : "--x"
    36  //  - all enabled                : "rwx"
    37  func (it newAttributeCreator) UsingRwxString(
    38  	rwx string,
    39  ) Attribute {
    40  	length := len(rwx)
    41  
    42  	if length != SingleRwxLength {
    43  		panic(GetRwxLengthError(rwx))
    44  	}
    45  
    46  	r := rwx[0]
    47  	w := rwx[1]
    48  	e := rwx[2]
    49  
    50  	return Attribute{
    51  		IsRead:    r == ReadChar,
    52  		IsWrite:   w == WriteChar,
    53  		IsExecute: e == ExecuteChar,
    54  	}
    55  }
    56  
    57  // UsingByteMust
    58  //
    59  //  Byte can be at most 0 to 7
    60  //
    61  // 1 - Execute true
    62  // 2 - Write true
    63  // 3 - Write + Execute true
    64  // 4 - Read true
    65  // 5 - Read + Execute true
    66  // 6 - Read + Write true
    67  // 7 - Read + Write + Execute all true
    68  //
    69  // Warning:
    70  //  Panics if more than 7
    71  func (it newAttributeCreator) UsingByteMust(v7 byte) Attribute {
    72  	attr, err := it.UsingByte(v7)
    73  
    74  	if err != nil {
    75  		panic(attr)
    76  	}
    77  
    78  	return attr
    79  }
    80  
    81  // UsingByte
    82  //
    83  //  Byte can be at most 0 to 7
    84  //
    85  // 1 - Execute true
    86  // 2 - Write true
    87  // 3 - Write + Execute true
    88  // 4 - Read true
    89  // 5 - Read + Execute true
    90  // 6 - Read + Write true
    91  // 7 - Read + Write + Execute all true
    92  //
    93  // Warning:
    94  //  Panics if more than 7
    95  func (it newAttributeCreator) UsingByte(v7 byte) (Attribute, error) {
    96  	if ReadWriteExecute.IsGreaterThan(v7) {
    97  		return Attribute{}, errcore.
    98  			ShouldBeLessThanEqualType.
    99  			Error(
   100  				"v7 byte should not be more than "+ReadWriteExecute.String(),
   101  				v7)
   102  	}
   103  
   104  	// TODO optimize logic in future.
   105  	isRead := v7 >= ReadValue
   106  	isWrite := (isRead && v7 >= ReadWriteValue) || (!isRead && v7 >= WriteValue)
   107  	isExecute := (isWrite && isRead && v7 >= ReadWriteExecuteValue) ||
   108  		(isRead && !isWrite && v7 >= ReadExecuteValue) ||
   109  		(isWrite && !isRead && v7 >= WriteExecuteValue) ||
   110  		(!isRead && !isWrite && v7 >= ExecuteValue)
   111  
   112  	return Attribute{
   113  		IsRead:    isRead,
   114  		IsWrite:   isWrite,
   115  		IsExecute: isExecute,
   116  	}, nil
   117  }
   118  
   119  // UsingVariantMust
   120  //
   121  // safe because converting AttrVariant should never exceed 7
   122  //
   123  // Warning:
   124  //  Panics if more than 7
   125  func (it newAttributeCreator) UsingVariantMust(v AttrVariant) Attribute {
   126  	return it.UsingByteMust(v.Value())
   127  }
   128  
   129  func (it newAttributeCreator) UsingVariant(v AttrVariant) (Attribute, error) {
   130  	return it.UsingByte(v.Value())
   131  }