|
| 1 | +// Copyright 2022 Drone.IO Inc. All rights reserved. |
| 2 | +// Use of this source code is governed by the Polyform License |
| 3 | +// that can be found in the LICENSE file. |
| 4 | + |
| 5 | +// Copyright 2019 Drone.IO Inc. All rights reserved. |
| 6 | +// Use of this source code is governed by the Polyform License |
| 7 | +// that can be found in the LICENSE file. |
| 8 | + |
| 9 | +// Package internal contains runner internals. |
| 10 | +package paths |
| 11 | + |
| 12 | +import ( |
| 13 | + "os" |
| 14 | + "path/filepath" |
| 15 | +) |
| 16 | + |
| 17 | +const ( |
| 18 | + defaultWorkingDir = "/tmp" |
| 19 | + workingDirEnvVar = "WORKING_DIR" |
| 20 | +) |
| 21 | + |
| 22 | +var workingDir string |
| 23 | + |
| 24 | +func init() { |
| 25 | + workingDir = os.Getenv(workingDirEnvVar) |
| 26 | + if workingDir == "" { |
| 27 | + workingDir = defaultWorkingDir |
| 28 | + } |
| 29 | + // Ensure the path is absolute |
| 30 | + if !filepath.IsAbs(workingDir) { |
| 31 | + absPath, err := filepath.Abs(workingDir) |
| 32 | + if err == nil { |
| 33 | + workingDir = absPath |
| 34 | + } |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +// JoinPath joins the working directory with the provided path segments |
| 39 | +// If the path is already absolute, it returns the path as-is |
| 40 | +func JoinPath(elem ...string) string { |
| 41 | + if len(elem) == 0 { |
| 42 | + return workingDir |
| 43 | + } |
| 44 | + // If the first element is an absolute path, use it directly |
| 45 | + if filepath.IsAbs(elem[0]) { |
| 46 | + return filepath.Join(elem...) |
| 47 | + } |
| 48 | + // Otherwise, join with working directory |
| 49 | + return filepath.Join(append([]string{workingDir}, elem...)...) |
| 50 | +} |
| 51 | + |
| 52 | +// GetSharedVolPath returns the path for shared volume |
| 53 | +func GetSharedVolPath() string { |
| 54 | + return JoinPath("engine") |
| 55 | +} |
| 56 | + |
| 57 | +// GetTIVolPath returns the path for TI volume |
| 58 | +func GetTIVolPath() string { |
| 59 | + return JoinPath("ti") |
| 60 | +} |
| 61 | + |
| 62 | +// ResolveHostPath resolves a host path to be within the working directory |
| 63 | +// System paths (like Docker socket) are returned as-is |
| 64 | +// Other absolute paths are made relative to the working directory |
| 65 | +func ResolveHostPath(path string) string { |
| 66 | + // If it's already an absolute path, strip the leading / and make it relative to working dir |
| 67 | + // This ensures paths like /harness become {WORKING_DIR}/harness |
| 68 | + if filepath.IsAbs(path) { |
| 69 | + // Remove leading separator(s) |
| 70 | + cleanPath := filepath.Clean(path) |
| 71 | + if len(cleanPath) > 0 && (cleanPath[0] == '/' || cleanPath[0] == '\\') { |
| 72 | + cleanPath = cleanPath[1:] |
| 73 | + } |
| 74 | + // Handle Windows drive letters (C:, D:, etc.) |
| 75 | + if len(cleanPath) >= 2 && cleanPath[1] == ':' { |
| 76 | + cleanPath = cleanPath[2:] |
| 77 | + if len(cleanPath) > 0 && (cleanPath[0] == '/' || cleanPath[0] == '\\') { |
| 78 | + cleanPath = cleanPath[1:] |
| 79 | + } |
| 80 | + } |
| 81 | + return JoinPath(cleanPath) |
| 82 | + } |
| 83 | + // If it's relative, join with working directory |
| 84 | + return JoinPath(path) |
| 85 | +} |
0 commit comments