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

     1  package chmodhelper
     2  
     3  import (
     4  	"os"
     5  	"path"
     6  	"path/filepath"
     7  )
     8  
     9  type newSimpleFileReaderWriterCreator struct{}
    10  
    11  // Create
    12  //
    13  // Arguments:
    14  // 	- chmodDir     : applying on parentDir
    15  // 	- chmodFile    : applying on file
    16  // 	- absParentDir : absolute parentDir ( it can be two level before ),
    17  // 	    it doesn't have to be relative to absFilePath but can be relative.
    18  // 	- absFilePath  : absolute file path
    19  func (it newSimpleFileReaderWriterCreator) Create(
    20  	chmodDir,
    21  	chmodFile os.FileMode,
    22  	absParentDir,
    23  	absFilePath string,
    24  ) *SimpleFileReaderWriter {
    25  	return &SimpleFileReaderWriter{
    26  		ChmodDir:               chmodDir,
    27  		ChmodFile:              chmodFile,
    28  		ParentDir:              absParentDir,
    29  		FilePath:               absFilePath,
    30  		IsMustChmodApplyOnFile: true,
    31  		IsApplyChmodOnMismatch: true,
    32  	}
    33  }
    34  
    35  // All
    36  //
    37  // Arguments:
    38  // 	- chmodDir     : applying on parentDir
    39  // 	- chmodFile    : applying on file
    40  // 	- absParentDir : absolute parentDir ( it can be two level before ),
    41  // 	    it doesn't have to be relative to absFilePath but can be relative.
    42  // 	- absFilePath  : absolute file path
    43  func (it newSimpleFileReaderWriterCreator) All(
    44  	chmodDir,
    45  	chmodFile os.FileMode,
    46  	isApplyChmodMust bool,
    47  	isApplyOnMismatch bool,
    48  	absParentDir,
    49  	absFilePath string,
    50  ) *SimpleFileReaderWriter {
    51  	return &SimpleFileReaderWriter{
    52  		ChmodDir:               chmodDir,
    53  		ChmodFile:              chmodFile,
    54  		ParentDir:              absParentDir,
    55  		FilePath:               absFilePath,
    56  		IsMustChmodApplyOnFile: true,
    57  		IsApplyChmodOnMismatch: true,
    58  	}
    59  }
    60  
    61  // CreateClean
    62  //
    63  //  Applies path.Clean() to relative actual path from relative(cmd/..) or (.) path
    64  //  then create the reader, writer.
    65  //
    66  // Arguments:
    67  // 	- chmodDir  : applying on parentDir
    68  // 	- chmodFile : applying on file
    69  // 	- parentDir : absolute parentDir ( it can be two level before ) after clean,
    70  // 	    it doesn't have to be relative to absFilePath but can be relative.
    71  // 	- filePath  : absolute file path after clean or else will not work
    72  func (it newSimpleFileReaderWriterCreator) CreateClean(
    73  	chmodDir,
    74  	chmodFile os.FileMode,
    75  	parentDir,
    76  	filePath string,
    77  ) *SimpleFileReaderWriter {
    78  	parentDir = path.Clean(parentDir)
    79  	filePath = path.Clean(filePath)
    80  
    81  	return &SimpleFileReaderWriter{
    82  		ChmodDir:               chmodDir,
    83  		ChmodFile:              chmodFile,
    84  		ParentDir:              parentDir,
    85  		FilePath:               filePath,
    86  		IsMustChmodApplyOnFile: true,
    87  		IsApplyChmodOnMismatch: true,
    88  	}
    89  }
    90  
    91  // Default
    92  //
    93  //  applies default chmod dir - 0755
    94  //  (filemode.DirDefault), file - 0644  (filemode.FileDefault)
    95  //
    96  // Arguments:
    97  // 	- chmodDir     : applying on parentDir
    98  // 	- chmodFile    : applying on file
    99  // 	- absParentDir : absolute parentDir will be from parent of absFilePath
   100  // 	- absFilePath  : absolute file path
   101  func (it newSimpleFileReaderWriterCreator) Default(
   102  	absFilePath string,
   103  ) *SimpleFileReaderWriter {
   104  	parentDir := filepath.Dir(absFilePath)
   105  
   106  	return &SimpleFileReaderWriter{
   107  		ChmodDir:               dirDefaultChmod,
   108  		ChmodFile:              fileDefaultChmod,
   109  		ParentDir:              parentDir,
   110  		FilePath:               absFilePath,
   111  		IsMustChmodApplyOnFile: true,
   112  		IsApplyChmodOnMismatch: true,
   113  	}
   114  }
   115  
   116  // DefaultCleanPath
   117  //
   118  //  Applies path.Clean() to relative actual path from relative(cmd/..) or (.) path
   119  //  then create the reader, writer.
   120  //
   121  //  applies default chmod dir - 0755
   122  //  (filemode.DirDefault), file - 0644  (filemode.FileDefault)
   123  //
   124  // Arguments:
   125  // 	- chmodDir     : applying on parentDir
   126  // 	- chmodFile    : applying on file
   127  // 	- absFilePath  : absolute file path after clean.
   128  // 	- absParentDir : absolute parentDir will be from parent of absFilePath after clean
   129  func (it newSimpleFileReaderWriterCreator) DefaultCleanPath(
   130  	filePath string,
   131  ) *SimpleFileReaderWriter {
   132  	filePath = path.Clean(filePath)
   133  	parentDir := filepath.Dir(filePath)
   134  
   135  	return &SimpleFileReaderWriter{
   136  		ChmodDir:               dirDefaultChmod,
   137  		ChmodFile:              fileDefaultChmod,
   138  		ParentDir:              parentDir,
   139  		FilePath:               filePath,
   140  		IsMustChmodApplyOnFile: true,
   141  		IsApplyChmodOnMismatch: true,
   142  	}
   143  }
   144  
   145  // Path
   146  //
   147  // Arguments:
   148  // 	- chmodDir     : applying on parentDir
   149  // 	- chmodFile    : applying on file
   150  // 	- absFilePath  : absolute file path.
   151  // 	- parentDir    : will be extracted from absFilePath.
   152  func (it newSimpleFileReaderWriterCreator) Path(
   153  	chmodDir,
   154  	chmodFile os.FileMode,
   155  	absFilePath string,
   156  ) *SimpleFileReaderWriter {
   157  	parentDir := filepath.Dir(absFilePath)
   158  
   159  	return &SimpleFileReaderWriter{
   160  		ChmodDir:               chmodDir,
   161  		ChmodFile:              chmodFile,
   162  		ParentDir:              parentDir,
   163  		FilePath:               absFilePath,
   164  		IsMustChmodApplyOnFile: true,
   165  		IsApplyChmodOnMismatch: true,
   166  	}
   167  }
   168  
   169  func (it newSimpleFileReaderWriterCreator) PathCondition(
   170  	isApplyClean bool,
   171  	chmodDir,
   172  	chmodFile os.FileMode,
   173  	filePath string,
   174  ) *SimpleFileReaderWriter {
   175  	if isApplyClean {
   176  		filePath = path.Clean(filePath)
   177  	}
   178  
   179  	parentDir := filepath.Dir(filePath)
   180  
   181  	return &SimpleFileReaderWriter{
   182  		ChmodDir:               chmodDir,
   183  		ChmodFile:              chmodFile,
   184  		ParentDir:              parentDir,
   185  		FilePath:               filePath,
   186  		IsMustChmodApplyOnFile: true,
   187  		IsApplyChmodOnMismatch: true,
   188  	}
   189  }
   190  
   191  // PathDirDefaultChmod
   192  //
   193  //  dir will be applied with default chmod - 0755
   194  func (it newSimpleFileReaderWriterCreator) PathDirDefaultChmod(
   195  	chmodFile os.FileMode,
   196  	filePath string,
   197  ) *SimpleFileReaderWriter {
   198  	parentDir := filepath.Dir(filePath)
   199  
   200  	return &SimpleFileReaderWriter{
   201  		ChmodDir:               dirDefaultChmod,
   202  		ChmodFile:              chmodFile,
   203  		ParentDir:              parentDir,
   204  		FilePath:               filePath,
   205  		IsMustChmodApplyOnFile: true,
   206  		IsApplyChmodOnMismatch: true,
   207  	}
   208  }