github.com/scottcagno/storage@v1.8.0/cmd/filesystem/main.go (about)

     1  package main
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"github.com/scottcagno/storage/pkg/filesystem"
     7  	"io/fs"
     8  	"log"
     9  	"os"
    10  	"path/filepath"
    11  	"regexp"
    12  	"strings"
    13  )
    14  
    15  func main() {
    16  
    17  	fmt.Printf("Running List... [skip printing directories, and hidden files]\n")
    18  	if err := filesystem.List(".", func(f fs.FileInfo) error {
    19  		// only print files, not directories
    20  		if f.IsDir() {
    21  			return fs.SkipDir
    22  		}
    23  		// only print files that are not "hidden"
    24  		if !strings.HasPrefix(f.Name(), ".") {
    25  			fmt.Printf("%s\n", f.Name())
    26  		}
    27  		return nil
    28  	}); err != nil {
    29  		log.Printf("List: %s\n", err)
    30  	}
    31  
    32  	fmt.Printf("\n\nRunning Walk... [only list contents of the base files and lsmtree]\n")
    33  	if err := filesystem.Walk(".", func(f fs.FileInfo) error {
    34  		// only walk a few dirs, skipping all others
    35  		if f.IsDir() && f.Name() != "." && f.Name() != "pkg" && f.Name() != "lsmtree" {
    36  			return fs.SkipDir
    37  		}
    38  		fmt.Printf("%s (dir=%v)\n", f.Name(), f.IsDir())
    39  		return nil
    40  	}); err != nil {
    41  		log.Printf("List: %s\n", err)
    42  	}
    43  
    44  	//fmt.Printf("Running cat...\n")
    45  	//runCat()
    46  	//
    47  	//fmt.Printf("\rRunning ls...\n")
    48  	//runLS()
    49  	//
    50  	//fmt.Printf("\rRunning grep...\n")
    51  	//runGrep()
    52  }
    53  
    54  func testWalk(pattern, path string) {
    55  	// compile regex pattern
    56  	reg := regexp.MustCompile(pattern)
    57  	// "clean" path
    58  	path = filepath.ToSlash(path)
    59  	// start walking
    60  	err := filepath.Walk(path,
    61  		func(path string, info os.FileInfo, err error) error {
    62  			if err != nil {
    63  				return err
    64  			}
    65  			// check for match
    66  			foundMatch := reg.MatchString(path)
    67  			if foundMatch {
    68  				fmt.Fprintf(os.Stdout, "path: %q, size: %d\n", path, info.Size())
    69  			}
    70  			return nil
    71  		})
    72  	if err != nil {
    73  		log.Println(err)
    74  	}
    75  }
    76  
    77  func runCat() {
    78  	br := bytes.NewReader([]byte(`this is a test, this is only a test.\nplease and thank you\n`))
    79  	filesystem.Cat(os.Stdout, br)
    80  }
    81  
    82  func runLS() {
    83  	filesystem.LS("pkg/lsmtree")
    84  }
    85  
    86  func runGrep() {
    87  	// roughly equivalent to: grep --color=never -En "(maxKeySizeAllowed|commitLog)" pkg/lsmtree/*.go
    88  	filesystem.Grep(`(maxKeySizeAllowed|commitLog)`, "pkg/lsmtree/*.go")
    89  }