2021-02-26 07:43:11 +00:00
|
|
|
package catalog
|
2020-12-29 12:32:45 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ResolvePath resolves the path to an absolute one in various ways.
|
|
|
|
//
|
|
|
|
// It checks if the filename is an absolute path, looks in the current directory
|
|
|
|
// or checking the nuclei templates directory. If a second path is given,
|
|
|
|
// it also tries to find paths relative to that second path.
|
2021-02-26 07:43:11 +00:00
|
|
|
func (c *Catalog) ResolvePath(templateName, second string) (string, error) {
|
2021-08-23 13:11:26 +00:00
|
|
|
if filepath.IsAbs(templateName) {
|
2020-12-29 12:32:45 +00:00
|
|
|
return templateName, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if second != "" {
|
2021-08-23 11:53:37 +00:00
|
|
|
secondBasePath := filepath.Join(filepath.Dir(second), templateName)
|
2020-12-29 12:32:45 +00:00
|
|
|
if _, err := os.Stat(secondBasePath); !os.IsNotExist(err) {
|
|
|
|
return secondBasePath, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
curDirectory, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2021-08-23 11:53:37 +00:00
|
|
|
templatePath := filepath.Join(curDirectory, templateName)
|
2020-12-29 12:32:45 +00:00
|
|
|
if _, err := os.Stat(templatePath); !os.IsNotExist(err) {
|
|
|
|
return templatePath, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.templatesDirectory != "" {
|
2021-08-23 11:53:37 +00:00
|
|
|
templatePath := filepath.Join(c.templatesDirectory, templateName)
|
2020-12-29 12:32:45 +00:00
|
|
|
if _, err := os.Stat(templatePath); !os.IsNotExist(err) {
|
|
|
|
return templatePath, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", fmt.Errorf("no such path found: %s", templateName)
|
|
|
|
}
|