Skip to content

Commit

Permalink
feat(scan): get list of files to scan
Browse files Browse the repository at this point in the history
  • Loading branch information
mrsimonemms committed Sep 18, 2023
1 parent 3f439d9 commit 56ca281
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 7 deletions.
11 changes: 7 additions & 4 deletions cmd/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@
package cmd

import (
"fmt"

"github.com/mrsimonemms/golang-helpers/logger"
"github.com/mrsimonemms/toodaloo/pkg/config"
"github.com/mrsimonemms/toodaloo/pkg/scanner"
"github.com/spf13/cobra"
)

Expand All @@ -34,9 +33,13 @@ var scanCmd = &cobra.Command{
return err
}

fmt.Println(cfg)
s, err := scanner.New(rootCfg.WorkingDirectory, cfg)
if err != nil {
logger.Log().WithError(err).Error("Unable to load scanner")
return err
}

return nil
return s.Exec()
},
}

Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/mrsimonemms/toodaloo
go 1.20

require (
github.com/google/uuid v1.3.1
github.com/mrsimonemms/golang-helpers v0.1.1
github.com/sirupsen/logrus v1.9.3
github.com/spf13/cobra v1.7.0
Expand All @@ -11,6 +12,7 @@ require (
)

require (
github.com/bmatcuk/doublestar/v4 v4.6.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
github.com/bmatcuk/doublestar/v4 v4.6.0 h1:HTuxyug8GyFbRkrffIpzNCSK4luc0TY3wzXvzIZhEXc=
github.com/bmatcuk/doublestar/v4 v4.6.0/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4=
github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/mrsimonemms/golang-helpers v0.1.1 h1:p8uvDTWd9v861xDy2OJdU22iECpgbyyQKMmQqjAH3Zs=
Expand Down
9 changes: 6 additions & 3 deletions pkg/config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ type Config struct {
}

var defaultConfig = Config{
Glob: "**/*",
IgnorePaths: []string{},
Output: "toodaloo.yaml",
Glob: "**/*",
IgnorePaths: []string{
".git",
".git/**/*",
},
Output: "toodaloo.yaml",
Tags: []string{
"fixme",
"todo",
Expand Down
28 changes: 28 additions & 0 deletions pkg/scanner/scan.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2023 Simon Emms <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package scanner

import (
"github.com/mrsimonemms/toodaloo/pkg/config"
)

func New(workingDirectory string, cfg *config.Config) (*Scan, error) {
return &Scan{
config: cfg,
workingDirectory: workingDirectory,
}, nil
}
80 changes: 80 additions & 0 deletions pkg/scanner/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright 2023 Simon Emms <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package scanner

import (
"os"
"slices"

Check failure on line 21 in pkg/scanner/types.go

View workflow job for this annotation

GitHub Actions / test

package slices is not in GOROOT (/opt/hostedtoolcache/go/1.20.8/x64/src/slices)

"github.com/bmatcuk/doublestar/v4"
"github.com/mrsimonemms/golang-helpers/logger"
"github.com/mrsimonemms/toodaloo/pkg/config"
)

type Scan struct {
config *config.Config
workingDirectory string
}

func (s *Scan) getListOfFiles() ([]string, error) {
fileList := make([]string, 0)
ignoreList := make([]string, 0)

fsys := os.DirFS(s.workingDirectory)
matches, err := doublestar.Glob(fsys, s.config.Glob)
if err != nil {
return nil, err
}

for _, i := range s.config.IgnorePaths {
m, err := doublestar.Glob(fsys, i)
if err != nil {
return nil, err
}

ignoreList = append(ignoreList, m...)
}

for _, f := range matches {
if !slices.Contains(ignoreList, f) {
fileInfo, err := os.Stat(f)
if err != nil {
return nil, err
}

// Check path isn't a directory
if !fileInfo.IsDir() {
fileList = append(fileList, f)
}
}
}

return fileList, nil
}

func (s *Scan) Exec() error {
files, err := s.getListOfFiles()
if err != nil {
logger.Log().WithError(err).Error("Failed to get list of files")
return err
}

logger.Log().WithField("files", files).Debug("File list")
logger.Log().WithField("file-count", len(files)).Info("Files found")

return nil
}

0 comments on commit 56ca281

Please sign in to comment.