-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
71 lines (57 loc) · 1.35 KB
/
util.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package clade
import (
"fmt"
"os"
"path/filepath"
"github.com/lesomnus/pl"
)
// ResolvePath returns an absolute representation of joined path of `base` and `path`.
// If the joined path is not absolute, it will be joined with the current working directory to turn it into an absolute path.
// If the `path` is empty, the `base` is joined with `fallback`.
func ResolvePath(base string, path string, fallback string) (string, error) {
if base == "" {
wd, err := os.Getwd()
if err != nil {
return "", err
}
base = wd
}
if !filepath.IsAbs(base) {
abs, err := filepath.Abs(base)
if err != nil {
return "", err
}
base = abs
}
if path == "" {
path = fallback
}
if filepath.IsAbs(path) {
return path, nil
}
return filepath.Join(base, path), nil
}
func toString(value any) (string, bool) {
switch v := value.(type) {
case interface{ String() string }:
return v.String(), true
case string:
return v, true
default:
return "", false
}
}
func executeBeSingleString(executor *pl.Executor, pl *pl.Pl, data any) (string, error) {
results, err := executor.Execute(pl, data)
if err != nil {
return "", err
}
if len(results) != 1 {
return "", fmt.Errorf("expect result be sized 1 but was %d", len(results))
}
v, ok := toString(results[0])
if !ok {
return "", fmt.Errorf("expect result be string or stringer")
}
return v, nil
}