2021-02-06 00:36:43 +05:30
|
|
|
package offlinehttp
|
|
|
|
|
|
|
|
|
|
import (
|
2022-05-08 08:52:21 +02:00
|
|
|
"io/fs"
|
2021-02-06 00:36:43 +05:30
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// getInputPaths parses the specified input paths and returns a compiled
|
|
|
|
|
// list of finished absolute paths to the files evaluating any allowlist, denylist,
|
|
|
|
|
// glob, file or folders, etc.
|
2021-10-01 14:30:04 +03:00
|
|
|
func (request *Request) getInputPaths(target string, callback func(string)) error {
|
2021-02-06 00:36:43 +05:30
|
|
|
processed := make(map[string]struct{})
|
|
|
|
|
|
|
|
|
|
// Template input includes a wildcard
|
|
|
|
|
if strings.Contains(target, "*") {
|
2021-10-01 14:30:04 +03:00
|
|
|
if err := request.findGlobPathMatches(target, processed, callback); err != nil {
|
2021-02-06 00:36:43 +05:30
|
|
|
return errors.Wrap(err, "could not find glob matches")
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Template input is either a file or a directory
|
2021-10-01 14:30:04 +03:00
|
|
|
file, err := request.findFileMatches(target, processed, callback)
|
2021-02-06 00:36:43 +05:30
|
|
|
if err != nil {
|
|
|
|
|
return errors.Wrap(err, "could not find file")
|
|
|
|
|
}
|
|
|
|
|
if file {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Recursively walk down the Templates directory and run all
|
|
|
|
|
// the template file checks
|
2021-10-01 14:30:04 +03:00
|
|
|
if err := request.findDirectoryMatches(target, processed, callback); err != nil {
|
2021-02-06 00:36:43 +05:30
|
|
|
return errors.Wrap(err, "could not find directory matches")
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// findGlobPathMatches returns the matched files from a glob path
|
2021-10-01 14:30:04 +03:00
|
|
|
func (request *Request) findGlobPathMatches(absPath string, processed map[string]struct{}, callback func(string)) error {
|
2021-02-06 00:36:43 +05:30
|
|
|
matches, err := filepath.Glob(absPath)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return errors.Errorf("wildcard found, but unable to glob: %s\n", err)
|
|
|
|
|
}
|
|
|
|
|
for _, match := range matches {
|
2021-08-23 14:53:37 +03:00
|
|
|
if filepath.Ext(match) != ".txt" {
|
2021-02-06 00:36:43 +05:30
|
|
|
continue // only process .txt files
|
|
|
|
|
}
|
|
|
|
|
if _, ok := processed[match]; !ok {
|
|
|
|
|
processed[match] = struct{}{}
|
|
|
|
|
callback(match)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// findFileMatches finds if a path is an absolute file. If the path
|
|
|
|
|
// is a file, it returns true otherwise false with no errors.
|
2021-10-01 14:30:04 +03:00
|
|
|
func (request *Request) findFileMatches(absPath string, processed map[string]struct{}, callback func(string)) (bool, error) {
|
2021-02-06 00:36:43 +05:30
|
|
|
info, err := os.Stat(absPath)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return false, err
|
|
|
|
|
}
|
|
|
|
|
if !info.Mode().IsRegular() {
|
|
|
|
|
return false, nil
|
|
|
|
|
}
|
2021-08-23 14:53:37 +03:00
|
|
|
if filepath.Ext(absPath) != ".txt" {
|
2021-02-06 00:36:43 +05:30
|
|
|
return false, nil // only process .txt files
|
|
|
|
|
}
|
|
|
|
|
if _, ok := processed[absPath]; !ok {
|
|
|
|
|
processed[absPath] = struct{}{}
|
|
|
|
|
callback(absPath)
|
|
|
|
|
}
|
|
|
|
|
return true, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// findDirectoryMatches finds matches for templates from a directory
|
2021-10-01 14:30:04 +03:00
|
|
|
func (request *Request) findDirectoryMatches(absPath string, processed map[string]struct{}, callback func(string)) error {
|
2022-05-08 08:52:21 +02:00
|
|
|
err := filepath.WalkDir(
|
|
|
|
|
absPath,
|
|
|
|
|
func(p string, d fs.DirEntry, err error) error {
|
|
|
|
|
// continue on errors
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2021-02-06 00:36:43 +05:30
|
|
|
if d.IsDir() {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2021-08-23 14:53:37 +03:00
|
|
|
if filepath.Ext(p) != ".txt" {
|
2021-02-06 00:36:43 +05:30
|
|
|
return nil // only process .txt files
|
|
|
|
|
}
|
|
|
|
|
if _, ok := processed[p]; !ok {
|
|
|
|
|
callback(p)
|
|
|
|
|
processed[p] = struct{}{}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
},
|
2022-05-08 08:52:21 +02:00
|
|
|
)
|
2021-02-06 00:36:43 +05:30
|
|
|
return err
|
|
|
|
|
}
|