2022-08-06 23:19:07 +02:00
|
|
|
package filepathext
|
|
|
|
|
|
|
|
import (
|
2022-10-15 00:48:45 +02:00
|
|
|
"os"
|
2022-08-06 23:19:07 +02:00
|
|
|
"path/filepath"
|
|
|
|
)
|
|
|
|
|
|
|
|
// SmartJoin joins two paths, but only if the second is not already an
|
|
|
|
// absolute path.
|
|
|
|
func SmartJoin(a, b string) string {
|
|
|
|
if filepath.IsAbs(b) {
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
return filepath.Join(a, b)
|
|
|
|
}
|
2022-10-15 00:48:45 +02:00
|
|
|
|
|
|
|
// TryAbsToRel tries to convert an absolute path to relative based on the
|
|
|
|
// process working directory. If it can't, it returns the absolute path.
|
|
|
|
func TryAbsToRel(abs string) string {
|
|
|
|
wd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
return abs
|
|
|
|
}
|
|
|
|
|
|
|
|
rel, err := filepath.Rel(wd, abs)
|
|
|
|
if err != nil {
|
|
|
|
return abs
|
|
|
|
}
|
|
|
|
|
|
|
|
return rel
|
|
|
|
}
|