From 4708eea9f3adcd4fcb204ee1badf4f30cbec5d5f Mon Sep 17 00:00:00 2001 From: Simon Emms Date: Thu, 14 Sep 2023 20:32:54 +0000 Subject: [PATCH 1/4] feat(root): create root command --- Dockerfile | 14 ++++++++++++ Makefile | 14 ++++++++++++ cmd/root.go | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++ cmd/version.go | 16 +++++++++++++ go.mod | 15 +++++++++++++ go.sum | 22 ++++++++++++++++++ main.go | 22 ++++++++++++++++++ 7 files changed, 164 insertions(+) create mode 100644 cmd/root.go create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go diff --git a/Dockerfile b/Dockerfile index 0af9fc9..655cad7 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,3 +1,17 @@ +# 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. + FROM golang AS builder ARG GIT_COMMIT ARG GIT_REPO diff --git a/Makefile b/Makefile index 2db9846..9d63ea4 100644 --- a/Makefile +++ b/Makefile @@ -1,3 +1,17 @@ +# 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. + cruft-update: ifeq (,$(wildcard .cruft.json)) @echo "Cruft not configured" diff --git a/cmd/root.go b/cmd/root.go new file mode 100644 index 0000000..541e3b3 --- /dev/null +++ b/cmd/root.go @@ -0,0 +1,61 @@ +/* +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 cmd + +import ( + "fmt" + "os" + + "github.com/mrsimonemms/golang-helpers/logger" + "github.com/mrsimonemms/toodaloo/pkg/config" + "github.com/sirupsen/logrus" + "github.com/spf13/cobra" +) + +var rootCfg struct { + CfgFile string + LogLevel string + WorkingDirectory string +} + +var Config *config.Config + +// rootCmd represents the base command when called without any subcommands +var rootCmd = &cobra.Command{ + Use: "toodaloo", + Short: "Say goodbye to your todos", + PersistentPreRunE: func(cmd *cobra.Command, args []string) error { + return logger.SetLevel(rootCfg.LogLevel) + }, +} + +// Execute adds all child commands to the root command and sets flags appropriately. +// This is called by main.main(). It only needs to happen once to the rootCmd. +func Execute() { + err := rootCmd.Execute() + if err != nil { + os.Exit(1) + } +} + +func init() { + cwd, err := os.Getwd() + cobra.CheckErr(err) + + rootCmd.PersistentFlags().StringVarP(&rootCfg.CfgFile, "config", "c", ".toodaloorc.yaml", "the config file") + rootCmd.PersistentFlags().StringVarP(&rootCfg.WorkingDirectory, "directory", "d", cwd, "working directory") + rootCmd.PersistentFlags().StringVarP(&rootCfg.LogLevel, "log-level", "l", logrus.InfoLevel.String(), fmt.Sprintf("log level: %s", logger.GetAllLevels())) +} diff --git a/cmd/version.go b/cmd/version.go index c2c8a30..3830daf 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -1,3 +1,19 @@ +/* + * 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 cmd import ( diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..5d01f47 --- /dev/null +++ b/go.mod @@ -0,0 +1,15 @@ +module github.com/mrsimonemms/toodaloo + +go 1.20 + +require ( + github.com/mrsimonemms/golang-helpers v0.1.1 + github.com/sirupsen/logrus v1.9.3 + github.com/spf13/cobra v1.7.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 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..d8ae6ec --- /dev/null +++ b/go.sum @@ -0,0 +1,22 @@ +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/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= +github.com/mrsimonemms/golang-helpers v0.1.1/go.mod h1:VrFy6bbkeaE00HHxs8u07ppDvJPUvGb0rxZeYdtuaFU= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= +github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= +github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I= +github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0= +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/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ= +golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go new file mode 100644 index 0000000..862ef8d --- /dev/null +++ b/main.go @@ -0,0 +1,22 @@ +/* +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 main + +import "github.com/mrsimonemms/toodaloo/cmd" + +func main() { + cmd.Execute() +} From 0d3ce2f577d65a15b6f4ba184d781c60b1845600 Mon Sep 17 00:00:00 2001 From: Simon Emms Date: Sat, 16 Sep 2023 14:36:47 +0000 Subject: [PATCH 2/4] feat(init): initialize the config file --- .toodaloorc.yaml | 5 +++++ cmd/init.go | 34 +++++++++++++++++++++++++++++++++ go.mod | 2 ++ go.sum | 4 ++++ pkg/config/config.go | 45 ++++++++++++++++++++++++++++++++++++++++++++ pkg/config/types.go | 33 ++++++++++++++++++++++++++++++++ 6 files changed, 123 insertions(+) create mode 100644 .toodaloorc.yaml create mode 100644 cmd/init.go create mode 100644 pkg/config/config.go create mode 100644 pkg/config/types.go diff --git a/.toodaloorc.yaml b/.toodaloorc.yaml new file mode 100644 index 0000000..e4b3a68 --- /dev/null +++ b/.toodaloorc.yaml @@ -0,0 +1,5 @@ +output: toodaloo.yaml +tags: +- fixme +- todo +- '@todo' diff --git a/cmd/init.go b/cmd/init.go new file mode 100644 index 0000000..f06a25a --- /dev/null +++ b/cmd/init.go @@ -0,0 +1,34 @@ +/* +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 cmd + +import ( + "github.com/mrsimonemms/toodaloo/pkg/config" + "github.com/spf13/cobra" +) + +// initCmd represents the init command +var initCmd = &cobra.Command{ + Use: "init", + Short: "Initialize a config file", + RunE: func(cmd *cobra.Command, args []string) error { + return config.Init(rootCfg.WorkingDirectory, rootCfg.CfgFile) + }, +} + +func init() { + rootCmd.AddCommand(initCmd) +} diff --git a/go.mod b/go.mod index 5d01f47..9dd8ad2 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,8 @@ require ( github.com/mrsimonemms/golang-helpers v0.1.1 github.com/sirupsen/logrus v1.9.3 github.com/spf13/cobra v1.7.0 + gopkg.in/yaml.v2 v2.4.0 + sigs.k8s.io/yaml v1.3.0 ) require ( diff --git a/go.sum b/go.sum index d8ae6ec..2db37cd 100644 --- a/go.sum +++ b/go.sum @@ -18,5 +18,9 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ 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= 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= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +sigs.k8s.io/yaml v1.3.0 h1:a2VclLzOGrwOHDiV8EfBGhvjHvP46CtW5j6POvhYGGo= +sigs.k8s.io/yaml v1.3.0/go.mod h1:GeOyir5tyXNByN85N/dRIT9es5UQNerPYEKK56eTBm8= diff --git a/pkg/config/config.go b/pkg/config/config.go new file mode 100644 index 0000000..9923461 --- /dev/null +++ b/pkg/config/config.go @@ -0,0 +1,45 @@ +/* + * 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 config + +import ( + "os" + "path/filepath" + + "github.com/mrsimonemms/golang-helpers/logger" + "sigs.k8s.io/yaml" +) + +func Init(directory, filename string) error { + file := filepath.Join(directory, filename) + l := logger.Log().WithField("filename", file) + + c, err := yaml.Marshal(defaultConfig) + if err != nil { + l.Error("Failed to generate default config") + return err + } + + l.Info("Saving config to file") + + if err := os.WriteFile(file, c, 0o644); err != nil { + l.WithError(err).Error("Failed to save file") + return err + } + + return nil +} diff --git a/pkg/config/types.go b/pkg/config/types.go new file mode 100644 index 0000000..0f7b64d --- /dev/null +++ b/pkg/config/types.go @@ -0,0 +1,33 @@ +/* + * 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 config + +type Config struct { + IgnorePaths []string `json:"ignore_paths,omitempty"` + Output string `json:"output"` + Tags []string `json:"tags"` +} + +var defaultConfig = Config{ + IgnorePaths: []string{}, + Output: "toodaloo.yaml", + Tags: []string{ + "fixme", + "todo", + "@todo", + }, +} From 3f439d9b9860706f655fab080f606ddca48aa725 Mon Sep 17 00:00:00 2001 From: Simon Emms Date: Sat, 16 Sep 2023 17:20:44 +0100 Subject: [PATCH 3/4] feat(scan): scaffold the scan command --- cmd/scan.go | 45 ++++++++++++++++++++++++++++++++++++++++++++ pkg/config/config.go | 26 +++++++++++++++++++++++++ pkg/config/types.go | 2 ++ 3 files changed, 73 insertions(+) create mode 100644 cmd/scan.go diff --git a/cmd/scan.go b/cmd/scan.go new file mode 100644 index 0000000..328b8a0 --- /dev/null +++ b/cmd/scan.go @@ -0,0 +1,45 @@ +/* + * 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 cmd + +import ( + "fmt" + + "github.com/mrsimonemms/golang-helpers/logger" + "github.com/mrsimonemms/toodaloo/pkg/config" + "github.com/spf13/cobra" +) + +// scanCmd represents the scan command +var scanCmd = &cobra.Command{ + Use: "scan", + Short: "Scan a project", + RunE: func(cmd *cobra.Command, args []string) error { + cfg, err := config.NewConfigFromFile(rootCfg.WorkingDirectory, rootCfg.CfgFile) + if err != nil { + logger.Log().WithError(err).Error("Error reading config") + return err + } + + fmt.Println(cfg) + + return nil + }, +} + +func init() { + rootCmd.AddCommand(scanCmd) +} diff --git a/pkg/config/config.go b/pkg/config/config.go index 9923461..cb2bcd8 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -43,3 +43,29 @@ func Init(directory, filename string) error { return nil } + +func NewConfigFromFile(directory, filename string) (*Config, error) { + file := filepath.Join(directory, filename) + l := logger.Log().WithField("filename", file) + + l.Infof("Loading configuration from file") + + bytes, err := os.ReadFile(filename) + if err != nil && !os.IsNotExist(err) { + return nil, err + } + + if os.IsNotExist(err) { + l.Info("Config file does not exist, using the default config") + + return &defaultConfig, nil + } + + // Start from the default config + config := defaultConfig + if err := yaml.Unmarshal(bytes, &config); err != nil { + return nil, err + } + + return &config, nil +} diff --git a/pkg/config/types.go b/pkg/config/types.go index 0f7b64d..d938353 100644 --- a/pkg/config/types.go +++ b/pkg/config/types.go @@ -17,12 +17,14 @@ package config type Config struct { + Glob string `json:"glob"` IgnorePaths []string `json:"ignore_paths,omitempty"` Output string `json:"output"` Tags []string `json:"tags"` } var defaultConfig = Config{ + Glob: "**/*", IgnorePaths: []string{}, Output: "toodaloo.yaml", Tags: []string{ From 8fbf8733a3ae3a87fbe387acb245e45dda620533 Mon Sep 17 00:00:00 2001 From: Simon Emms Date: Mon, 18 Sep 2023 21:06:56 +0000 Subject: [PATCH 4/4] 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 +}