-
-
Notifications
You must be signed in to change notification settings - Fork 153
Expand file tree
/
Copy pathworking_directory.go
More file actions
48 lines (42 loc) · 1.4 KB
/
working_directory.go
File metadata and controls
48 lines (42 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package cmd
import (
"os"
"path/filepath"
errUtils "github.com/cloudposse/atmos/errors"
)
// resolveWorkingDirectory resolves and validates a working directory path.
// If workDir is empty, returns defaultDir. If workDir is relative, resolves against basePath.
// Returns error if the directory doesn't exist or isn't a directory.
func resolveWorkingDirectory(workDir, basePath, defaultDir string) (string, error) {
if workDir == "" {
return defaultDir, nil
}
// Clean and resolve paths. filepath.Clean normalizes paths like /tmp/foo/.. to /tmp.
// For relative paths, filepath.Join already cleans the result.
resolvedDir := filepath.Clean(workDir)
if !filepath.IsAbs(workDir) {
resolvedDir = filepath.Join(basePath, workDir)
}
// Validate directory exists and is a directory.
info, err := os.Stat(resolvedDir)
if os.IsNotExist(err) {
return "", errUtils.Build(errUtils.ErrWorkingDirNotFound).
WithCause(err).
WithContext("path", resolvedDir).
WithHint("Check that the working_directory path exists").
Err()
}
if err != nil {
return "", errUtils.Build(errUtils.ErrWorkingDirAccessFailed).
WithCause(err).
WithContext("path", resolvedDir).
Err()
}
if !info.IsDir() {
return "", errUtils.Build(errUtils.ErrWorkingDirNotDirectory).
WithContext("path", resolvedDir).
WithHint("The working_directory must be a directory, not a file").
Err()
}
return resolvedDir, nil
}