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

     1  package chmodhelper
     2  
     3  import (
     4  	"os"
     5  
     6  	"gitlab.com/evatix-go/core/chmodhelper/chmodins"
     7  	"gitlab.com/evatix-go/core/errcore"
     8  )
     9  
    10  type chmodApplier struct{}
    11  
    12  func (it chmodApplier) Default(
    13  	fileMode os.FileMode,
    14  	location string,
    15  ) error {
    16  	rwx := New.RwxWrapper.UsingFileModePtr(
    17  		fileMode)
    18  
    19  	return rwx.ApplyChmod(
    20  		false,
    21  		location)
    22  }
    23  
    24  func (it chmodApplier) OnMismatchOption(
    25  	isApply,
    26  	isSkipOnInvalid bool,
    27  	fileMode os.FileMode,
    28  	location string,
    29  ) error {
    30  	if !isApply {
    31  		return nil
    32  	}
    33  
    34  	rwx := New.RwxWrapper.UsingFileModePtr(
    35  		fileMode)
    36  
    37  	return rwx.ApplyChmodOptions(
    38  		isApply,
    39  		true,
    40  		isSkipOnInvalid,
    41  		location)
    42  }
    43  
    44  func (it chmodApplier) OnMismatch(
    45  	isSkipOnInvalid bool,
    46  	fileMode os.FileMode,
    47  	location string,
    48  ) error {
    49  	rwx := New.RwxWrapper.UsingFileModePtr(
    50  		fileMode)
    51  
    52  	return rwx.ApplyChmodOptions(
    53  		true,
    54  		true,
    55  		isSkipOnInvalid,
    56  		location)
    57  }
    58  
    59  func (it chmodApplier) OnMismatchSkipInvalid(
    60  	fileMode os.FileMode,
    61  	location string,
    62  ) error {
    63  	rwx := New.RwxWrapper.UsingFileModePtr(
    64  		fileMode)
    65  
    66  	return rwx.ApplyChmodOptions(
    67  		true,
    68  		true,
    69  		true,
    70  		location)
    71  }
    72  
    73  func (it chmodApplier) SkipInvalidFile(
    74  	fileMode os.FileMode,
    75  	location string,
    76  ) error {
    77  	rwx := New.RwxWrapper.UsingFileModePtr(
    78  		fileMode)
    79  
    80  	return rwx.ApplyChmod(
    81  		true,
    82  		location)
    83  }
    84  
    85  func (it chmodApplier) ApplyIf(
    86  	isApply bool,
    87  	fileMode os.FileMode,
    88  	location string,
    89  ) error {
    90  	if !isApply {
    91  		return nil
    92  	}
    93  
    94  	rwx := New.RwxWrapper.UsingFileModePtr(
    95  		fileMode)
    96  
    97  	return rwx.ApplyChmod(
    98  		false,
    99  		location)
   100  }
   101  
   102  func (it chmodApplier) Options(
   103  	isSkipInvalidPaths,
   104  	isRecursive bool,
   105  	fileMode os.FileMode,
   106  	location string,
   107  ) error {
   108  	rwx := New.RwxWrapper.UsingFileModePtr(
   109  		fileMode)
   110  
   111  	if isRecursive {
   112  		return rwx.ApplyRecursive(
   113  			isSkipInvalidPaths,
   114  			location)
   115  	}
   116  
   117  	return rwx.ApplyChmod(
   118  		isSkipInvalidPaths,
   119  		location)
   120  }
   121  
   122  func (it chmodApplier) RecursivePath(
   123  	isSkipInvalidPaths bool,
   124  	fileMode os.FileMode,
   125  	location string,
   126  ) error {
   127  	return it.Options(
   128  		isSkipInvalidPaths,
   129  		true,
   130  		fileMode,
   131  		location)
   132  }
   133  
   134  func (it chmodApplier) RecursivePaths(
   135  	isContinueOnError bool,
   136  	isSkipInvalidPaths bool,
   137  	fileMode os.FileMode,
   138  	locations ...string,
   139  ) error {
   140  	return it.PathsUsingFileModeConditions(
   141  		fileMode,
   142  		&chmodins.Condition{
   143  			IsSkipOnInvalid:   isSkipInvalidPaths,
   144  			IsContinueOnError: isContinueOnError,
   145  			IsRecursive:       true,
   146  		},
   147  		locations...)
   148  }
   149  
   150  func (it chmodApplier) RecursivePathsContinueOnError(
   151  	isSkipInvalidPaths bool,
   152  	fileMode os.FileMode,
   153  	locations ...string,
   154  ) error {
   155  	return it.PathsUsingFileModeConditions(
   156  		fileMode,
   157  		&chmodins.Condition{
   158  			IsSkipOnInvalid:   isSkipInvalidPaths,
   159  			IsContinueOnError: true,
   160  			IsRecursive:       true,
   161  		},
   162  		locations...)
   163  }
   164  
   165  func (it chmodApplier) RecursivePathsCaptureInvalids(
   166  	fileMode os.FileMode,
   167  	locations ...string,
   168  ) error {
   169  	return it.PathsUsingFileModeConditions(
   170  		fileMode,
   171  		&chmodins.Condition{
   172  			IsContinueOnError: false,
   173  			IsRecursive:       true,
   174  		},
   175  		locations...)
   176  }
   177  
   178  func (it chmodApplier) PathsUsingFileModeRecursive(
   179  	isContinueOnError bool,
   180  	fileMode os.FileMode,
   181  	locations ...string,
   182  ) error {
   183  	return it.PathsUsingFileModeConditions(
   184  		fileMode,
   185  		&chmodins.Condition{
   186  			IsContinueOnError: isContinueOnError,
   187  			IsRecursive:       true,
   188  		},
   189  		locations...)
   190  }
   191  
   192  func (it chmodApplier) PathsUsingFileModeContinueOnErr(
   193  	isRecursive bool,
   194  	fileMode os.FileMode,
   195  	locations ...string,
   196  ) error {
   197  	return it.PathsUsingFileModeConditions(
   198  		fileMode,
   199  		&chmodins.Condition{
   200  			IsContinueOnError: true,
   201  			IsRecursive:       isRecursive,
   202  		},
   203  		locations...)
   204  }
   205  
   206  func (it chmodApplier) PathsUsingFileModeOptions(
   207  	isSkipOnInvalid,
   208  	isContinueOnError,
   209  	isRecursive bool,
   210  	fileMode os.FileMode,
   211  	locations ...string,
   212  ) error {
   213  	return it.PathsUsingFileModeConditions(
   214  		fileMode,
   215  		&chmodins.Condition{
   216  			IsSkipOnInvalid:   isSkipOnInvalid,
   217  			IsContinueOnError: isContinueOnError,
   218  			IsRecursive:       isRecursive,
   219  		},
   220  		locations...)
   221  }
   222  
   223  func (it chmodApplier) PathsUsingFileModeConditions(
   224  	fileMode os.FileMode,
   225  	condition *chmodins.Condition,
   226  	locations ...string,
   227  ) error {
   228  	if len(locations) == 0 {
   229  		return nil
   230  	}
   231  
   232  	if condition == nil {
   233  		return errcore.CannotBeNilOrEmptyType.
   234  			ErrorNoRefs("condition")
   235  	}
   236  
   237  	rwxWrapper := New.RwxWrapper.UsingFileMode(fileMode)
   238  
   239  	return rwxWrapper.ApplyLinuxChmodOnMany(
   240  		condition,
   241  		locations...)
   242  }
   243  
   244  // RwxPartial
   245  //
   246  // can be any length in
   247  // between 0-10 (rest will be fixed by wildcard)
   248  //
   249  // rwxPartial:
   250  //  - "-rwx" will be "-rwx******"
   251  //  - "-rwxr-x" will be "-rwxr-x***"
   252  //  - "-rwxr-x" will be "-rwxr-x***"
   253  func (it chmodApplier) RwxPartial(
   254  	rwxPartial string,
   255  	condition *chmodins.Condition,
   256  	locations ...string,
   257  ) error {
   258  	if len(locations) == 0 {
   259  		return nil
   260  	}
   261  
   262  	rwxInstructionExecutor, err := RwxPartialToInstructionExecutor(
   263  		rwxPartial,
   264  		condition)
   265  
   266  	if err != nil {
   267  		return err
   268  	}
   269  
   270  	return rwxInstructionExecutor.
   271  		ApplyOnPathsPtr(&locations)
   272  }
   273  
   274  // RwxStringApplyChmod
   275  //
   276  //  rwxFullString 10 chars "-rwxrwxrwx"
   277  func RwxStringApplyChmod(
   278  	rwxFullString string, // rwxFullString 10 chars "-rwxrwxrwx"
   279  	condition *chmodins.Condition,
   280  	locations ...string,
   281  ) error {
   282  	if len(locations) == 0 {
   283  		return nil
   284  	}
   285  
   286  	rwxFullStringErr := chmodins.GetRwxFullLengthError(
   287  		rwxFullString)
   288  	if rwxFullStringErr != nil {
   289  		return rwxFullStringErr
   290  	}
   291  
   292  	if condition == nil {
   293  		return errcore.
   294  			CannotBeNilOrEmptyType.
   295  			ErrorNoRefs("condition")
   296  	}
   297  
   298  	rwxWrapper, err := New.RwxWrapper.RwxFullString(
   299  		rwxFullString)
   300  
   301  	if err != nil {
   302  		return err
   303  	}
   304  
   305  	return rwxWrapper.ApplyLinuxChmodOnMany(
   306  		condition,
   307  		locations...)
   308  }
   309  
   310  // RwxOwnerGroupOtherApplyChmod rwxFullString 10 chars "-rwxrwxrwx"
   311  func RwxOwnerGroupOtherApplyChmod(
   312  	rwxOwnerGroupOther *chmodins.RwxOwnerGroupOther,
   313  	condition *chmodins.Condition,
   314  	locations ...string,
   315  ) error {
   316  	if len(locations) == 0 {
   317  		return nil
   318  	}
   319  
   320  	if rwxOwnerGroupOther == nil {
   321  		return errcore.CannotBeNilOrEmptyType.
   322  			ErrorNoRefs("rwxOwnerGroupOther")
   323  	}
   324  
   325  	if condition == nil {
   326  		return errcore.CannotBeNilOrEmptyType.
   327  			ErrorNoRefs("condition")
   328  	}
   329  
   330  	rwxWrapper, err := New.RwxWrapper.RwxFullString(
   331  		rwxOwnerGroupOther.String())
   332  
   333  	if err != nil {
   334  		return err
   335  	}
   336  
   337  	return rwxWrapper.ApplyLinuxChmodOnMany(
   338  		condition,
   339  		locations...)
   340  }