-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpathops.go
37 lines (32 loc) · 942 Bytes
/
pathops.go
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
// Copyright (C) 2017, 2018 Damon Revoe. All rights reserved.
// Use of this source code is governed by the MIT
// license, which can be found in the LICENSE file.
package main
import (
"os"
"path/filepath"
)
func relativeToCwd(absPath string) (string, error) {
cwd, err := os.Getwd()
if err != nil {
return "", err
}
return filepath.Rel(cwd, absPath)
}
func absIfNotEmpty(pathname string) (string, error) {
if pathname == "" {
return "", nil
}
return filepath.Abs(pathname)
}
// relativeIfShorter returns a pathname relative between the first
// and the second pathname arguments under condition that both
// arguments are absolute pathnames and the resulting relative
// pathname is shorter than the second argument.
func relativeIfShorter(basePath, targetPath string) string {
relPath, err := filepath.Rel(basePath, targetPath)
if err == nil && len(relPath) < len(targetPath) {
return relPath
}
return targetPath
}