Skip to content

Commit 9a28885

Browse files
authored
devpkg: add const for flake types (#1627)
1 parent 33ea788 commit 9a28885

File tree

2 files changed

+200
-191
lines changed

2 files changed

+200
-191
lines changed

internal/devpkg/flakeref.go

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@ import (
99
"go.jetpack.io/devbox/internal/redact"
1010
)
1111

12+
const (
13+
FlakeTypeIndirect = "indirect"
14+
FlakeTypePath = "path"
15+
FlakeTypeFile = "file"
16+
FlakeTypeGit = "git"
17+
FlakeTypeGitHub = "github"
18+
FlakeTypeTarball = "tarball"
19+
)
20+
1221
// FlakeRef is a parsed Nix flake reference. A flake reference is a subset of
1322
// the Nix CLI "installable" syntax. Installables may specify an attribute path
1423
// and derivation outputs with a flake reference using the '#' and '^' characters.
@@ -86,7 +95,7 @@ func ParseFlakeRef(ref string) (FlakeRef, error) {
8695
// don't allow it at all.
8796
return FlakeRef{}, redact.Errorf("path-style flake reference %q contains a '?' or '#'", ref)
8897
}
89-
parsed.Type = "path"
98+
parsed.Type = FlakeTypePath
9099
parsed.Path = ref
91100
return parsed, nil
92101
}
@@ -107,7 +116,7 @@ func parseFlakeURLRef(ref string) (parsed FlakeRef, fragment string, err error)
107116
case "", "flake":
108117
// [flake:]<flake-id>(/<rev-or-ref>(/rev)?)?
109118

110-
parsed.Type = "indirect"
119+
parsed.Type = FlakeTypeIndirect
111120
split, err := splitPathOrOpaque(refURL)
112121
if err != nil {
113122
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)
126135
case "path":
127136
// [path:]<path>(\?<params)?
128137

129-
parsed.Type = "path"
138+
parsed.Type = FlakeTypePath
130139
if refURL.Path == "" {
131140
parsed.Path, err = url.PathUnescape(refURL.Opaque)
132141
if err != nil {
@@ -137,26 +146,26 @@ func parseFlakeURLRef(ref string) (parsed FlakeRef, fragment string, err error)
137146
}
138147
case "http", "https", "file":
139148
if isArchive(refURL.Path) {
140-
parsed.Type = "tarball"
149+
parsed.Type = FlakeTypeTarball
141150
} else {
142-
parsed.Type = "file"
151+
parsed.Type = FlakeTypeFile
143152
}
144153
parsed.Dir = refURL.Query().Get("dir")
145154
parsed.URL = refURL.String()
146155
case "tarball+http", "tarball+https", "tarball+file":
147-
parsed.Type = "tarball"
156+
parsed.Type = FlakeTypeTarball
148157
parsed.Dir = refURL.Query().Get("dir")
149158

150159
refURL.Scheme = refURL.Scheme[8:] // remove tarball+
151160
parsed.URL = refURL.String()
152161
case "file+http", "file+https", "file+file":
153-
parsed.Type = "file"
162+
parsed.Type = FlakeTypeFile
154163
parsed.Dir = refURL.Query().Get("dir")
155164

156165
refURL.Scheme = refURL.Scheme[5:] // remove file+
157166
parsed.URL = refURL.String()
158167
case "git", "git+http", "git+https", "git+ssh", "git+git", "git+file":
159-
parsed.Type = "git"
168+
parsed.Type = FlakeTypeGit
160169
q := refURL.Query()
161170
parsed.Dir = q.Get("dir")
162171
parsed.Ref = q.Get("ref")
@@ -182,7 +191,7 @@ func parseFlakeURLRef(ref string) (parsed FlakeRef, fragment string, err error)
182191
func parseGitHubFlakeRef(refURL *url.URL, parsed *FlakeRef) error {
183192
// github:<owner>/<repo>(/<rev-or-ref>)?(\?<params>)?
184193

185-
parsed.Type = "github"
194+
parsed.Type = FlakeTypeGitHub
186195
split, err := splitPathOrOpaque(refURL)
187196
if err != nil {
188197
return err
@@ -237,12 +246,12 @@ func parseGitHubFlakeRef(refURL *url.URL, parsed *FlakeRef) error {
237246
// string.
238247
func (f FlakeRef) String() string {
239248
switch f.Type {
240-
case "file":
249+
case FlakeTypeFile:
241250
if f.URL == "" {
242251
return ""
243252
}
244253
return "file+" + f.URL
245-
case "git":
254+
case FlakeTypeGit:
246255
if f.URL == "" {
247256
return ""
248257
}
@@ -265,7 +274,7 @@ func (f FlakeRef) String() string {
265274
}
266275
url.RawQuery = buildQueryString("ref", f.Ref, "rev", f.Rev, "dir", f.Dir)
267276
return url.String()
268-
case "github":
277+
case FlakeTypeGitHub:
269278
if f.Owner == "" || f.Repo == "" {
270279
return ""
271280
}
@@ -275,7 +284,7 @@ func (f FlakeRef) String() string {
275284
RawQuery: buildQueryString("host", f.Host, "dir", f.Dir),
276285
}
277286
return url.String()
278-
case "indirect":
287+
case FlakeTypeIndirect:
279288
if f.ID == "" {
280289
return ""
281290
}
@@ -285,7 +294,7 @@ func (f FlakeRef) String() string {
285294
RawQuery: buildQueryString("dir", f.Dir),
286295
}
287296
return url.String()
288-
case "path":
297+
case FlakeTypePath:
289298
if f.Path == "" {
290299
return ""
291300
}
@@ -302,7 +311,7 @@ func (f FlakeRef) String() string {
302311
url.Opaque = "."
303312
}
304313
return url.String()
305-
case "tarball":
314+
case FlakeTypeTarball:
306315
if f.URL == "" {
307316
return ""
308317
}

0 commit comments

Comments
 (0)