-
Notifications
You must be signed in to change notification settings - Fork 42
/
dep.go
214 lines (176 loc) · 5.39 KB
/
dep.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
// Copyright 2021 Upbound Inc
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package xpkg
import (
"context"
"fmt"
"os"
"github.com/alecthomas/kong"
"github.com/crossplane/crossplane-runtime/pkg/errors"
"github.com/crossplane/crossplane/apis/pkg/v1beta1"
"github.com/pterm/pterm"
"github.com/spf13/afero"
"github.com/upbound/up/internal/xpkg"
"github.com/upbound/up/internal/xpkg/dep"
"github.com/upbound/up/internal/xpkg/dep/cache"
"github.com/upbound/up/internal/xpkg/dep/manager"
"github.com/upbound/up/internal/xpkg/dep/resolver/image"
"github.com/upbound/up/internal/xpkg/workspace"
)
const (
errMetaFileNotFound = "crossplane.yaml file not found in current directory"
)
// AfterApply constructs and binds Upbound-specific context to any subcommands
// that have Run() methods that receive it.
func (c *depCmd) AfterApply(kongCtx *kong.Context, p pterm.TextPrinter) error {
kongCtx.Bind(pterm.DefaultBulletList.WithWriter(kongCtx.Stdout))
ctx := context.Background()
fs := afero.NewOsFs()
cache, err := cache.NewLocal(c.CacheDir)
if err != nil {
return err
}
c.c = cache
// only parse the workspace if we aren't attempting to clean the cache
if !c.CleanCache {
r := image.NewResolver()
m, err := manager.New(
manager.WithCache(cache),
manager.WithResolver(r),
)
if err != nil {
return err
}
c.m = m
wd, err := os.Getwd()
if err != nil {
return err
}
ws, err := workspace.New(wd, workspace.WithFS(fs), workspace.WithPrinter(p))
if err != nil {
return err
}
c.ws = ws
if err := ws.Parse(ctx); err != nil {
return err
}
}
// workaround interfaces not being bindable ref: https://github.com/alecthomas/kong/issues/48
kongCtx.BindTo(ctx, (*context.Context)(nil))
return nil
}
// depCmd manages crossplane dependencies.
type depCmd struct {
c *cache.Local
m *manager.Manager
ws *workspace.Workspace
// TODO(@tnthornton) remove cacheDir flag. Having a user supplied flag
// can result in broken behavior between xpls and dep. CacheDir should
// only be supplied by the Config.
CacheDir string `short:"d" help:"Directory used for caching package images." default:"~/.up/cache/" env:"CACHE_DIR" type:"path"`
CleanCache bool `short:"c" help:"Clean dep cache."`
Package string `arg:"" optional:"" help:"Package to be added."`
}
func (c *depCmd) Help() string {
return `
The dep command manages crossplane package dependencies of the package
in the current directory. It caches package information in a local file system
cache (by default in ~/.up/cache), to be used e.g. for the Crossplane language
server.
If a package (e.g. provider-foo@v0.42.0 or provider-foo for latest) is specified,
it will be added to the crossplane.yaml file in the current directory as dependency.
`
}
// Run executes the dep command.
func (c *depCmd) Run(ctx context.Context, p pterm.TextPrinter, pb *pterm.BulletListPrinter) error {
// no need to do anything else if clean cache was called.
// TODO (@tnthornton) this feels a little out of place here. We should
// consider adding a separate command for doing this.
if c.CleanCache {
if err := c.c.Clean(); err != nil {
return err
}
p.Printfln("xpkg cache cleaned")
return nil
}
if c.Package != "" {
if err := c.userSuppliedDep(ctx); err != nil {
return err
}
p.Printfln("%s added to xpkg cache", c.Package)
return nil
}
deps, err := c.metaSuppliedDeps(ctx)
if err != nil {
return err
}
if len(deps) == 0 {
p.Printfln("No dependencies specified")
return nil
}
p.Printfln("Dependencies added to xpkg cache:")
li := make([]pterm.BulletListItem, len(deps))
for i, d := range deps {
li[i] = pterm.BulletListItem{
Level: 0,
Text: fmt.Sprintf("%s (%s)", d.Package, d.Constraints),
Bullet: "-",
}
}
// TODO(hasheddan): bullet list printer incorrectly appends an extra
// trailing newline. Update when fixed upstream.
return pb.WithItems(li).Render()
}
func (c *depCmd) userSuppliedDep(ctx context.Context) error {
// exit early check if we were supplied an invalid package string
_, err := xpkg.ValidDep(c.Package)
if err != nil {
return err
}
d := dep.New(c.Package)
ud, _, err := c.m.AddAll(ctx, d)
if err != nil {
return errors.Wrapf(err, "in %s", c.Package)
}
meta := c.ws.View().Meta()
if meta != nil {
// crossplane.yaml file exists in the workspace, upsert the new dependency
if err := meta.Upsert(ud); err != nil {
return err
}
if err := c.ws.Write(meta); err != nil {
return err
}
}
return nil
}
func (c *depCmd) metaSuppliedDeps(ctx context.Context) ([]v1beta1.Dependency, error) {
meta := c.ws.View().Meta()
if meta == nil {
return nil, errors.New(errMetaFileNotFound)
}
deps, err := meta.DependsOn()
if err != nil {
return nil, err
}
resolvedDeps := make([]v1beta1.Dependency, len(deps))
for i, d := range deps {
ud, _, err := c.m.AddAll(ctx, d)
if err != nil {
return nil, err
}
resolvedDeps[i] = ud
}
return resolvedDeps, nil
}