J
J
jajabin2019-06-02 20:56:08
linux
jajabin, 2019-06-02 20:56:08

How to get absolute paths to files in a range of directories?

I implemented a log parser on GO, at the moment the program accepts a path parameter in this format, example /var/log/2019/2019-05/2019-05-27/ , parses only the current folder and returns full paths to files, I would like to know but how to implement a file path parser in such a form as --datastart /var/log/2019/2019-05/2019-05-01/ --dataend /var/log/2019/2019-05/2019-05- 31/ , in order to parse a certain range of dates, which are divided into folders.
My first option implementation function:

func WalkFile(done <- chan struct{},root string)(<-chan string,<-chan error){

  paths := make(chan string)
  errc  := make(chan error,1)

  go func() {

    defer close(paths)
    errc <- filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
      if err != nil{
        return err
      }
      if !info.Mode().IsRegular(){
        return nil

      }

      select {
      case paths <- path:
      case <-done:
        return nil

        
      }
      return nil
    })

  }()

  return paths,errc
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
rustler2000, 2019-06-03
@rustler2000

No one will parse and filter for you. This is your logic.
But recursively bypassing gets in the way - https://github.com/golang/go/issues/11862.
They also recommend https://github.com/bmatcuk/doublestar - I use it myself. Or manual recursion.

A
Andrey Tsvetkov, 2019-06-03
@yellow79

Alternatively, you can pass a list, not a flag, but arguments, something like
Linux will independently find everything that satisfies the conditions and pass it to your application

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question