github.com/wmuizelaar/kpt@v0.0.0-20221018115725-bd564717b2ed/internal/util/pathutil/pathutil.go (about) 1 // Copyright 2022 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package pathutil 16 17 import ( 18 "os" 19 "path/filepath" 20 ) 21 22 // ResolveAbsAndRelPaths returns absolute and relative paths for input path 23 func ResolveAbsAndRelPaths(path string) (string, string, error) { 24 cwd, err := os.Getwd() 25 if err != nil { 26 return "", "", err 27 } 28 29 var relPath string 30 var absPath string 31 if filepath.IsAbs(path) { 32 // If the provided path is absolute, we find the relative path by 33 // comparing it to the current working directory. 34 relPath, err = filepath.Rel(cwd, path) 35 if err != nil { 36 return "", "", err 37 } 38 absPath = filepath.Clean(path) 39 } else { 40 // If the provided path is relative, we find the absolute path by 41 // combining the current working directory with the relative path. 42 relPath = filepath.Clean(path) 43 absPath = filepath.Join(cwd, path) 44 } 45 46 return absPath, relPath, nil 47 }