MCPcopy Index your code
hub / github.com/coder/coder / resolveAgentAbsPath

Function resolveAgentAbsPath

cli/open.go:602–636  ·  view source on GitHub ↗

resolveAgentAbsPath resolves the absolute path to a file or directory in the workspace. If the path is relative, it will be resolved relative to the workspace's expanded directory. If the path is absolute, it will be returned as-is. If the path is relative and the workspace directory is not expanded

(workingDirectory, relOrAbsPath, agentOS string, local bool)

Source from the content-addressed store, hash-verified

600// If the path is being resolved within the workspace, the path will be resolved
601// relative to the current working directory.
602func resolveAgentAbsPath(workingDirectory, relOrAbsPath, agentOS string, local bool) (string, error) {
603 switch {
604 case relOrAbsPath == "":
605 return workingDirectory, nil
606
607 case relOrAbsPath == "~" || strings.HasPrefix(relOrAbsPath, "~/"):
608 return "", xerrors.Errorf("path %q requires expansion and is not supported, use an absolute path instead", relOrAbsPath)
609
610 case local:
611 p, err := filepath.Abs(relOrAbsPath)
612 if err != nil {
613 return "", xerrors.Errorf("expand path: %w", err)
614 }
615 return p, nil
616
617 case agentOS == "windows":
618 relOrAbsPath = unixToWindowsPath(relOrAbsPath)
619 switch {
620 case workingDirectory != "" && !isWindowsAbsPath(relOrAbsPath):
621 return windowsJoinPath(workingDirectory, relOrAbsPath), nil
622 case isWindowsAbsPath(relOrAbsPath):
623 return relOrAbsPath, nil
624 default:
625 return "", xerrors.Errorf("path %q not supported, use an absolute path instead", relOrAbsPath)
626 }
627
628 // Note that we use `path` instead of `filepath` since we want Unix behavior.
629 case workingDirectory != "" && !path.IsAbs(relOrAbsPath):
630 return path.Join(workingDirectory, relOrAbsPath), nil
631 case path.IsAbs(relOrAbsPath):
632 return relOrAbsPath, nil
633 default:
634 return "", xerrors.Errorf("path %q not supported, use an absolute path instead", relOrAbsPath)
635 }
636}
637
638func doAsync(f func()) (wait func()) {
639 done := make(chan struct{})

Callers 2

openVSCodeMethod · 0.85
Test_resolveAgentAbsPathFunction · 0.85

Calls 4

unixToWindowsPathFunction · 0.85
isWindowsAbsPathFunction · 0.85
windowsJoinPathFunction · 0.85
ErrorfMethod · 0.45

Tested by 1

Test_resolveAgentAbsPathFunction · 0.68