diff --git a/Makefile b/Makefile index 3834e19..90011eb 100644 --- a/Makefile +++ b/Makefile @@ -27,7 +27,7 @@ init: clean: /bin/rm -rf build/ /bin/rm -rf macadmins_extension - /bin/rm -rf ${PKGDIR_TMP}_darwin + /bin/rm -rf ${PKGDIR_TMP}* /bin/rm -f macadmins_extension.zip build: .pre-build diff --git a/tables/fileline/file_line.go b/tables/fileline/file_line.go index ee2c22d..4337054 100644 --- a/tables/fileline/file_line.go +++ b/tables/fileline/file_line.go @@ -27,55 +27,67 @@ func FileLineColumns() []table.ColumnDefinition { func FileLineGenerate(ctx context.Context, queryContext table.QueryContext) ([]map[string]string, error) { path := "" + wildcard := false if constraintList, present := queryContext.Constraints["path"]; present { // 'path' is in the where clause for _, constraint := range constraintList.Constraints { + // LIKE + if constraint.Operator == table.OperatorLike { + path = constraint.Expression + wildcard = true + } + // = if constraint.Operator == table.OperatorEquals { path = constraint.Expression + wildcard = false } } } - - output, err := processFile(path) + var results []map[string]string + output, err := processFile(path, wildcard) if err != nil { - return nil, err + return results, err } - return output, nil + for _, item := range output { + results = append(results, map[string]string{ + "line": item.Line, + "path": item.Path, + }) + } + + return results, nil } -func processFile(path string) ([]map[string]string, error) { +func processFile(path string, wildcard bool) ([]FileLine, error) { - var output []map[string]string + var output []FileLine - // Replace % for * for glob - replacedPath := strings.ReplaceAll(path, "%", "*") + if wildcard { + replacedPath := strings.ReplaceAll(path, "%", "*") - files, err := filepath.Glob(replacedPath) - if err != nil { - return nil, err - } - - for _, file := range files { - // get slice of lines - lines, _ := readLines(file) + files, err := filepath.Glob(replacedPath) + if err != nil { + return nil, err + } + for _, file := range files { + lines, _ := readLines(file) + output = append(output, lines...) - for _, line := range lines { - output = append(output, map[string]string{ - "line": line, - "path": file, - }) } + } else { + lines, _ := readLines(path) + output = append(output, lines...) } return output, nil } -func readLines(path string) ([]string, error) { - var output []string - fmt.Println(path) +func readLines(path string) ([]FileLine, error) { + var output []FileLine + if !fileExists(path) { err := errors.New("File does not exist") return nil, err @@ -87,12 +99,16 @@ func readLines(path string) ([]string, error) { defer file.Close() scanner := bufio.NewScanner(file) + scanner.Split(bufio.ScanLines) + for scanner.Scan() { - output = append(output, scanner.Text()) + line := scanner.Text() + item := FileLine{Path: path, Line: line} + output = append(output, item) } - if err := scanner.Err(); err != nil { - return nil, err + if scanner.Err() != nil { + fmt.Printf("error: %s\n", scanner.Err()) } return output, nil