forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
path.go
184 lines (150 loc) · 4.12 KB
/
path.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package flag
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
flags "github.com/jessevdk/go-flags"
)
type Path string
func (p Path) String() string {
return string(p)
}
func (Path) Complete(prefix string) []flags.Completion {
return completeWithTilde(prefix)
}
type PathWithExistenceCheck string
func (PathWithExistenceCheck) Complete(prefix string) []flags.Completion {
return completeWithTilde(prefix)
}
func (p *PathWithExistenceCheck) UnmarshalFlag(path string) error {
_, err := os.Stat(path)
if err != nil {
if os.IsNotExist(err) {
return &flags.Error{
Type: flags.ErrRequired,
Message: fmt.Sprintf("The specified path '%s' does not exist.", path),
}
}
return err
}
*p = PathWithExistenceCheck(path)
return nil
}
type JSONOrFileWithValidation map[string]interface{}
func (JSONOrFileWithValidation) Complete(prefix string) []flags.Completion {
return completeWithTilde(prefix)
}
func (p *JSONOrFileWithValidation) UnmarshalFlag(pathOrJSON string) error {
var jsonBytes []byte
errorToReturn := &flags.Error{
Type: flags.ErrRequired,
Message: "Invalid configuration provided for -c flag. Please provide a valid JSON object or path to a file containing a valid JSON object.",
}
_, err := os.Stat(pathOrJSON)
if err == nil {
jsonBytes, err = ioutil.ReadFile(pathOrJSON)
if err != nil {
return errorToReturn
}
} else {
jsonBytes = []byte(pathOrJSON)
}
var jsonMap map[string]interface{}
if jsonIsInvalid := json.Unmarshal(jsonBytes, &jsonMap); jsonIsInvalid != nil {
return errorToReturn
}
*p = JSONOrFileWithValidation(jsonMap)
return nil
}
type PathWithExistenceCheckOrURL string
func (PathWithExistenceCheckOrURL) Complete(prefix string) []flags.Completion {
return completeWithTilde(prefix)
}
func (p *PathWithExistenceCheckOrURL) UnmarshalFlag(path string) error {
if !strings.HasPrefix(path, "http://") && !strings.HasPrefix(path, "https://") {
_, err := os.Stat(path)
if err != nil {
if os.IsNotExist(err) {
return &flags.Error{
Type: flags.ErrRequired,
Message: fmt.Sprintf("The specified path '%s' does not exist.", path),
}
}
return err
}
}
*p = PathWithExistenceCheckOrURL(path)
return nil
}
type PathWithAt string
func (PathWithAt) Complete(prefix string) []flags.Completion {
if prefix == "" || prefix[0] != '@' {
return nil
}
prefix = prefix[1:]
var homeDir string
if strings.HasPrefix(prefix, fmt.Sprintf("~%c", os.PathSeparator)) {
// when $HOME is empty this will complete on /, however this is not tested
homeDir = os.Getenv("HOME")
prefix = fmt.Sprintf("%s%s", homeDir, prefix[1:])
}
return findMatches(
fmt.Sprintf("%s*", prefix),
func(path string) string {
if homeDir != "" {
newPath, err := filepath.Rel(homeDir, path)
if err == nil {
path = filepath.Join("~", newPath)
}
}
return fmt.Sprintf("@%s", path)
})
}
type PathWithBool string
func (PathWithBool) Complete(prefix string) []flags.Completion {
return append(
completions([]string{"true", "false"}, prefix, false),
completeWithTilde(prefix)...,
)
}
func findMatches(pattern string, formatMatch func(string) string) []flags.Completion {
paths, _ := filepath.Glob(pattern)
if paths == nil {
return nil
}
matches := []flags.Completion{}
for _, path := range paths {
info, err := os.Stat(path)
if err != nil {
continue
}
formattedMatch := formatMatch(path)
if info.IsDir() {
formattedMatch = fmt.Sprintf("%s%c", formattedMatch, os.PathSeparator)
}
matches = append(matches, flags.Completion{Item: formattedMatch})
}
return matches
}
func completeWithTilde(prefix string) []flags.Completion {
var homeDir string
if strings.HasPrefix(prefix, fmt.Sprintf("~%c", os.PathSeparator)) {
// when $HOME is empty this will complete on /, however this is not tested
homeDir = os.Getenv("HOME")
prefix = fmt.Sprintf("%s%s", homeDir, prefix[1:])
}
return findMatches(
fmt.Sprintf("%s*", prefix),
func(path string) string {
if homeDir != "" {
newPath, err := filepath.Rel(homeDir, path)
if err == nil {
path = filepath.Join("~", newPath)
}
}
return path
})
}