Skip to content

Commit

Permalink
add check for os.PathError
Browse files Browse the repository at this point in the history
  • Loading branch information
kaiyan-sheng committed Apr 14, 2021
1 parent 423cf8a commit ab574d3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
2 changes: 1 addition & 1 deletion categories.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func categoriesHandler(packagesBasePaths []string, cacheTime time.Duration) func
for _, t := range p.PolicyTemplates {
// Skip when policy template level `categories` is empty and there is only one policy template
if t.Categories == nil && len(p.PolicyTemplates) == 1 {
continue
break
}

for _, c := range p.Categories {
Expand Down
30 changes: 16 additions & 14 deletions util/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,22 +176,20 @@ func NewPackage(basePath string) (*Package, error) {
}

// Collect basic information from policy templates and store into the /search endpoint
if p.PolicyTemplates != nil {
t := p.PolicyTemplates[i]
baseT := BasePolicyTemplate{
Name: t.Name,
Title: t.Title,
Description: t.Description,
}

for k, i := range p.PolicyTemplates[i].Icons {
t.Icons[k].Path = i.getPath(p)
}
t := p.PolicyTemplates[i]
baseT := BasePolicyTemplate{
Name: t.Name,
Title: t.Title,
Description: t.Description,
}

baseT.Icons = t.Icons
p.BasePolicyTemplates = append(p.BasePolicyTemplates, baseT)
for k, i := range p.PolicyTemplates[i].Icons {
t.Icons[k].Path = i.getPath(p)
}

baseT.Icons = t.Icons
p.BasePolicyTemplates = append(p.BasePolicyTemplates, baseT)

// Store paths for all screenshots under each policy template
if p.PolicyTemplates[i].Screenshots != nil {
for k, s := range p.PolicyTemplates[i].Screenshots {
Expand All @@ -202,7 +200,11 @@ func NewPackage(basePath string) (*Package, error) {
// Store policy template specific README
readmePath := filepath.Join(p.BasePath, "docs", p.PolicyTemplates[i].Name+".md")
readme, err := os.Stat(readmePath)
if err == nil && readme != nil {
if err != nil {
if _, ok := err.(*os.PathError); !ok {
return nil, fmt.Errorf("failed to find %s file: %s", p.PolicyTemplates[i].Name+".md", err)
}
} else if readme != nil {
if readme.IsDir() {
return nil, fmt.Errorf("%s.md is a directory", p.PolicyTemplates[i].Name)
}
Expand Down

0 comments on commit ab574d3

Please sign in to comment.