Skip to content

Commit

Permalink
item name as subdir/file.yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
mmetc committed Aug 6, 2024
1 parent e7547fd commit bf5f570
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
7 changes: 5 additions & 2 deletions pkg/cwhub/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,20 +130,23 @@ func (h *Hub) getItemFileInfo(path string, logger *logrus.Logger) (*itemFileInfo
return nil, fmt.Errorf("path is too short: %s (%d)", path, len(subsInstall))

Check warning on line 130 in pkg/cwhub/sync.go

View check run for this annotation

Codecov / codecov/patch

pkg/cwhub/sync.go#L130

Added line #L130 was not covered by tests
}

// this can be in any number of subdirs, we ignore them
// this can be in any number of subdirs, we join them to compose the item name

ftype := subsInstall[0]
stage := ""
fname := strings.Join(subsInstall[1:], "/")

if ftype == PARSERS || ftype == POSTOVERFLOWS {
stage = subsInstall[1]
fname = strings.Join(subsInstall[2:], "/")
}

ret = &itemFileInfo{
inhub: false,
ftype: ftype,
stage: stage,
fauthor: "",
fname: subsInstall[len(subsInstall)-1],
fname: fname,
}
default:
return nil, fmt.Errorf("file '%s' is not from hub '%s' nor from the configuration directory '%s'", path, hubDir, installDir)

Check warning on line 152 in pkg/cwhub/sync.go

View check run for this annotation

Codecov / codecov/patch

pkg/cwhub/sync.go#L151-L152

Added lines #L151 - L152 were not covered by tests
Expand Down
12 changes: 11 additions & 1 deletion test/bats/20_hub_items.bats
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,8 @@ teardown() {
@test "item files can be in a subdirectory" {
rune -0 mkdir -p "$CONFIG_DIR/scenarios/sub/sub2/sub3"
rune -0 touch "$CONFIG_DIR/scenarios/sub/imlocal.yaml"
rune -0 cscli scenarios inspect imlocal.yaml -o json
# subdir name is now part of the item name
rune -0 cscli scenarios inspect sub/imlocal.yaml -o json
rune -0 jq -e '[.tainted,.local==false,true]' <(output)
rune -0 rm "$CONFIG_DIR/scenarios/sub/imlocal.yaml"

Expand All @@ -270,3 +271,12 @@ teardown() {
rune -0 cscli scenarios inspect crowdsecurity/smb-bf -o json
rune -0 jq -e '[.tainted,.local==false,false]' <(output)
}

@test "same file name for local items in different subdirectories" {
rune -0 mkdir -p "$CONFIG_DIR"/scenarios/{foo,bar}
rune -0 touch "$CONFIG_DIR/scenarios/foo/local.yaml"
rune -0 touch "$CONFIG_DIR/scenarios/bar/local.yaml"
rune -0 cscli scenarios list -o json
rune -0 jq -c '[.scenarios[].name] | sort' <(output)
assert_json '["bar/local.yaml","foo/local.yaml"]'
}

0 comments on commit bf5f570

Please sign in to comment.