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 4fbf6b3 commit d860db5
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 8 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
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@ module github.com/mrsimonemms/toodaloo
go 1.20

require (
github.com/bmatcuk/doublestar/v4 v4.6.0
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
golang.org/x/exp v0.0.0-20230905200255-921286631fa9
gopkg.in/yaml.v2 v2.4.0
sigs.k8s.io/yaml v1.3.0
)

require (
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
golang.org/x/sys v0.12.0 // indirect
)
8 changes: 8 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 All @@ -15,8 +19,12 @@ github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g=
golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
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"

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

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 d860db5

Please sign in to comment.