generated from mrsimonemms/new
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(scan): get list of files to scan
- Loading branch information
1 parent
4fbf6b3
commit d860db5
Showing
6 changed files
with
133 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |