Skip to content

Commit 63e849a

Browse files
committed
refactor: remove legacy capture.go
Move shared parsing helpers to artifacts.go and remove unused capture.go and capture_test.go files.
1 parent 3356710 commit 63e849a

3 files changed

Lines changed: 71 additions & 551 deletions

File tree

pkg/cmd/flags/artifacts.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,77 @@ func PrepareArtifacts(artifactsSlice []string) (ArtifactsConfig, error) {
430430
return artifacts, nil
431431
}
432432

433+
// parseFileCaptureSubOption parses file capture sub-options in the format '<sub-opt>=<value>'.
434+
// This is shared logic between the legacy capture flag and the new artifacts flag.
435+
func parseFileCaptureSubOption(option string, captureConfig *config.FileCaptureConfig) error {
436+
optAndValue := strings.SplitN(option, "=", 2)
437+
if len(optAndValue) != 2 || len(optAndValue[1]) == 0 {
438+
return errfmt.Errorf("invalid file capture option format: %s", option)
439+
}
440+
opt := optAndValue[0]
441+
value := optAndValue[1]
442+
switch opt {
443+
case "path":
444+
if !strings.HasSuffix(option, "*") {
445+
return errfmt.Errorf("file path filter should end with *")
446+
}
447+
pathPrefix := strings.TrimSuffix(value, "*")
448+
if len(pathPrefix) == 0 {
449+
return errfmt.Errorf("capture path filter cannot be empty")
450+
}
451+
captureConfig.PathFilter = append(captureConfig.PathFilter, pathPrefix)
452+
case "type":
453+
typeFilter := strings.TrimPrefix(option, "type=")
454+
filterFlag, err := parseFileCaptureType(typeFilter)
455+
if err != nil {
456+
return err
457+
}
458+
captureConfig.TypeFilter |= filterFlag
459+
case "fd":
460+
typeFilter := strings.TrimPrefix(option, "fd=")
461+
filterFlag, err := parseFileCaptureFDs(typeFilter)
462+
if err != nil {
463+
return err
464+
}
465+
captureConfig.TypeFilter |= filterFlag
466+
default:
467+
return errfmt.Errorf("unrecognized file capture option: %s", opt)
468+
}
469+
470+
return nil
471+
}
472+
473+
var captureFileTypeStringToFlag = map[string]config.FileCaptureType{
474+
"pipe": config.CapturePipeFiles,
475+
"socket": config.CaptureSocketFiles,
476+
"regular": config.CaptureRegularFiles,
477+
"elf": config.CaptureELFFiles,
478+
}
479+
480+
// parseFileCaptureType parses file type string to its matching bit-flag value.
481+
func parseFileCaptureType(filter string) (config.FileCaptureType, error) {
482+
filterFlag, ok := captureFileTypeStringToFlag[filter]
483+
if ok {
484+
return filterFlag, nil
485+
}
486+
return 0, errfmt.Errorf("unsupported file type filter value for capture - %s", filter)
487+
}
488+
489+
var captureFDsStringToFlag = map[string]config.FileCaptureType{
490+
"stdin": config.CaptureStdinFiles,
491+
"stdout": config.CaptureStdoutFiles,
492+
"stderr": config.CaptureStderrFiles,
493+
}
494+
495+
// parseFileCaptureFDs parses file standard FD string to its matching bit-flag value.
496+
func parseFileCaptureFDs(filter string) (config.FileCaptureType, error) {
497+
filterFlag, ok := captureFDsStringToFlag[filter]
498+
if ok {
499+
return filterFlag, nil
500+
}
501+
return 0, errfmt.Errorf("unsupported file FD filter value for capture - %s", filter)
502+
}
503+
433504
// parseFileArtifactOptionWrite parses file-write artifact options.
434505
func parseFileArtifactOptionWrite(fileConfig *FileWriteConfig, subOpt string) error {
435506
if strings.HasPrefix(subOpt, filtersKey+"=") {

pkg/cmd/flags/capture.go

Lines changed: 0 additions & 297 deletions
This file was deleted.

0 commit comments

Comments
 (0)