MCPcopy Create free account
hub / github.com/cogentcore/core / dirsTo

Method dirsTo

filetree/node.go:504–544  ·  view source on GitHub ↗

dirsTo opens all the directories above the given filename, and returns the node for element at given path (can be a file or directory itself -- not opened -- just returned)

(path string)

Source from the content-addressed store, hash-verified

502// dirsTo opens all the directories above the given filename, and returns the node
503// for element at given path (can be a file or directory itself -- not opened -- just returned)
504func (fn *Node) dirsTo(path string) (*Node, error) {
505 pth, err := filepath.Abs(path)
506 if err != nil {
507 log.Printf("filetree.Node DirsTo path %v could not be turned into an absolute path: %v\n", path, err)
508 return nil, err
509 }
510 rpath := fn.RelativePathFrom(core.Filename(pth))
511 if rpath == "." {
512 return fn, nil
513 }
514 dirs := strings.Split(rpath, string(filepath.Separator))
515 cfn := fn
516 sz := len(dirs)
517 for i := 0; i < sz; i++ {
518 dr := dirs[i]
519 sfni := cfn.ChildByName(dr, 0)
520 if sfni == nil {
521 if i == sz-1 { // ok for terminal -- might not exist yet
522 return cfn, nil
523 } else {
524 err = fmt.Errorf("filetree.Node could not find node %v in: %v, orig: %v, rel: %v", dr, cfn.Filepath, pth, rpath)
525 // slog.Error(err.Error()) // note: this is expected sometimes
526 return nil, err
527 }
528 }
529 sfn := AsNode(sfni)
530 if sfn.IsDir() || i == sz-1 {
531 if i < sz-1 && !sfn.isOpen() {
532 sfn.openDir()
533 } else {
534 cfn = sfn
535 }
536 } else {
537 err := fmt.Errorf("filetree.Node non-terminal node %v is not a directory in: %v", dr, cfn.Filepath)
538 slog.Error(err.Error())
539 return nil, err
540 }
541 cfn = sfn
542 }
543 return cfn, nil
544}

Callers 2

FindFileMethod · 0.95
UpdatePathMethod · 0.80

Calls 12

RelativePathFromMethod · 0.95
FilenameTypeAlias · 0.92
SplitMethod · 0.80
ChildByNameMethod · 0.80
openDirMethod · 0.80
AsNodeFunction · 0.70
ErrorfMethod · 0.65
ErrorMethod · 0.65
AbsMethod · 0.45
PrintfMethod · 0.45
IsDirMethod · 0.45
isOpenMethod · 0.45

Tested by

no test coverage detected