1
0
mirror of https://github.com/strongdm/comply synced 2024-11-24 16:44:53 +00:00
comply/internal/path/path.go

56 lines
1.3 KiB
Go
Raw Normal View History

2018-05-15 21:13:11 +00:00
package path
import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/pkg/errors"
)
// File wraps an os.FileInfo as well as the absolute path to the underlying file.
type File struct {
FullPath string
Info os.FileInfo
}
// Standards lists all standard files.
2021-10-12 10:14:30 +00:00
var Standards = func() ([]File, error) {
2018-05-15 21:13:11 +00:00
return filesFor("standards", "yml")
}
// Narratives lists all narrative files.
2021-10-12 10:14:30 +00:00
var Narratives = func() ([]File, error) {
2018-05-15 21:13:11 +00:00
return filesFor("narratives", "md")
}
// Policies lists all policy files.
2021-10-12 10:14:30 +00:00
var Policies = func() ([]File, error) {
2018-05-15 21:13:11 +00:00
return filesFor("policies", "md")
}
// Procedures lists all procedure files.
2021-10-12 10:14:30 +00:00
var Procedures = func() ([]File, error) {
2018-05-15 21:13:11 +00:00
return filesFor("procedures", "md")
}
func filesFor(name, extension string) ([]File, error) {
var filtered []File
files, err := ioutil.ReadDir(filepath.Join(".", name))
if err != nil {
return nil, errors.Wrap(err, "unable to load files for: "+name)
}
for _, f := range files {
if !strings.HasSuffix(f.Name(), "."+extension) || strings.HasPrefix(strings.ToUpper(f.Name()), "README") {
continue
}
abs, err := filepath.Abs(filepath.Join(".", name, f.Name()))
if err != nil {
return nil, errors.Wrap(err, "unable to load file: "+f.Name())
}
filtered = append(filtered, File{abs, f})
}
return filtered, nil
}