2022-08-10 23:35:58 +05:30
|
|
|
package disk
|
2020-12-29 18:02:45 +05:30
|
|
|
|
|
|
|
|
import (
|
2022-05-08 08:52:21 +02:00
|
|
|
"io/fs"
|
2020-12-29 18:02:45 +05:30
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// GetTemplatesPath returns a list of absolute paths for the provided template list.
|
2022-12-21 22:48:43 +05:30
|
|
|
func (c *DiskCatalog) GetTemplatesPath(definitions []string) ([]string, map[string]error) {
|
2020-12-29 18:02:45 +05:30
|
|
|
// keeps track of processed dirs and files
|
|
|
|
|
processed := make(map[string]bool)
|
|
|
|
|
allTemplates := []string{}
|
2022-12-21 22:48:43 +05:30
|
|
|
erred := make(map[string]error)
|
2020-12-29 18:02:45 +05:30
|
|
|
|
|
|
|
|
for _, t := range definitions {
|
2022-01-12 18:33:17 +05:30
|
|
|
if strings.HasPrefix(t, "http") && (strings.HasSuffix(t, ".yaml") || strings.HasSuffix(t, ".yml")) {
|
|
|
|
|
if _, ok := processed[t]; !ok {
|
|
|
|
|
processed[t] = true
|
|
|
|
|
allTemplates = append(allTemplates, t)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
paths, err := c.GetTemplatePath(t)
|
|
|
|
|
if err != nil {
|
2022-12-21 22:48:43 +05:30
|
|
|
erred[t] = err
|
2022-01-12 18:33:17 +05:30
|
|
|
}
|
|
|
|
|
for _, path := range paths {
|
|
|
|
|
if _, ok := processed[path]; !ok {
|
|
|
|
|
processed[path] = true
|
|
|
|
|
allTemplates = append(allTemplates, path)
|
|
|
|
|
}
|
2020-12-29 18:02:45 +05:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-12-21 22:48:43 +05:30
|
|
|
return allTemplates, erred
|
2020-12-29 18:02:45 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetTemplatePath parses the specified input template path and returns a compiled
|
|
|
|
|
// list of finished absolute paths to the templates evaluating any glob patterns
|
|
|
|
|
// or folders provided as in.
|
2022-08-10 23:35:58 +05:30
|
|
|
func (c *DiskCatalog) GetTemplatePath(target string) ([]string, error) {
|
2020-12-29 18:02:45 +05:30
|
|
|
processed := make(map[string]struct{})
|
|
|
|
|
|
|
|
|
|
absPath, err := c.convertPathToAbsolute(target)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, errors.Wrapf(err, "could not find template file")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Template input includes a wildcard
|
|
|
|
|
if strings.Contains(absPath, "*") {
|
2021-02-26 13:13:11 +05:30
|
|
|
matches, findErr := c.findGlobPathMatches(absPath, processed)
|
|
|
|
|
if findErr != nil {
|
|
|
|
|
return nil, errors.Wrap(findErr, "could not find glob matches")
|
2020-12-29 18:02:45 +05:30
|
|
|
}
|
|
|
|
|
if len(matches) == 0 {
|
|
|
|
|
return nil, errors.Errorf("no templates found for path")
|
|
|
|
|
}
|
|
|
|
|
return matches, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Template input is either a file or a directory
|
|
|
|
|
match, file, err := c.findFileMatches(absPath, processed)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, errors.Wrap(err, "could not find file")
|
|
|
|
|
}
|
|
|
|
|
if file {
|
|
|
|
|
if match != "" {
|
|
|
|
|
return []string{match}, nil
|
|
|
|
|
}
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Recursively walk down the Templates directory and run all
|
|
|
|
|
// the template file checks
|
|
|
|
|
matches, err := c.findDirectoryMatches(absPath, processed)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, errors.Wrap(err, "could not find directory matches")
|
|
|
|
|
}
|
|
|
|
|
if len(matches) == 0 {
|
2022-04-18 17:21:33 -05:00
|
|
|
return nil, errors.Errorf("no templates found in path %s", absPath)
|
2020-12-29 18:02:45 +05:30
|
|
|
}
|
|
|
|
|
return matches, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// convertPathToAbsolute resolves the paths provided to absolute paths
|
2021-11-25 18:54:16 +02:00
|
|
|
// before doing any operations on them regardless of them being BLOB, folders, files, etc.
|
2022-08-10 23:35:58 +05:30
|
|
|
func (c *DiskCatalog) convertPathToAbsolute(t string) (string, error) {
|
2020-12-29 18:02:45 +05:30
|
|
|
if strings.Contains(t, "*") {
|
2021-08-23 14:53:37 +03:00
|
|
|
file := filepath.Base(t)
|
|
|
|
|
absPath, err := c.ResolvePath(filepath.Dir(t), "")
|
2020-12-29 18:02:45 +05:30
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
2021-08-23 14:53:37 +03:00
|
|
|
return filepath.Join(absPath, file), nil
|
2020-12-29 18:02:45 +05:30
|
|
|
}
|
|
|
|
|
return c.ResolvePath(t, "")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// findGlobPathMatches returns the matched files from a glob path
|
2022-08-10 23:35:58 +05:30
|
|
|
func (c *DiskCatalog) findGlobPathMatches(absPath string, processed map[string]struct{}) ([]string, error) {
|
2020-12-29 18:02:45 +05:30
|
|
|
matches, err := filepath.Glob(absPath)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, errors.Errorf("wildcard found, but unable to glob: %s\n", err)
|
|
|
|
|
}
|
|
|
|
|
results := make([]string, 0, len(matches))
|
|
|
|
|
for _, match := range matches {
|
|
|
|
|
if _, ok := processed[match]; !ok {
|
|
|
|
|
processed[match] = struct{}{}
|
|
|
|
|
results = append(results, match)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return results, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// findFileMatches finds if a path is an absolute file. If the path
|
|
|
|
|
// is a file, it returns true otherwise false with no errors.
|
2022-08-10 23:35:58 +05:30
|
|
|
func (c *DiskCatalog) findFileMatches(absPath string, processed map[string]struct{}) (match string, matched bool, err error) {
|
2020-12-29 18:02:45 +05:30
|
|
|
info, err := os.Stat(absPath)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", false, err
|
|
|
|
|
}
|
|
|
|
|
if !info.Mode().IsRegular() {
|
|
|
|
|
return "", false, nil
|
|
|
|
|
}
|
|
|
|
|
if _, ok := processed[absPath]; !ok {
|
|
|
|
|
processed[absPath] = struct{}{}
|
|
|
|
|
return absPath, true, nil
|
|
|
|
|
}
|
|
|
|
|
return "", true, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// findDirectoryMatches finds matches for templates from a directory
|
2022-08-10 23:35:58 +05:30
|
|
|
func (c *DiskCatalog) findDirectoryMatches(absPath string, processed map[string]struct{}) ([]string, error) {
|
2020-12-29 18:02:45 +05:30
|
|
|
var results []string
|
2022-05-08 08:52:21 +02:00
|
|
|
err := filepath.WalkDir(
|
|
|
|
|
absPath,
|
|
|
|
|
func(path string, d fs.DirEntry, err error) error {
|
|
|
|
|
// continue on errors
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2020-12-29 18:02:45 +05:30
|
|
|
if !d.IsDir() && strings.HasSuffix(path, ".yaml") {
|
|
|
|
|
if _, ok := processed[path]; !ok {
|
|
|
|
|
results = append(results, path)
|
|
|
|
|
processed[path] = struct{}{}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
},
|
2022-05-08 08:52:21 +02:00
|
|
|
)
|
2020-12-29 18:02:45 +05:30
|
|
|
return results, err
|
|
|
|
|
}
|