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

     1  package chmodhelper
     2  
     3  import (
     4  	"gitlab.com/evatix-go/core/constants"
     5  	"gitlab.com/evatix-go/core/errcore"
     6  )
     7  
     8  type RwxInstructionExecutors struct {
     9  	items *[]*RwxInstructionExecutor
    10  }
    11  
    12  func NewRwxInstructionExecutors(capacity int) *RwxInstructionExecutors {
    13  	slice := make([]*RwxInstructionExecutor, constants.Zero, capacity)
    14  
    15  	return &RwxInstructionExecutors{
    16  		items: &slice,
    17  	}
    18  }
    19  
    20  // Add skips nil
    21  func (it *RwxInstructionExecutors) Add(
    22  	rwxInstructionExecutor *RwxInstructionExecutor,
    23  ) *RwxInstructionExecutors {
    24  	if rwxInstructionExecutor == nil {
    25  		return it
    26  	}
    27  
    28  	*it.items = append(*it.items, rwxInstructionExecutor)
    29  
    30  	return it
    31  }
    32  
    33  // Adds skips nil
    34  func (it *RwxInstructionExecutors) Adds(
    35  	rwxInstructionExecutors ...*RwxInstructionExecutor,
    36  ) *RwxInstructionExecutors {
    37  	if rwxInstructionExecutors == nil {
    38  		return it
    39  	}
    40  
    41  	items := *it.items
    42  
    43  	for _, executor := range rwxInstructionExecutors {
    44  		items = append(items, executor)
    45  	}
    46  
    47  	*it.items = items
    48  
    49  	return it
    50  }
    51  
    52  func (it *RwxInstructionExecutors) Length() int {
    53  	if it.items == nil {
    54  		return constants.Zero
    55  	}
    56  
    57  	return len(*it.items)
    58  }
    59  
    60  func (it *RwxInstructionExecutors) Count() int {
    61  	return it.Length()
    62  }
    63  
    64  func (it *RwxInstructionExecutors) IsEmpty() bool {
    65  	return it.Length() == 0
    66  }
    67  
    68  func (it *RwxInstructionExecutors) HasAnyItem() bool {
    69  	return it.Length() > 0
    70  }
    71  
    72  func (it *RwxInstructionExecutors) LastIndex() int {
    73  	return it.Length() - 1
    74  }
    75  
    76  func (it *RwxInstructionExecutors) HasIndex(index int) bool {
    77  	return it.LastIndex() >= index
    78  }
    79  
    80  func (it *RwxInstructionExecutors) VerifyRwxModifiers(
    81  	isContinueOnErr,
    82  	isRecursiveIgnore bool,
    83  	locations []string,
    84  ) error {
    85  	if len(locations) == 0 {
    86  		return nil
    87  	}
    88  
    89  	if isContinueOnErr {
    90  		return it.verifyChmodErrorContinueOnErr(
    91  			isRecursiveIgnore,
    92  			locations)
    93  	}
    94  
    95  	for _, executor := range *it.items {
    96  		err := executor.VerifyRwxModifiers(
    97  			isRecursiveIgnore,
    98  			locations)
    99  
   100  		if err != nil {
   101  			return err
   102  		}
   103  	}
   104  
   105  	return nil
   106  }
   107  
   108  func (it *RwxInstructionExecutors) verifyChmodErrorContinueOnErr(
   109  	isRecursiveIgnore bool,
   110  	locations []string,
   111  ) error {
   112  	var sliceErr []string
   113  
   114  	for _, executor := range *it.items {
   115  		err := executor.VerifyRwxModifiers(
   116  			isRecursiveIgnore,
   117  			locations)
   118  
   119  		if err != nil {
   120  			sliceErr = append(
   121  				sliceErr,
   122  				err.Error())
   123  		}
   124  	}
   125  
   126  	return errcore.SliceToError(sliceErr)
   127  }
   128  
   129  func (it *RwxInstructionExecutors) Items() *[]*RwxInstructionExecutor {
   130  	return it.items
   131  }
   132  
   133  func (it *RwxInstructionExecutors) ApplyOnPath(location string) error {
   134  	if it.IsEmpty() {
   135  		return nil
   136  	}
   137  
   138  	for _, executor := range *it.items {
   139  		err := executor.ApplyOnPath(location)
   140  
   141  		if err != nil {
   142  			return err
   143  
   144  		}
   145  	}
   146  
   147  	return nil
   148  }
   149  
   150  func (it *RwxInstructionExecutors) ApplyOnPaths(locations []string) error {
   151  	if len(locations) == 0 {
   152  		return nil
   153  	}
   154  
   155  	return it.ApplyOnPathsPtr(&locations)
   156  }
   157  
   158  func (it *RwxInstructionExecutors) ApplyOnPathsPtr(locations *[]string) error {
   159  	if it.IsEmpty() {
   160  		return nil
   161  	}
   162  
   163  	for _, executor := range *it.items {
   164  		err := executor.ApplyOnPathsPtr(locations)
   165  
   166  		if err != nil {
   167  			return err
   168  		}
   169  	}
   170  
   171  	return nil
   172  }