Skip to content

Commit

Permalink
use filepath.Glob and filepath.Match
Browse files Browse the repository at this point in the history
  • Loading branch information
laverya committed Apr 15, 2020
1 parent 5050f46 commit 2bc9938
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 19 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ require (
github.com/go-openapi/spec v0.19.4 // indirect
github.com/go-redis/redis/v7 v7.2.0
github.com/go-sql-driver/mysql v1.5.0
github.com/gobwas/glob v0.2.3
github.com/golang/snappy v0.0.1 // indirect
github.com/google/gofuzz v1.1.0
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
Expand Down
16 changes: 13 additions & 3 deletions pkg/analyze/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,17 @@ func (f fileContentProvider) getFileContents(fileName string) ([]byte, error) {
}

func (f fileContentProvider) getChildFileContents(dirName string) (map[string][]byte, error) {
// TODO: walk sub-dirs
// return nil, errors.New("not implemented")
return map[string][]byte{}, nil
files, err := filepath.Glob(filepath.Join(f.rootDir, dirName))
if err != nil {
return nil, errors.Wrapf(err, "invalid glob %q", dirName)
}
fileArr := map[string][]byte{}
for _, filePath := range files {
bytes, err := ioutil.ReadFile(filePath)
if err != nil {
return nil, errors.Wrapf(err, "read %q", filePath)
}
fileArr[filePath] = bytes
}
return fileArr, nil
}
9 changes: 2 additions & 7 deletions pkg/analyze/text_analyze_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package analyzer

import (
"fmt"
"path/filepath"
"strings"
"testing"

"github.com/gobwas/glob"
troubleshootv1beta1 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta1"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -314,13 +314,8 @@ func Test_textAnalyze(t *testing.T) {
}
}

g, err := glob.Compile(n)
if err != nil {
return nil, err
}

for k, v := range test.files {
if g.Match(k) {
if ok, _ := filepath.Match(n, k); ok {
matching[k] = v
}
}
Expand Down
10 changes: 2 additions & 8 deletions pkg/preflight/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package preflight

import (
"fmt"
"path/filepath"
"strings"

"github.com/gobwas/glob"
analyze "github.com/replicatedhq/troubleshoot/pkg/analyze"
)

Expand All @@ -26,14 +26,8 @@ func (c CollectResult) Analyze() []*analyze.AnalyzeResult {
}
}

g, err := glob.Compile(prefix)
// don't treat this as a true error as glob is a late addition
if err != nil {
return matching, nil
}

for k, v := range c.AllCollectedData {
if g.Match(k) {
if ok, _ := filepath.Match(prefix, k); ok {
matching[k] = v
}
}
Expand Down

0 comments on commit 2bc9938

Please sign in to comment.