pkg.re/essentialkaos/ek.10@v12.41.0+incompatible/path/example_test.go (about)

     1  package path
     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  )
    13  
    14  // ////////////////////////////////////////////////////////////////////////////////// //
    15  
    16  func ExampleBase() {
    17  	fmt.Println(Base("/home/user/project"))
    18  
    19  	// Output:
    20  	// project
    21  }
    22  
    23  func ExampleClean() {
    24  	fmt.Println(Clean("/project//abc"))
    25  
    26  	// Output:
    27  	// /project/abc
    28  }
    29  
    30  func ExampleDir() {
    31  	fmt.Println(Dir("/home/user/project"))
    32  
    33  	// Output:
    34  	// /home/user
    35  }
    36  
    37  func ExampleExt() {
    38  	fmt.Println(Ext("/home/user/file.zip"))
    39  
    40  	// Output:
    41  	// .zip
    42  }
    43  
    44  func ExampleIsAbs() {
    45  	fmt.Println(IsAbs("/dev/null"))
    46  
    47  	// Output:
    48  	// true
    49  }
    50  
    51  func ExampleJoin() {
    52  	fmt.Println(Join("home", "user", "project"))
    53  
    54  	// Output:
    55  	// home/user/project
    56  }
    57  
    58  func ExampleMatch() {
    59  	fmt.Println(Match("/home/*", "/home/user"))
    60  
    61  	// Output:
    62  	// true <nil>
    63  }
    64  
    65  func ExampleSplit() {
    66  	fmt.Println(Split("/home/user/file.zip"))
    67  
    68  	// Output:
    69  	// /home/user/ file.zip
    70  }
    71  
    72  func ExampleIsSafe() {
    73  	path1 := "/home/user/project"
    74  	path2 := "/usr/sbin/myapp"
    75  
    76  	fmt.Printf("%s is safe → %t\n", path1, IsSafe(path1))
    77  	fmt.Printf("%s is safe → %t\n", path2, IsSafe(path2))
    78  
    79  	// Output:
    80  	// /home/user/project is safe → true
    81  	// /usr/sbin/myapp is safe → false
    82  }
    83  
    84  func ExampleDirN() {
    85  	path1 := "/home/user/project/config/file.cfg"
    86  	path2 := "/usr/sbin/myapp"
    87  
    88  	fmt.Printf("Config dir: %s\n", DirN(path1, 4))
    89  	fmt.Printf("Bin dir: %s\n", DirN(path2, 2))
    90  
    91  	// Output:
    92  	// Config dir: /home/user/project/config
    93  	// Bin dir: /usr/sbin
    94  }
    95  
    96  func ExampleIsDotfile() {
    97  	file1 := "/home/user/project/file"
    98  	file2 := "/home/user/project/.file"
    99  
   100  	fmt.Printf("%s is dotfile → %t\n", file1, IsDotfile(file1))
   101  	fmt.Printf("%s is dotfile → %t\n", file2, IsDotfile(file2))
   102  
   103  	// Output:
   104  	// /home/user/project/file is dotfile → false
   105  	// /home/user/project/.file is dotfile → true
   106  }
   107  
   108  func ExampleIsGlob() {
   109  	file1 := "file"
   110  	file2 := "*.file"
   111  
   112  	fmt.Printf("%s is glob → %t\n", file1, IsGlob(file1))
   113  	fmt.Printf("%s is glob → %t\n", file2, IsGlob(file2))
   114  
   115  	// Output:
   116  	// file is glob → false
   117  	// *.file is glob → true
   118  }