-
Notifications
You must be signed in to change notification settings - Fork 9
/
locbound.go
230 lines (192 loc) · 5.59 KB
/
locbound.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package fncobra
import (
"context"
"fmt"
"os"
"path/filepath"
"strings"
"sync"
"github.com/spf13/cobra"
"namespacelabs.dev/foundation/internal/console"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/internal/fnfs"
"namespacelabs.dev/foundation/internal/parsing"
"namespacelabs.dev/foundation/schema"
"namespacelabs.dev/foundation/std/cfg"
"namespacelabs.dev/foundation/std/module"
)
type Locations struct {
Locations []fnfs.Location
Refs []*schema.PackageRef
Root *parsing.Root
// Whether the user explicitly specified a list of locations.
// If true, "All" can be not empty if "DefaultToAllWhenEmpty" is true
UserSpecified bool
}
type LocationsParser struct {
locsOut *Locations
env *cfg.Context
opts ParseLocationsOpts
}
type ParseLocationsOpts struct {
// Verify that exactly one location is specified.
RequireSingle bool
// If true, and no locations are specified, then "workspace.ListSchemas" result is used.
ReturnAllIfNoneSpecified bool
// If true, package reference added to Refs
SupportPackageRef bool
}
func ParseLocations(locsOut *Locations, env *cfg.Context, opts ...ParseLocationsOpts) *LocationsParser {
return &LocationsParser{
locsOut: locsOut,
opts: MergeParseLocationOpts(opts),
env: env,
}
}
func MergeParseLocationOpts(opts []ParseLocationsOpts) ParseLocationsOpts {
var merged ParseLocationsOpts
for _, opt := range opts {
if opt.ReturnAllIfNoneSpecified {
merged.ReturnAllIfNoneSpecified = true
}
if opt.RequireSingle {
merged.RequireSingle = true
}
if opt.SupportPackageRef {
merged.SupportPackageRef = true
}
}
return merged
}
func (p *LocationsParser) AddFlags(cmd *cobra.Command) {}
func (p *LocationsParser) Parse(ctx context.Context, args []string) error {
if p.locsOut == nil {
return fnerrors.InternalError("locsOut must be set")
}
result, err := ParseLocs(ctx, args, p.env, p.opts)
if err != nil {
return err
}
*p.locsOut = *result
return nil
}
func ParseLocs(ctx context.Context, args []string, env *cfg.Context, opts ParseLocationsOpts) (*Locations, error) {
root, err := module.FindRoot(ctx, ".")
if err != nil {
return nil, err
}
cwd, err := os.Getwd()
if err != nil {
return nil, err
}
relCwd, err := filepath.Rel(root.Abs(), cwd)
if err != nil {
return nil, err
}
var once sync.Once
var previousSchemaList parsing.SchemaList
var previousErr error
schemaList := func() (parsing.SchemaList, error) {
once.Do(func() {
previousSchemaList, previousErr = parsing.ListSchemas(ctx, *env, root)
})
return previousSchemaList, previousErr
}
var locs []fnfs.Location
var refs []*schema.PackageRef
if opts.ReturnAllIfNoneSpecified && len(args) == 0 {
schemaList, err := schemaList()
if err != nil {
return nil, err
}
locs = schemaList.Locations
} else {
var err error
locs, refs, err = locationsAndPackageRefsFromArgs(ctx, root.Workspace().ModuleName(), root.Workspace().Proto().AllReferencedModules(), relCwd, args, schemaList, opts)
if err != nil {
return nil, err
}
}
if opts.RequireSingle && len(locs)+len(refs) != 1 {
return nil, fnerrors.New("expected exactly one package")
}
return &Locations{
Root: root,
Locations: locs,
Refs: refs,
UserSpecified: len(args) > 0,
}, nil
}
func locationsAndPackageRefsFromArgs(ctx context.Context, mainModuleName string, moduleNames []string, cwd string, args []string, listSchemas func() (parsing.SchemaList, error), opts ParseLocationsOpts) ([]fnfs.Location, []*schema.PackageRef, error) {
var locations []fnfs.Location
var refs []*schema.PackageRef
for _, arg := range args {
if filepath.IsAbs(arg) {
return nil, nil, fnerrors.New("absolute paths are not supported: %s", arg)
}
origArg := arg
expando := false
isRef := false
if strings.HasSuffix(arg, "/...") {
expando = true
arg = strings.TrimSuffix(arg, "/...")
}
if opts.SupportPackageRef && strings.Contains(arg, ":") {
isRef = true
}
moduleName, rel := matchModule(moduleNames, arg)
if moduleName == "" {
moduleName = mainModuleName
rel = filepath.Join(cwd, arg)
}
fmt.Fprintf(console.Debug(ctx), "location parsing: %s -> moduleName: %q rel: %q expando: %v isRef: %v (moduleNames: %v)\n",
origArg, moduleName, rel, expando, isRef, moduleNames)
if strings.HasPrefix(rel, "..") {
return nil, nil, fnerrors.New("can't refer to packages outside of the module root: %s", rel)
}
if expando {
schemas, err := listSchemas()
if err != nil {
return nil, nil, err
}
for _, p := range schemas.Locations {
if matchesExpando(p, moduleName, rel) {
locations = append(locations, p)
}
}
} else {
if isRef {
pr, err := schema.StrictParsePackageRef(filepath.Join(moduleName, rel))
if err != nil {
return nil, nil, err
}
refs = append(refs, pr)
} else {
fsloc := fnfs.Location{
ModuleName: moduleName,
RelPath: rel,
}
locations = append(locations, fsloc)
}
}
}
return locations, refs, nil
}
func matchModule(moduleNames []string, arg string) (string, string) {
for _, m := range moduleNames {
modulePrefix := m + "/"
if strings.HasPrefix(arg, modulePrefix) {
return m, arg[len(modulePrefix):]
}
}
return "", arg
}
func matchesExpando(p fnfs.Location, moduleName, rel string) bool {
if p.ModuleName != moduleName {
return false
}
return p.RelPath == rel || strings.HasPrefix(p.RelPath, rel+"/")
}