-
Notifications
You must be signed in to change notification settings - Fork 63
/
update.go
124 lines (107 loc) · 2.99 KB
/
update.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
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2022, Unikraft GmbH and The KraftKit Authors.
// Licensed under the BSD-3-Clause License (the "License").
// You may not use this file expect in compliance with the License.
package update
import (
"context"
"fmt"
"github.com/MakeNowJust/heredoc"
"github.com/spf13/cobra"
"kraftkit.sh/cmdfactory"
"kraftkit.sh/config"
"kraftkit.sh/log"
"kraftkit.sh/pack"
"kraftkit.sh/packmanager"
"kraftkit.sh/tui/processtree"
)
type UpdateOptions struct {
Manager string `long:"manager" short:"m" usage:"Force the handler type" default:"all" local:"true"`
}
// Update the local index of known locations for remote Unikraft components.
func Update(ctx context.Context, opts *UpdateOptions, args ...string) error {
if opts == nil {
opts = &UpdateOptions{}
}
return opts.Run(ctx, args)
}
func NewCmd() *cobra.Command {
cmd, err := cmdfactory.New(&UpdateOptions{}, cobra.Command{
Short: "Retrieve new lists of Unikraft components, libraries and packages",
Use: "update [FLAGS]",
Aliases: []string{"upd"},
Long: heredoc.Doc(`
Retrieve new lists of Unikraft components, libraries and packages.
`),
Example: heredoc.Doc(`
# Update the local index of known locations for remote Unikraft components
$ kraft pkg update
`),
Annotations: map[string]string{
cmdfactory.AnnotationHelpGroup: "pkg",
},
})
if err != nil {
panic(err)
}
return cmd
}
func (*UpdateOptions) Pre(cmd *cobra.Command, _ []string) error {
ctx, err := packmanager.WithDefaultUmbrellaManagerInContext(cmd.Context())
if err != nil {
return err
}
cmd.SetContext(ctx)
return nil
}
func (opts *UpdateOptions) Run(ctx context.Context, args []string) error {
var err error
pm := packmanager.G(ctx)
processes := []*processtree.ProcessTreeItem{}
// Force a particular package manager
if len(opts.Manager) > 0 && opts.Manager != "all" {
pm, err = pm.From(pack.PackageFormat(opts.Manager))
if err != nil {
return err
}
processes = []*processtree.ProcessTreeItem{
processtree.NewProcessTreeItem(
"updating",
pm.Format().String(),
func(ctx context.Context) error {
return pm.Update(ctx)
},
),
}
} else {
umbrella, err := packmanager.PackageManagers()
if err != nil {
return err
}
for _, pm := range umbrella {
pm := pm // Go closures
processes = append(processes,
processtree.NewProcessTreeItem(
fmt.Sprintf("updating %s index", pm.Format().String()),
"",
func(ctx context.Context) error {
return pm.Update(ctx)
},
),
)
}
}
model, err := processtree.NewProcessTree(
ctx,
[]processtree.ProcessTreeOption{
processtree.IsParallel(!config.G[config.KraftKit](ctx).NoParallel),
processtree.WithRenderer(log.LoggerTypeFromString(config.G[config.KraftKit](ctx).Log.Type) != log.FANCY),
processtree.WithHideOnSuccess(log.LoggerTypeFromString(config.G[config.KraftKit](ctx).Log.Type) != log.JSON),
},
processes...,
)
if err != nil {
return err
}
return model.Start()
}