From b6b921f9294ff502f78d354d25ef4ff89f6d7515 Mon Sep 17 00:00:00 2001 From: Simon Emms Date: Mon, 18 Sep 2023 21:06:56 +0000 Subject: [PATCH] feat(scan): get list of files to scan --- cmd/scan.go | 11 +++--- go.mod | 5 ++- go.sum | 8 +++++ pkg/config/types.go | 9 +++-- pkg/scanner/scan.go | 28 ++++++++++++++++ pkg/scanner/types.go | 80 ++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 133 insertions(+), 8 deletions(-) create mode 100644 pkg/scanner/scan.go create mode 100644 pkg/scanner/types.go diff --git a/cmd/scan.go b/cmd/scan.go index 328b8a0..d80ffe4 100644 --- a/cmd/scan.go +++ b/cmd/scan.go @@ -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" ) @@ -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() }, } diff --git a/go.mod b/go.mod index 9dd8ad2..584b703 100644 --- a/go.mod +++ b/go.mod @@ -3,9 +3,12 @@ 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 ) @@ -13,5 +16,5 @@ require ( 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 ) diff --git a/go.sum b/go.sum index 2db37cd..61944f1 100644 --- a/go.sum +++ b/go.sum @@ -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= @@ -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= diff --git a/pkg/config/types.go b/pkg/config/types.go index d938353..e84c278 100644 --- a/pkg/config/types.go +++ b/pkg/config/types.go @@ -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", diff --git a/pkg/scanner/scan.go b/pkg/scanner/scan.go new file mode 100644 index 0000000..bc7fa07 --- /dev/null +++ b/pkg/scanner/scan.go @@ -0,0 +1,28 @@ +/* + * Copyright 2023 Simon Emms + * + * 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 +} diff --git a/pkg/scanner/types.go b/pkg/scanner/types.go new file mode 100644 index 0000000..b6813fa --- /dev/null +++ b/pkg/scanner/types.go @@ -0,0 +1,80 @@ +/* + * Copyright 2023 Simon Emms + * + * 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 +}