Skip to content

Commit

Permalink
feat(reports): create default reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
mrsimonemms committed Sep 23, 2023
1 parent bb1d4c5 commit 2eba5fd
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 20 deletions.
26 changes: 25 additions & 1 deletion cmd/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,31 @@
package cmd

import (
"fmt"

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

var scanOpts struct {
Output string
ReportType string
}

// scanCmd represents the scan command
var scanCmd = &cobra.Command{
Use: "scan",
Short: "Scan a project",
RunE: func(cmd *cobra.Command, args []string) error {
report, ok := reports.Reports[scanOpts.ReportType]
if !ok {
logger.Log().WithField("report", scanOpts.ReportType).Fatal("Unknown report type")
return fmt.Errorf("unknown report type: %s", scanOpts.ReportType)
}

cfg, err := config.NewConfigFromFile(rootCfg.WorkingDirectory, rootCfg.CfgFile)
if err != nil {
logger.Log().WithError(err).Error("Error reading config")
Expand All @@ -39,10 +53,20 @@ var scanCmd = &cobra.Command{
return err
}

return s.Exec()
result, err := s.Exec()
if err != nil {
logger.Log().WithError(err).Error("Unable to scan files")
return err
}

logger.Log().WithField("report-type", scanOpts.ReportType).Info("Generating report")
return reports.Generate(scanOpts.Output, result, report)
},
}

func init() {
rootCmd.AddCommand(scanCmd)

scanCmd.Flags().StringVarP(&scanOpts.Output, "output", "o", ".toodaloo.yaml", "toodaloo report path")
scanCmd.Flags().StringVarP(&scanOpts.ReportType, "report-type", "r", "toodaloo", "report type")
}
40 changes: 40 additions & 0 deletions pkg/reports/reports.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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 reports

import (
"os"

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

var Reports map[string]Report = make(map[string]Report, 0)

type Report interface {
Generate([]scanner.ScanResult) ([]byte, error)
}

func Generate(filepath string, result []scanner.ScanResult, report Report) error {
data, err := report.Generate(result)
if err != nil {
return err
}

logger.Log().WithField("file", filepath).Info("Writing report to file")
return os.WriteFile(filepath, data, 0o644)
}
34 changes: 34 additions & 0 deletions pkg/reports/toodaloo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* 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 reports

import (
"github.com/mrsimonemms/toodaloo/pkg/scanner"
"sigs.k8s.io/yaml"
)

type ToodalooReport struct{}

// This is a basic report, just dumping the report to YAML
func (t ToodalooReport) Generate(res []scanner.ScanResult) ([]byte, error) {
return yaml.Marshal(res)
}

func init() {
// Register the report type
Reports["toodaloo"] = ToodalooReport{}
}
7 changes: 0 additions & 7 deletions pkg/scanner/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (

"github.com/mrsimonemms/toodaloo/pkg/config"
"github.com/sirupsen/logrus"
"sigs.k8s.io/yaml"
)

func New(workingDirectory string, cfg *config.Config) (*Scan, error) {
Expand Down Expand Up @@ -85,12 +84,6 @@ func scanForTodos(l *logrus.Entry, filename string, tags []string) ([]ScanResult
line++
}

e, err := yaml.Marshal(res)
if err != nil {
return nil, err
}
fmt.Printf("%s\n", e)

l.Debug("Scan ending")
return res, err
}
21 changes: 9 additions & 12 deletions pkg/scanner/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package scanner

import (
"fmt"
"os"
"path"

Expand Down Expand Up @@ -81,13 +80,12 @@ func (s *Scan) getListOfFiles() ([]string, error) {
return fileList, nil
}

func (s *Scan) scanFilesForTodos(files []string) error {
func (s *Scan) scanFilesForTodos(files []string) ([]ScanResult, error) {
g := new(errgroup.Group)

res := make([]ScanResult, 0)
logger.Log().WithField("tags", s.config.Tags).Debug("Scanning files for todos")
for _, file := range files {

file := file
l := logger.Log().WithField("file", file)

Expand All @@ -104,28 +102,27 @@ func (s *Scan) scanFilesForTodos(files []string) error {
}

if err := g.Wait(); err != nil {
return err
return nil, err
}

fmt.Printf("%+v\n", res)

logger.Log().Debug("File scan finished")
return nil
return res, nil
}

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

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

if err := s.scanFilesForTodos(files); err != nil {
result, err := s.scanFilesForTodos(files)
if err != nil {
logger.Log().WithError(err).Error("Error scanning files")
return err
return nil, err
}
return nil
return result, nil
}

0 comments on commit 2eba5fd

Please sign in to comment.