pkg.re/essentialkaos/ek.v10@v12.41.0+incompatible/fsutil/examples_test.go (about)

     1  package fsutil
     2  
     3  // ////////////////////////////////////////////////////////////////////////////////// //
     4  //                                                                                    //
     5  //                         Copyright (c) 2022 ESSENTIAL KAOS                          //
     6  //      Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0>     //
     7  //                                                                                    //
     8  // ////////////////////////////////////////////////////////////////////////////////// //
     9  
    10  import (
    11  	"fmt"
    12  	"time"
    13  )
    14  
    15  // ////////////////////////////////////////////////////////////////////////////////// //
    16  
    17  func ExampleCheckPerms() {
    18  	dir := "/home/john"
    19  	file := dir + "/test.txt"
    20  
    21  	// Target is file, readable and non-empty
    22  	if CheckPerms("FRS", file) {
    23  		fmt.Println("Everything fine!")
    24  	}
    25  
    26  	// Target is readable, writable and executable
    27  	if CheckPerms("RWX", file) {
    28  		fmt.Println("Everything fine!")
    29  	}
    30  
    31  	// Target is directory, readable, writable and executable
    32  	if CheckPerms("DRWX", file) {
    33  		fmt.Println("Everything fine!")
    34  	}
    35  }
    36  
    37  func ExampleValidatePerms() {
    38  	dir := "/home/john"
    39  	file := dir + "/test.txt"
    40  
    41  	// Target is file, readable and non-empty
    42  	err := ValidatePerms("FRS", file)
    43  
    44  	if err != nil {
    45  		fmt.Println(err.Error())
    46  	}
    47  }
    48  
    49  func ExampleProperPath() {
    50  	paths := []string{
    51  		"/home/john/.config/myapp/config",
    52  		"/home/john/.myappconfig",
    53  		"/etc/myapp.conf",
    54  	}
    55  
    56  	config := ProperPath("FRS", paths)
    57  
    58  	if config != "" {
    59  		fmt.Printf("Used configuration file: %s\n", config)
    60  	} else {
    61  		fmt.Println("Can't find configuration file")
    62  	}
    63  }
    64  
    65  func ExampleIsExist() {
    66  	file := "/home/john/test.txt"
    67  
    68  	if IsExist(file) {
    69  		fmt.Printf("File %s is found on system!\n", file)
    70  	} else {
    71  		fmt.Printf("File %s does not exist!\n", file)
    72  	}
    73  }
    74  
    75  func ExampleIsRegular() {
    76  	target := "/home/john/test.txt"
    77  
    78  	if IsRegular(target) {
    79  		fmt.Printf("%s is a regular file!\n", target)
    80  	} else {
    81  		fmt.Printf("%s is NOT a regular file!\n", target)
    82  	}
    83  }
    84  
    85  func ExampleIsSocket() {
    86  	target := "/var/run/myapp.sock"
    87  
    88  	if IsSocket(target) {
    89  		fmt.Printf("%s is a socket file!\n", target)
    90  	} else {
    91  		fmt.Printf("%s is NOT a socket file!\n", target)
    92  	}
    93  }
    94  
    95  func ExampleIsBlockDevice() {
    96  	target := "/dev/sda"
    97  
    98  	if IsBlockDevice(target) {
    99  		fmt.Printf("%s is a block device!\n", target)
   100  	} else {
   101  		fmt.Printf("%s is NOT a block device!\n", target)
   102  	}
   103  }
   104  
   105  func ExampleIsCharacterDevice() {
   106  	target := "/dev/tty0"
   107  
   108  	if IsCharacterDevice(target) {
   109  		fmt.Printf("%s is a character device!\n", target)
   110  	} else {
   111  		fmt.Printf("%s is NOT a character device!\n", target)
   112  	}
   113  }
   114  
   115  func ExampleIsDir() {
   116  	target := "/home/john"
   117  
   118  	if IsDir(target) {
   119  		fmt.Printf("%s is a directory!\n", target)
   120  	} else {
   121  		fmt.Printf("%s is NOT a directory!\n", target)
   122  	}
   123  }
   124  
   125  func ExampleIsLink() {
   126  	target := "/dev/stdout"
   127  
   128  	if IsLink(target) {
   129  		fmt.Printf("%s is a link!\n", target)
   130  	} else {
   131  		fmt.Printf("%s is NOT a link!\n", target)
   132  	}
   133  }
   134  
   135  func ExampleIsReadable() {
   136  	target := "/home/john/test.txt"
   137  
   138  	if IsReadable(target) {
   139  		fmt.Printf("%s is readable!\n", target)
   140  	} else {
   141  		fmt.Printf("%s is NOT readable!\n", target)
   142  	}
   143  }
   144  
   145  func ExampleIsReadableByUser() {
   146  	target := "/home/john/test.txt"
   147  	user := "johndoe"
   148  
   149  	if IsReadableByUser(target, user) {
   150  		fmt.Printf("%s is readable for user %s!\n", target, user)
   151  	} else {
   152  		fmt.Printf("%s is NOT readable for user %s!\n", target, user)
   153  	}
   154  }
   155  
   156  func ExampleIsWritable() {
   157  	target := "/home/john/test.txt"
   158  
   159  	if IsWritable(target) {
   160  		fmt.Printf("%s is writable!\n", target)
   161  	} else {
   162  		fmt.Printf("%s is NOT writable!\n", target)
   163  	}
   164  }
   165  
   166  func ExampleIsWritableByUser() {
   167  	target := "/home/john/test.txt"
   168  	user := "johndoe"
   169  
   170  	if IsWritableByUser(target, user) {
   171  		fmt.Printf("%s is writable for user %s!\n", target, user)
   172  	} else {
   173  		fmt.Printf("%s is NOT writable for user %s!\n", target, user)
   174  	}
   175  }
   176  
   177  func ExampleIsExecutable() {
   178  	target := "/home/john/myapp"
   179  
   180  	if IsExecutable(target) {
   181  		fmt.Printf("%s is executable!\n", target)
   182  	} else {
   183  		fmt.Printf("%s is NOT executable!\n", target)
   184  	}
   185  }
   186  
   187  func ExampleIsExecutableByUser() {
   188  	target := "/home/john/myapp"
   189  	user := "johndoe"
   190  
   191  	if IsExecutableByUser(target, user) {
   192  		fmt.Printf("%s is executable for user %s!\n", target, user)
   193  	} else {
   194  		fmt.Printf("%s is NOT executable for user %s!\n", target, user)
   195  	}
   196  }
   197  
   198  func ExampleIsEmpty() {
   199  	target := "/home/john/test.txt"
   200  
   201  	if IsEmpty(target) {
   202  		fmt.Printf("%s is an empty file!\n", target)
   203  	} else {
   204  		fmt.Printf("%s is NOT an empty file!\n", target)
   205  	}
   206  }
   207  
   208  func ExampleIsNonEmpty() {
   209  	target := "/home/john/test.txt"
   210  
   211  	if IsNonEmpty(target) {
   212  		fmt.Printf("%s is NOT an empty file!\n", target)
   213  	} else {
   214  		fmt.Printf("%s is an empty file!\n", target)
   215  	}
   216  }
   217  
   218  func ExampleIsEmptyDir() {
   219  	target := "/home/john/myfiles"
   220  
   221  	if IsEmptyDir(target) {
   222  		fmt.Printf("%s is an empty directory!\n", target)
   223  	} else {
   224  		fmt.Printf("%s is NOT an empty directory!\n", target)
   225  	}
   226  }
   227  
   228  func ExampleGetOwner() {
   229  	target := "/home/john/test.txt"
   230  
   231  	uid, gid, err := GetOwner(target)
   232  
   233  	if err != nil {
   234  		panic(err.Error())
   235  	}
   236  
   237  	fmt.Printf("Owner UID (User ID): %d\n", uid)
   238  	fmt.Printf("Owner GID (Group ID): %d\n", gid)
   239  }
   240  
   241  func ExampleGetATime() {
   242  	target := "/home/john/test.txt"
   243  	aTime, err := GetATime(target)
   244  
   245  	if err != nil {
   246  		panic(err.Error())
   247  	}
   248  
   249  	fmt.Printf("Access timestamp (atime): %v\n", aTime)
   250  }
   251  
   252  func ExampleGetCTime() {
   253  	target := "/home/john/test.txt"
   254  	aTime, err := GetCTime(target)
   255  
   256  	if err != nil {
   257  		panic(err.Error())
   258  	}
   259  
   260  	fmt.Printf("Change timestamp (ctime): %v\n", aTime)
   261  }
   262  
   263  func ExampleGetMTime() {
   264  	target := "/home/john/test.txt"
   265  	aTime, err := GetMTime(target)
   266  
   267  	if err != nil {
   268  		panic(err.Error())
   269  	}
   270  
   271  	fmt.Printf("Modified timestamp (mtime): %v\n", aTime)
   272  }
   273  
   274  func ExampleGetSize() {
   275  	target := "/home/john/test.txt"
   276  	size := GetSize(target)
   277  
   278  	if size != -1 {
   279  		fmt.Printf("File size: %d bytes\n", size)
   280  	}
   281  }
   282  
   283  func ExampleGetMode() {
   284  	target := "/home/john/test.txt"
   285  	mode := GetMode(target)
   286  
   287  	if mode != 0 {
   288  		fmt.Printf("File mode: %v\n", mode)
   289  	}
   290  }
   291  
   292  func ExampleCopyFile() {
   293  	target := "/home/john/test.txt"
   294  
   295  	err := CopyFile(target, "/home/bob/test.txt", 0644)
   296  
   297  	if err != nil {
   298  		panic(err.Error())
   299  	}
   300  
   301  	fmt.Printf("File %s successfully copied to bob user directory\n", target)
   302  }
   303  
   304  func ExampleMoveFile() {
   305  	target := "/home/john/test.txt"
   306  
   307  	err := MoveFile(target, "/home/bob/test.txt", 0644)
   308  
   309  	if err != nil {
   310  		panic(err.Error())
   311  	}
   312  
   313  	fmt.Printf("File %s successfully moved to bob user directory\n", target)
   314  }
   315  
   316  func ExampleCopyDir() {
   317  	target := "/home/john/documents"
   318  
   319  	err := CopyDir(target, "/home/bob/")
   320  
   321  	if err != nil {
   322  		panic(err.Error())
   323  	}
   324  
   325  	fmt.Printf("Directory %s successfully copied to bob user directory\n", target)
   326  }
   327  
   328  func ExampleTouchFile() {
   329  	err := TouchFile("/srv/myapp/.lock", 0600)
   330  
   331  	if err != nil {
   332  		panic(err.Error())
   333  	}
   334  
   335  	fmt.Println("Lock file successfully created!")
   336  }
   337  
   338  func ExampleCountLines() {
   339  	file := "/home/john/test.txt"
   340  
   341  	lineNum, err := CountLines(file)
   342  
   343  	if err != nil {
   344  		panic(err.Error())
   345  	}
   346  
   347  	fmt.Printf("File %s contains %d lines of text\n", file, lineNum)
   348  }
   349  
   350  func ExampleList() {
   351  	dir := "/home/john/documents"
   352  
   353  	// List all objects including hidden (files and directries
   354  	// with the dot at the beginning of the file name)
   355  	objects := List(dir, false)
   356  
   357  	if len(objects) == 0 {
   358  		fmt.Printf("Directory %s is empty", dir)
   359  		return
   360  	}
   361  
   362  	fmt.Printf("Directory %s contains:\n", dir)
   363  
   364  	for _, object := range objects {
   365  		fmt.Printf("  %s\n", object)
   366  	}
   367  }
   368  
   369  func ExampleListAll() {
   370  	dir := "/home/john/documents"
   371  
   372  	// List all objects excluding hidden (files and directries
   373  	// with the dot at the beginning of the file name)
   374  	objects := ListAll(dir, true)
   375  
   376  	if len(objects) == 0 {
   377  		fmt.Printf("Directory %s is empty", dir)
   378  		return
   379  	}
   380  
   381  	fmt.Printf("Directory %s contains:\n", dir)
   382  
   383  	for _, object := range objects {
   384  		fmt.Printf("  %s\n", object)
   385  	}
   386  }
   387  
   388  func ExampleListAllDirs() {
   389  	target := "/home/john/documents"
   390  
   391  	// List all directories including hidden (directries
   392  	// with the dot at the beginning of the file name)
   393  	dirs := ListAllDirs(target, true)
   394  
   395  	if len(dirs) == 0 {
   396  		fmt.Printf("Directory %s is empty", target)
   397  		return
   398  	}
   399  
   400  	fmt.Printf("Directory %s contains:\n", target)
   401  
   402  	for _, dir := range dirs {
   403  		fmt.Printf("  %s\n", dir)
   404  	}
   405  }
   406  
   407  func ExampleListAllFiles() {
   408  	target := "/home/john/documents"
   409  
   410  	// List all files including hidden (files with the dot
   411  	// at the beginning of the file name)
   412  	files := ListAllFiles(target, true)
   413  
   414  	if len(files) == 0 {
   415  		fmt.Printf("Directory %s is empty", target)
   416  		return
   417  	}
   418  
   419  	fmt.Printf("Directory %s contains:\n", target)
   420  
   421  	for _, file := range files {
   422  		fmt.Printf("  %s\n", file)
   423  	}
   424  }
   425  
   426  func ExampleListToAbsolute() {
   427  	dir := "/home/john/documents"
   428  
   429  	// List all objects including hidden (files and directries
   430  	// with the dot at the beginning of the file name)
   431  	objects := List(dir, false)
   432  
   433  	if len(objects) == 0 {
   434  		fmt.Printf("Directory %s is empty", dir)
   435  		return
   436  	}
   437  
   438  	// The method adds the path to the beginning of every item in the slice
   439  	ListToAbsolute(dir, objects)
   440  }
   441  
   442  func ExampleListingFilter() {
   443  	dir := "/home/john/documents"
   444  
   445  	filter := ListingFilter{
   446  		MatchPatterns: []string{"*.doc", "*.docx", "*.pdf"},
   447  		MTimeOlder:    time.Now().Unix() - 3600,
   448  		SizeGreater:   50 * 1024,
   449  		Perms:         "FR",
   450  	}
   451  
   452  	docs := List(dir, false, filter)
   453  
   454  	if len(docs) == 0 {
   455  		fmt.Printf("No documents found in %s\n", dir)
   456  		return
   457  	}
   458  
   459  	fmt.Println("Found documents:")
   460  
   461  	for _, doc := range docs {
   462  		fmt.Printf("  %s\n", doc)
   463  	}
   464  }
   465  
   466  func ExamplePush() {
   467  	// Current working directory is the directory where binary was executed
   468  
   469  	cwd := Push("/home/john/documents")
   470  
   471  	// Current working directory set to /home/john/documents
   472  
   473  	cwd = Push("/home/john/documents/work")
   474  
   475  	// Current working directory set to /home/john/documents/work
   476  
   477  	fmt.Println(cwd)
   478  
   479  	cwd = Pop()
   480  
   481  	// Current working directory set to /home/john/documents
   482  
   483  	cwd = Pop()
   484  
   485  	// Current working directory set to initial working directory
   486  }
   487  
   488  func ExamplePop() {
   489  	// Current working directory is the directory where binary was executed
   490  
   491  	cwd := Push("/home/john/documents")
   492  
   493  	// Current working directory set to /home/john/documents
   494  
   495  	cwd = Push("/home/john/documents/work")
   496  
   497  	// Current working directory set to /home/john/documents/work
   498  
   499  	fmt.Println(cwd)
   500  
   501  	cwd = Pop()
   502  
   503  	// Current working directory set to /home/john/documents
   504  
   505  	cwd = Pop()
   506  
   507  	// Current working directory set to initial working directory
   508  }