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

     1  package chmodhelper
     2  
     3  import (
     4  	"errors"
     5  	"strings"
     6  
     7  	"gitlab.com/evatix-go/core/chmodhelper/chmodclasstype"
     8  	"gitlab.com/evatix-go/core/chmodhelper/chmodins"
     9  	"gitlab.com/evatix-go/core/constants"
    10  	"gitlab.com/evatix-go/core/errcore"
    11  )
    12  
    13  type SingleRwx struct {
    14  	// Rwx Index Values
    15  	//  - 0: 'r'/'*'/'-'
    16  	//  - 1: 'w'/'*'/'-'
    17  	//  - 2: 'x'/'*'/'-'
    18  	// Examples can be :
    19  	//  - "rwx" or
    20  	//  - "*wx" or
    21  	//  - "rw*" or
    22  	//  - "***"
    23  	//
    24  	// Length must be 3. Not more not less.
    25  	Rwx       string
    26  	ClassType chmodclasstype.Variant
    27  }
    28  
    29  func NewSingleRwx(
    30  	rwx string,
    31  	classType chmodclasstype.Variant,
    32  ) (*SingleRwx, error) {
    33  	err := GetRwxLengthError(rwx)
    34  
    35  	if err != nil {
    36  		return nil, err
    37  	}
    38  
    39  	return &SingleRwx{
    40  		Rwx:       rwx,
    41  		ClassType: classType,
    42  	}, nil
    43  }
    44  
    45  func (it *SingleRwx) ToRwxOwnerGroupOther() *chmodins.RwxOwnerGroupOther {
    46  	switch it.ClassType {
    47  	case chmodclasstype.All:
    48  		return &chmodins.RwxOwnerGroupOther{
    49  			Owner: it.Rwx,
    50  			Group: it.Rwx,
    51  			Other: it.Rwx,
    52  		}
    53  	case chmodclasstype.Owner:
    54  		return &chmodins.RwxOwnerGroupOther{
    55  			Owner: it.Rwx,
    56  			Group: AllWildcards,
    57  			Other: AllWildcards,
    58  		}
    59  	case chmodclasstype.Group:
    60  		return &chmodins.RwxOwnerGroupOther{
    61  			Owner: AllWildcards,
    62  			Group: it.Rwx,
    63  			Other: AllWildcards,
    64  		}
    65  
    66  	case chmodclasstype.Other:
    67  		return &chmodins.RwxOwnerGroupOther{
    68  			Owner: AllWildcards,
    69  			Group: AllWildcards,
    70  			Other: it.Rwx,
    71  		}
    72  
    73  	case chmodclasstype.OwnerGroup:
    74  		return &chmodins.RwxOwnerGroupOther{
    75  			Owner: it.Rwx,
    76  			Group: it.Rwx,
    77  			Other: AllWildcards,
    78  		}
    79  
    80  	case chmodclasstype.GroupOther:
    81  		return &chmodins.RwxOwnerGroupOther{
    82  			Owner: AllWildcards,
    83  			Group: it.Rwx,
    84  			Other: it.Rwx,
    85  		}
    86  
    87  	case chmodclasstype.OwnerOther:
    88  		return &chmodins.RwxOwnerGroupOther{
    89  			Owner: it.Rwx,
    90  			Group: AllWildcards,
    91  			Other: it.Rwx,
    92  		}
    93  
    94  	default:
    95  		panic(chmodclasstype.BasicEnumImpl.RangesInvalidErr())
    96  	}
    97  }
    98  
    99  func (it *SingleRwx) ToRwxInstruction(
   100  	conditionalIns *chmodins.Condition,
   101  ) *chmodins.RwxInstruction {
   102  	rwxOwnerGroupOther := it.ToRwxOwnerGroupOther()
   103  
   104  	return &chmodins.RwxInstruction{
   105  		RwxOwnerGroupOther: *rwxOwnerGroupOther,
   106  		Condition:          *conditionalIns,
   107  	}
   108  }
   109  
   110  func (it *SingleRwx) ToVarRwxWrapper() (*RwxVariableWrapper, error) {
   111  	rwxOwnerGroupOther := it.ToRwxOwnerGroupOther()
   112  
   113  	return ParseRwxOwnerGroupOtherToRwxVariableWrapper(rwxOwnerGroupOther)
   114  }
   115  
   116  func (it *SingleRwx) ToDisabledRwxWrapper() (*RwxWrapper, error) {
   117  	rwxOwnerGroupOther := it.ToRwxOwnerGroupOther()
   118  	rwxFullString := rwxOwnerGroupOther.String()
   119  	rwxFullString = strings.ReplaceAll(
   120  		rwxFullString,
   121  		constants.WildcardSymbol,
   122  		constants.Hyphen)
   123  
   124  	rwxWrapper, err := New.RwxWrapper.RwxFullString(
   125  		rwxFullString)
   126  
   127  	if err != nil {
   128  		return nil, err
   129  	}
   130  
   131  	return &rwxWrapper, err
   132  }
   133  
   134  func (it *SingleRwx) ToRwxWrapper() (*RwxWrapper, error) {
   135  	if !it.ClassType.IsAll() {
   136  		return nil, errcore.MeaningfulError(
   137  			errcore.CannotConvertToRwxWhereVarRwxPossibleType,
   138  			"ToRwxWrapper",
   139  			errors.New("use ToVarRwx"))
   140  	}
   141  
   142  	rwxWrapper, err := New.RwxWrapper.UsingRwxOwnerGroupOther(
   143  		it.ToRwxOwnerGroupOther())
   144  
   145  	if err != nil {
   146  		return nil, err
   147  	}
   148  
   149  	return &rwxWrapper, err
   150  }
   151  
   152  func (it *SingleRwx) ApplyOnMany(
   153  	condition *chmodins.Condition,
   154  	locations ...string,
   155  ) error {
   156  	if len(locations) == 0 {
   157  		return nil
   158  	}
   159  
   160  	toRwxInstruction := it.ToRwxInstruction(condition)
   161  	executor, err := ParseRwxInstructionToExecutor(toRwxInstruction)
   162  
   163  	if err != nil {
   164  		return err
   165  	}
   166  
   167  	return executor.ApplyOnPathsPtr(&locations)
   168  }