@@ -9,6 +9,15 @@ import (
9
9
"go.jetpack.io/devbox/internal/redact"
10
10
)
11
11
12
+ const (
13
+ FlakeTypeIndirect = "indirect"
14
+ FlakeTypePath = "path"
15
+ FlakeTypeFile = "file"
16
+ FlakeTypeGit = "git"
17
+ FlakeTypeGitHub = "github"
18
+ FlakeTypeTarball = "tarball"
19
+ )
20
+
12
21
// FlakeRef is a parsed Nix flake reference. A flake reference is a subset of
13
22
// the Nix CLI "installable" syntax. Installables may specify an attribute path
14
23
// and derivation outputs with a flake reference using the '#' and '^' characters.
@@ -86,7 +95,7 @@ func ParseFlakeRef(ref string) (FlakeRef, error) {
86
95
// don't allow it at all.
87
96
return FlakeRef {}, redact .Errorf ("path-style flake reference %q contains a '?' or '#'" , ref )
88
97
}
89
- parsed .Type = "path"
98
+ parsed .Type = FlakeTypePath
90
99
parsed .Path = ref
91
100
return parsed , nil
92
101
}
@@ -107,7 +116,7 @@ func parseFlakeURLRef(ref string) (parsed FlakeRef, fragment string, err error)
107
116
case "" , "flake" :
108
117
// [flake:]<flake-id>(/<rev-or-ref>(/rev)?)?
109
118
110
- parsed .Type = "indirect"
119
+ parsed .Type = FlakeTypeIndirect
111
120
split , err := splitPathOrOpaque (refURL )
112
121
if err != nil {
113
122
return FlakeRef {}, "" , redact .Errorf ("parse flake reference URL path: %v" , err )
@@ -126,7 +135,7 @@ func parseFlakeURLRef(ref string) (parsed FlakeRef, fragment string, err error)
126
135
case "path" :
127
136
// [path:]<path>(\?<params)?
128
137
129
- parsed .Type = "path"
138
+ parsed .Type = FlakeTypePath
130
139
if refURL .Path == "" {
131
140
parsed .Path , err = url .PathUnescape (refURL .Opaque )
132
141
if err != nil {
@@ -137,26 +146,26 @@ func parseFlakeURLRef(ref string) (parsed FlakeRef, fragment string, err error)
137
146
}
138
147
case "http" , "https" , "file" :
139
148
if isArchive (refURL .Path ) {
140
- parsed .Type = "tarball"
149
+ parsed .Type = FlakeTypeTarball
141
150
} else {
142
- parsed .Type = "file"
151
+ parsed .Type = FlakeTypeFile
143
152
}
144
153
parsed .Dir = refURL .Query ().Get ("dir" )
145
154
parsed .URL = refURL .String ()
146
155
case "tarball+http" , "tarball+https" , "tarball+file" :
147
- parsed .Type = "tarball"
156
+ parsed .Type = FlakeTypeTarball
148
157
parsed .Dir = refURL .Query ().Get ("dir" )
149
158
150
159
refURL .Scheme = refURL .Scheme [8 :] // remove tarball+
151
160
parsed .URL = refURL .String ()
152
161
case "file+http" , "file+https" , "file+file" :
153
- parsed .Type = "file"
162
+ parsed .Type = FlakeTypeFile
154
163
parsed .Dir = refURL .Query ().Get ("dir" )
155
164
156
165
refURL .Scheme = refURL .Scheme [5 :] // remove file+
157
166
parsed .URL = refURL .String ()
158
167
case "git" , "git+http" , "git+https" , "git+ssh" , "git+git" , "git+file" :
159
- parsed .Type = "git"
168
+ parsed .Type = FlakeTypeGit
160
169
q := refURL .Query ()
161
170
parsed .Dir = q .Get ("dir" )
162
171
parsed .Ref = q .Get ("ref" )
@@ -182,7 +191,7 @@ func parseFlakeURLRef(ref string) (parsed FlakeRef, fragment string, err error)
182
191
func parseGitHubFlakeRef (refURL * url.URL , parsed * FlakeRef ) error {
183
192
// github:<owner>/<repo>(/<rev-or-ref>)?(\?<params>)?
184
193
185
- parsed .Type = "github"
194
+ parsed .Type = FlakeTypeGitHub
186
195
split , err := splitPathOrOpaque (refURL )
187
196
if err != nil {
188
197
return err
@@ -237,12 +246,12 @@ func parseGitHubFlakeRef(refURL *url.URL, parsed *FlakeRef) error {
237
246
// string.
238
247
func (f FlakeRef ) String () string {
239
248
switch f .Type {
240
- case "file" :
249
+ case FlakeTypeFile :
241
250
if f .URL == "" {
242
251
return ""
243
252
}
244
253
return "file+" + f .URL
245
- case "git" :
254
+ case FlakeTypeGit :
246
255
if f .URL == "" {
247
256
return ""
248
257
}
@@ -265,7 +274,7 @@ func (f FlakeRef) String() string {
265
274
}
266
275
url .RawQuery = buildQueryString ("ref" , f .Ref , "rev" , f .Rev , "dir" , f .Dir )
267
276
return url .String ()
268
- case "github" :
277
+ case FlakeTypeGitHub :
269
278
if f .Owner == "" || f .Repo == "" {
270
279
return ""
271
280
}
@@ -275,7 +284,7 @@ func (f FlakeRef) String() string {
275
284
RawQuery : buildQueryString ("host" , f .Host , "dir" , f .Dir ),
276
285
}
277
286
return url .String ()
278
- case "indirect" :
287
+ case FlakeTypeIndirect :
279
288
if f .ID == "" {
280
289
return ""
281
290
}
@@ -285,7 +294,7 @@ func (f FlakeRef) String() string {
285
294
RawQuery : buildQueryString ("dir" , f .Dir ),
286
295
}
287
296
return url .String ()
288
- case "path" :
297
+ case FlakeTypePath :
289
298
if f .Path == "" {
290
299
return ""
291
300
}
@@ -302,7 +311,7 @@ func (f FlakeRef) String() string {
302
311
url .Opaque = "."
303
312
}
304
313
return url .String ()
305
- case "tarball" :
314
+ case FlakeTypeTarball :
306
315
if f .URL == "" {
307
316
return ""
308
317
}
0 commit comments