-
Notifications
You must be signed in to change notification settings - Fork 63
/
add.go
173 lines (150 loc) · 4.46 KB
/
add.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
package add
import (
"context"
"fmt"
"os"
"strings"
"github.com/MakeNowJust/heredoc"
"github.com/spf13/cobra"
"kraftkit.sh/cmdfactory"
"kraftkit.sh/config"
"kraftkit.sh/internal/cli/kraft/pkg/pull"
"kraftkit.sh/packmanager"
"kraftkit.sh/unikraft"
"kraftkit.sh/unikraft/app"
"kraftkit.sh/unikraft/lib"
)
type AddOptions struct {
Kraftfile string `long:"kraftfile" short:"K" usage:"Set an alternative path of the Kraftfile"`
NoUpdate bool `long:"no-update" usage:"Do not update package index before running the build"`
Workdir string `long:"workdir" short:"w" usage:"workdir to add the package to"`
}
// Add adds a Unikraft library to the project directory and updates the Kraftfile
func Add(ctx context.Context, opts *AddOptions, args ...string) error {
if opts == nil {
opts = &AddOptions{}
}
return opts.Run(ctx, args)
}
func NewCmd() *cobra.Command {
cmd, err := cmdfactory.New(&AddOptions{}, cobra.Command{
Short: "Add unikraft library to the project directory",
Use: "add [FLAGS] [PACKAGE|DIR]",
Args: cmdfactory.MinimumArgs(1, "library name is not specified"),
Long: heredoc.Doc(`
Pull a Unikraft component microlibrary from a remote location
and add to the project directory
`),
Annotations: map[string]string{
cmdfactory.AnnotationHelpGroup: "lib",
},
Example: heredoc.Doc(`
# Add a local library to the project
$ kraft lib add path/to/library
# Add a library from a source repository
$ kraft lib add github.com/unikraft/lib-nginx.git
# Add from a manifest
$ kraft lib add nginx:staging
# Add a library from a registry
$ kraft lib add unikraft.org/nginx:stable
`),
})
if err != nil {
panic(err)
}
return cmd
}
func (opts *AddOptions) Pre(cmd *cobra.Command, _ []string) error {
ctx, err := packmanager.WithDefaultUmbrellaManagerInContext(cmd.Context())
if err != nil {
return err
}
cmd.SetContext(ctx)
return nil
}
func (opts *AddOptions) Run(ctx context.Context, args []string) error {
var workdir, packName, packVersion string
var packType unikraft.ComponentType
var err error
var library lib.LibraryConfig
isPackUndefindable := false
packageManager := packmanager.G(ctx)
if f, err := os.Stat(args[0]); err == nil && f.IsDir() {
if err = packageManager.AddSource(ctx, args[0]); err != nil {
return err
}
config.G[config.KraftKit](ctx).Unikraft.Manifests = append(
config.G[config.KraftKit](ctx).Unikraft.Manifests,
args[0],
)
if err := config.M[config.KraftKit](ctx).Write(true); err != nil {
return err
}
if err = packageManager.Update(ctx); err != nil {
return err
}
tempStrs := strings.Split(args[0], "/")
packName = tempStrs[len(tempStrs)-1]
packVersion = "default"
args[0] = packName + ":" + packVersion
} else {
// Verifying if user demands for a library otherwise return error.
packType, packName, packVersion, err = unikraft.GuessTypeNameVersion(args[0])
if err != nil {
isPackUndefindable = true
tempStrs := strings.Split(args[0], "/")
tempStr := tempStrs[len(tempStrs)-1]
tempStrs = strings.Split(tempStr, ".")
tempStr = tempStrs[0]
packType, packName, packVersion, err = unikraft.GuessTypeNameVersion(tempStr)
if err != nil {
return err
}
}
if packType != unikraft.ComponentTypeLib && packType != unikraft.ComponentTypeUnknown {
return fmt.Errorf("specified package %s is not a library", args[0])
}
}
if !isPackUndefindable && !strings.HasPrefix(args[0], "lib") {
args[0] = "lib/" + args[0]
}
// Pulling library.
if err = pull.Pull(ctx, &pull.PullOptions{}, args...); err != nil {
return err
}
popts := []app.ProjectOption{}
if len(opts.Kraftfile) > 0 {
popts = append(popts, app.WithProjectKraftfile(opts.Kraftfile))
} else {
popts = append(popts, app.WithProjectDefaultKraftfiles())
}
project, err := app.NewProjectFromOptions(
ctx,
append(popts, app.WithProjectWorkdir(workdir))...,
)
if err != nil {
return err
}
packs, err := packageManager.Catalog(ctx,
packmanager.WithName(packName),
packmanager.WithTypes(unikraft.ComponentTypeLib),
packmanager.WithVersion(packVersion),
packmanager.WithUpdate(!opts.NoUpdate),
)
if err != nil {
return err
}
if len(packs) == 0 {
return fmt.Errorf("specified library not found")
} else if len(packs) > 1 {
return fmt.Errorf("found more than one library")
}
library, err = lib.NewLibraryFromPackage(ctx, packs[0])
if err != nil {
return err
}
if err = project.AddLibrary(ctx, library); err != nil {
return err
}
return project.Save(ctx)
}