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

     1  package chmodhelper
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"os"
     7  	"path/filepath"
     8  	"strings"
     9  	"time"
    10  
    11  	"gitlab.com/evatix-go/core/constants"
    12  	"gitlab.com/evatix-go/core/errcore"
    13  	"gitlab.com/evatix-go/core/namevalue"
    14  )
    15  
    16  type PathExistStat struct {
    17  	Location string
    18  	FileInfo os.FileInfo
    19  	IsExist  bool
    20  	Error    error
    21  }
    22  
    23  func (it *PathExistStat) HasError() bool {
    24  	return it != nil && it.Error != nil
    25  }
    26  
    27  func (it *PathExistStat) IsEmptyError() bool {
    28  	return it == nil || it.Error == nil
    29  }
    30  
    31  func (it *PathExistStat) HasFileInfo() bool {
    32  	return it != nil && it.FileInfo != nil
    33  }
    34  
    35  func (it *PathExistStat) IsInvalidFileInfo() bool {
    36  	return it == nil || it.FileInfo == nil
    37  }
    38  
    39  func (it *PathExistStat) IsFile() bool {
    40  	return it.HasFileInfo() && !it.FileInfo.IsDir()
    41  }
    42  
    43  func (it *PathExistStat) IsDir() bool {
    44  	return it.HasFileInfo() && it.FileInfo.IsDir()
    45  }
    46  
    47  func (it *PathExistStat) LastModifiedDate() *time.Time {
    48  	if it.IsInvalid() {
    49  		return nil
    50  	}
    51  
    52  	lastModifiedTime := it.FileInfo.ModTime()
    53  
    54  	return &lastModifiedTime
    55  }
    56  
    57  func (it *PathExistStat) FileMode() *os.FileMode {
    58  	if it.IsInvalid() {
    59  		return nil
    60  	}
    61  
    62  	fileMode := it.FileInfo.Mode()
    63  
    64  	return &fileMode
    65  }
    66  
    67  func (it *PathExistStat) Size() *int64 {
    68  	if it.IsInvalid() {
    69  		return nil
    70  	}
    71  
    72  	size := it.FileInfo.Size()
    73  
    74  	return &size
    75  }
    76  
    77  func (it *PathExistStat) Split() (dir, filename string) {
    78  	if it.IsInvalid() || it.FileInfo.IsDir() {
    79  		return "", ""
    80  	}
    81  
    82  	return filepath.Split(it.Location)
    83  }
    84  
    85  func (it *PathExistStat) FileName() (filename string) {
    86  	_, fileName := it.Split()
    87  
    88  	return fileName
    89  }
    90  
    91  func (it *PathExistStat) ParentDir() (parentDir string) {
    92  	parentDir, _ = it.Split()
    93  
    94  	return parentDir
    95  }
    96  
    97  func (it *PathExistStat) Parent() *PathExistStat {
    98  	parentDir, _ := it.Split()
    99  
   100  	return GetPathExistStat(parentDir)
   101  }
   102  
   103  func (it *PathExistStat) ParentWithNewPath(additionalPaths ...string) string {
   104  	parentDir, _ := it.Split()
   105  	slice := append([]string{parentDir}, additionalPaths...)
   106  
   107  	return filepath.Join(slice...)
   108  }
   109  
   110  func (it *PathExistStat) ParentWithGlobPatternFiles(globPatterns ...string) ([]string, error) {
   111  	filePath := it.ParentWithNewPath(globPatterns...)
   112  
   113  	return filepath.Glob(filePath)
   114  }
   115  
   116  func (it *PathExistStat) ParentWith(additionalPaths ...string) *PathExistStat {
   117  	return GetPathExistStat(it.ParentWithNewPath(additionalPaths...))
   118  }
   119  
   120  func (it *PathExistStat) CombineWithNewPath(additionalPaths ...string) string {
   121  	slice := append([]string{it.Location}, additionalPaths...)
   122  
   123  	return filepath.Join(slice...)
   124  }
   125  
   126  func (it *PathExistStat) CombineWith(additionalPaths ...string) *PathExistStat {
   127  	return GetPathExistStat(it.CombineWithNewPath(additionalPaths...))
   128  }
   129  
   130  func (it *PathExistStat) DotExt() (dotExt string) {
   131  	_, fileName := it.Split()
   132  
   133  	return filepath.Ext(fileName)
   134  }
   135  
   136  func (it *PathExistStat) Dispose() {
   137  	if it == nil {
   138  		return
   139  	}
   140  
   141  	it.Location = constants.EmptyString
   142  	it.IsExist = false
   143  	it.Error = nil
   144  	it.FileInfo = nil
   145  }
   146  
   147  func (it *PathExistStat) IsInvalid() bool {
   148  	return it == nil ||
   149  		!it.IsExist ||
   150  		it.FileInfo == nil ||
   151  		it.Error != nil
   152  }
   153  
   154  func (it *PathExistStat) HasAnyIssues() bool {
   155  	return it == nil ||
   156  		!it.IsExist ||
   157  		it.FileInfo == nil ||
   158  		it.Error != nil
   159  }
   160  
   161  func (it *PathExistStat) NotExistError() error {
   162  	if it == nil {
   163  		return nil
   164  	}
   165  
   166  	if !it.IsExist || it.FileInfo == nil {
   167  		return it.MeaningFullError()
   168  	}
   169  
   170  	return nil
   171  }
   172  
   173  func (it *PathExistStat) MessageWithPathWrapped(
   174  	message string,
   175  ) string {
   176  	if it == nil {
   177  		return ""
   178  	}
   179  
   180  	return fmt.Sprintf(
   181  		messageWithPathWrappedFormat,
   182  		message,
   183  		it.Location)
   184  }
   185  
   186  // NotAFileError
   187  //
   188  // Get error on:
   189  //  - Path has issues or not exist
   190  //  - Expecting file, if not file then error
   191  func (it *PathExistStat) NotAFileError() error {
   192  	if it == nil {
   193  		return nil
   194  	}
   195  
   196  	if !it.IsExist {
   197  		return it.NotExistError()
   198  	}
   199  
   200  	if it.IsDir() {
   201  		return errcore.ExpectingSimpleNoTypeError(
   202  			it.MessageWithPathWrapped("Expecting file but received directory."),
   203  			"File",
   204  			"Directory")
   205  	}
   206  
   207  	return nil
   208  }
   209  
   210  // NotADirError
   211  //
   212  // Get error on:
   213  //  - Path has issues or not exist
   214  //  - Expecting dir, if not dir then error
   215  func (it *PathExistStat) NotADirError() error {
   216  	if it == nil {
   217  		return nil
   218  	}
   219  
   220  	if !it.IsExist {
   221  		return it.NotExistError()
   222  	}
   223  
   224  	if it.IsFile() {
   225  		return errcore.ExpectingSimpleNoTypeError(
   226  			it.MessageWithPathWrapped("Expecting directory but received file."),
   227  			"Directory",
   228  			"File")
   229  	}
   230  
   231  	return nil
   232  }
   233  
   234  func (it *PathExistStat) MeaningFullError() error {
   235  	if it == nil {
   236  		return nil
   237  	}
   238  
   239  	if it.IsEmptyError() {
   240  		return nil
   241  	}
   242  
   243  	newErrMsg := it.Error.Error() +
   244  		" Location :" +
   245  		it.Location
   246  
   247  	newErr := errors.New(newErrMsg)
   248  	meaningFulErr := errcore.MeaningfulError(
   249  		errcore.PathInvalidErrorType,
   250  		"Function : {PathExistStat.MeaningFullError()}",
   251  		newErr,
   252  	)
   253  
   254  	return meaningFulErr
   255  }
   256  
   257  func (it *PathExistStat) String() string {
   258  	if it == nil {
   259  		return ""
   260  	}
   261  
   262  	slice := errcore.VarNameValuesStrings(
   263  		namevalue.Instance{
   264  			Name:  "Location",
   265  			Value: it.Location,
   266  		},
   267  		namevalue.Instance{
   268  			Name:  "Name",
   269  			Value: it.FileName(),
   270  		},
   271  		namevalue.Instance{
   272  			Name:  "IsExist",
   273  			Value: it.IsExist,
   274  		},
   275  		namevalue.Instance{
   276  			Name:  "IsFile",
   277  			Value: it.IsFile(),
   278  		},
   279  		namevalue.Instance{
   280  			Name:  "IsDir",
   281  			Value: it.IsDir(),
   282  		},
   283  		namevalue.Instance{
   284  			Name:  "Chmod",
   285  			Value: it.FileMode(),
   286  		},
   287  		namevalue.Instance{
   288  			Name:  "Error",
   289  			Value: it.Error,
   290  		})
   291  
   292  	return constants.NewLineSpaceHyphenSpace + strings.Join(
   293  		slice,
   294  		constants.IndentFileInfoEachLineJoiner)
   295  }