MCPcopy
hub / github.com/harness/harness / cutOutTerminatedPath

Function cutOutTerminatedPath

app/api/middleware/encode/encode.go:272–298  ·  view source on GitHub ↗

cutOutTerminatedPath cuts out the resource path terminated with the provided marker (path segment suffix). e.g. subPath: "/space1/space2/+/authToken", marker: "/+" => "/space1/space2" e.g. subPath: "/space1/space2.git", marker: ".git" => "/space1/space2" e.g. subPath: "/space1/space2.git/", marker:

(subPath string, marker string)

Source from the content-addressed store, hash-verified

270// case 2: xxxxxxxxxxxxxxxxxxxxxxxxM/ (M is at the end and it should have a / after it)
271// case 3: xxxxxxxxxxxxxxxxxxxxxxxxxM (M is at the end).
272func cutOutTerminatedPath(subPath string, marker string) (string, bool) {
273 // base case of marker being empty
274 if marker == "" {
275 return subPath, true
276 }
277 endsWithSlash := strings.HasSuffix(marker, "/")
278 if !endsWithSlash {
279 marker += "/"
280 }
281
282 // case 1 and case 2
283 if path, _, found := strings.Cut(subPath, marker); found {
284 return path, true
285 }
286
287 if endsWithSlash {
288 return "", false
289 }
290
291 trimmedMarker := strings.TrimSuffix(marker, "/")
292 if strings.HasSuffix(subPath, trimmedMarker) {
293 subPath = subPath[:len(subPath)-len(trimmedMarker)]
294 return subPath, true
295 }
296
297 return "", false
298}

Callers 2

TestCutOutTerminatedPathFunction · 0.85

Calls

no outgoing calls

Tested by 1

TestCutOutTerminatedPathFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…