MCPcopy
hub / github.com/bitfield/script / Dirname

Method Dirname

script.go:310–323  ·  view source on GitHub ↗

Dirname reads paths from the pipe, one per line, and produces only the parent directories of each path. For example, /usr/local/bin/foo would become just /usr/local/bin. This is the complementary operation to [Pipe.Basename]. If a line is empty, Dirname will transform it to a single dot. Trailing s

()

Source from the content-addressed store, hash-verified

308// slashes are removed, unless Dirname returns the root folder. Otherwise, the
309// behaviour of Dirname is the same as [filepath.Dir] (not by coincidence).
310func (p *Pipe) Dirname() *Pipe {
311 return p.FilterLine(func(line string) string {
312 // filepath.Dir() does not handle trailing slashes correctly
313 if len(line) > 1 && strings.HasSuffix(line, "/") {
314 line = line[:len(line)-1]
315 }
316 dirname := filepath.Dir(line)
317 // filepath.Dir() does not preserve a leading './'
318 if strings.HasPrefix(line, "./") {
319 return "./" + dirname
320 }
321 return dirname
322 })
323}
324
325// Do performs the HTTP request req using the pipe's configured HTTP client, as
326// set by [Pipe.WithHTTPClient], or [http.DefaultClient] otherwise. The

Calls 1

FilterLineMethod · 0.95