MCPcopy Create free account
hub / github.com/bufbuild/buf / imageWithOnlyPaths

Function imageWithOnlyPaths

private/bufpkg/bufimage/util.go:42–200  ·  view source on GitHub ↗

paths can be either files (ending in .proto) or directories paths must be normalized and validated, and not duplicated if a directory, all .proto files underneath will be included

(image Image, fileOrDirPaths []string, excludeFileOrDirPaths []string, allowNotExist bool)

Source from the content-addressed store, hash-verified

40// paths must be normalized and validated, and not duplicated
41// if a directory, all .proto files underneath will be included
42func imageWithOnlyPaths(image Image, fileOrDirPaths []string, excludeFileOrDirPaths []string, allowNotExist bool) (Image, error) {
43 if err := normalpath.ValidatePathsNormalizedValidatedUnique(fileOrDirPaths); err != nil {
44 return nil, err
45 }
46 if err := normalpath.ValidatePathsNormalizedValidatedUnique(excludeFileOrDirPaths); err != nil {
47 return nil, err
48 }
49 excludeFileOrDirPathMap := xslices.ToStructMap(excludeFileOrDirPaths)
50 // These are the files that fileOrDirPaths actually reference and will
51 // result in the non-imports in our resulting Image. The Image will also include
52 // the ImageFiles that the nonImportImageFiles import
53 nonImportPaths := make(map[string]struct{})
54 var nonImportImageFiles []ImageFile
55 // We have only exclude paths, and therefore all other paths are target paths.
56 if len(fileOrDirPaths) == 0 && len(excludeFileOrDirPaths) > 0 {
57 for _, imageFile := range image.Files() {
58 if !imageFile.IsImport() {
59 if !normalpath.MapHasEqualOrContainingPath(excludeFileOrDirPathMap, imageFile.Path(), normalpath.Relative) {
60 nonImportPaths[imageFile.Path()] = struct{}{}
61 nonImportImageFiles = append(nonImportImageFiles, imageFile)
62 }
63 }
64 }
65 // Finally, before we construct the image, we need to validate that all exclude paths
66 // provided adhere to the allowNotExist flag.
67 if !allowNotExist {
68 if err := checkExcludePathsExistInImage(image, excludeFileOrDirPaths); err != nil {
69 return nil, err
70 }
71 }
72 return getImageWithImports(image, nonImportPaths, nonImportImageFiles)
73 }
74 // We do a check here to ensure that no paths are duplicated as a target and an exclude.
75 for _, fileOrDirPath := range fileOrDirPaths {
76 if _, ok := excludeFileOrDirPathMap[fileOrDirPath]; ok {
77 return nil, fmt.Errorf(
78 "cannot set the same path for both --path and --exclude-path flags: %s",
79 normalpath.Unnormalize(fileOrDirPath),
80 )
81 }
82 }
83 // potentialDirPaths are paths that we need to check if they are directories
84 // these are any files that do not end in .proto, as well as files that
85 // end in .proto but do not have a corresponding ImageFile - if there
86 // is not an ImageFile, the path ending in .proto could be a directory
87 // that itself contains ImageFiles, i.e. a/b.proto/c.proto is valid if not dumb
88 var potentialDirPaths []string
89 for _, fileOrDirPath := range fileOrDirPaths {
90 // this is not allowed, this is the equivalent of a root
91 if fileOrDirPath == "." {
92 return nil, errors.New(`"." is not a valid path value`)
93 }
94 if normalpath.Ext(fileOrDirPath) != ".proto" {
95 // not a .proto file, therefore must be a directory
96 potentialDirPaths = append(potentialDirPaths, fileOrDirPath)
97 } else {
98 if imageFile := image.GetFile(fileOrDirPath); imageFile != nil {
99 // We do not need to check excludes here, since we already checked for duplicated

Callers 2

ImageWithOnlyPathsFunction · 0.85

Calls 12

UnnormalizeFunction · 0.92
ExtFunction · 0.92
getImageWithImportsFunction · 0.85
shouldExcludeFileFunction · 0.85
FilesMethod · 0.65
IsImportMethod · 0.65
PathMethod · 0.65
GetFileMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…