-
Notifications
You must be signed in to change notification settings - Fork 63
/
kraft.go
168 lines (142 loc) · 4.91 KB
/
kraft.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
// 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 except in compliance with the License.
package kraft
import (
"context"
"fmt"
"os"
"github.com/MakeNowJust/heredoc"
"github.com/rancher/wrangler/pkg/signals"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"kraftkit.sh/cmdfactory"
"kraftkit.sh/config"
"kraftkit.sh/internal/bootstrap"
"kraftkit.sh/internal/cli"
kitupdate "kraftkit.sh/internal/update"
kitversion "kraftkit.sh/internal/version"
"kraftkit.sh/iostreams"
"kraftkit.sh/log"
"kraftkit.sh/internal/cli/kraft/build"
"kraftkit.sh/internal/cli/kraft/clean"
"kraftkit.sh/internal/cli/kraft/cloud"
"kraftkit.sh/internal/cli/kraft/events"
"kraftkit.sh/internal/cli/kraft/fetch"
"kraftkit.sh/internal/cli/kraft/login"
"kraftkit.sh/internal/cli/kraft/logs"
"kraftkit.sh/internal/cli/kraft/menu"
"kraftkit.sh/internal/cli/kraft/net"
"kraftkit.sh/internal/cli/kraft/pkg"
"kraftkit.sh/internal/cli/kraft/properclean"
"kraftkit.sh/internal/cli/kraft/ps"
"kraftkit.sh/internal/cli/kraft/remove"
"kraftkit.sh/internal/cli/kraft/run"
"kraftkit.sh/internal/cli/kraft/set"
"kraftkit.sh/internal/cli/kraft/stop"
"kraftkit.sh/internal/cli/kraft/unset"
"kraftkit.sh/internal/cli/kraft/version"
// Additional initializers
_ "kraftkit.sh/manifest"
_ "kraftkit.sh/oci"
)
type KraftOptions struct{}
func NewCmd() *cobra.Command {
cmd, err := cmdfactory.New(&KraftOptions{}, cobra.Command{
Short: "Build and use highly customized and ultra-lightweight unikernels",
Use: "kraft [FLAGS] SUBCOMMAND",
Long: heredoc.Docf(`
.
/^\ Build and use highly customized and ultra-lightweight unikernels.
:[ ]:
| = | Version: %s
/|/=\|\ Documentation: https://kraftkit.sh/
(_:| |:_) Issues & support: https://github.com/unikraft/kraftkit/issues
v v Platform: https://kraft.cloud/ (Join the beta!)
' '`, kitversion.Version()),
CompletionOptions: cobra.CompletionOptions{
HiddenDefaultCmd: true,
},
})
if err != nil {
panic(err)
}
cmd.AddGroup(&cobra.Group{ID: "build", Title: "BUILD COMMANDS"})
cmd.AddCommand(build.NewCmd())
cmd.AddCommand(clean.NewCmd())
cmd.AddCommand(fetch.NewCmd())
cmd.AddCommand(menu.NewCmd())
cmd.AddCommand(properclean.NewCmd())
cmd.AddCommand(set.NewCmd())
cmd.AddCommand(unset.NewCmd())
cmd.AddGroup(&cobra.Group{ID: "pkg", Title: "PACKAGING COMMANDS"})
cmd.AddCommand(pkg.NewCmd())
cmd.AddGroup(&cobra.Group{ID: "run", Title: "LOCAL RUNTIME COMMANDS"})
cmd.AddCommand(events.NewCmd())
cmd.AddCommand(logs.NewCmd())
cmd.AddCommand(ps.NewCmd())
cmd.AddCommand(remove.NewCmd())
cmd.AddCommand(run.NewCmd())
cmd.AddCommand(stop.NewCmd())
cmd.AddGroup(&cobra.Group{ID: "net", Title: "LOCAL NETWORKING COMMANDS"})
cmd.AddCommand(net.NewCmd())
cmd.AddGroup(&cobra.Group{ID: "kraftcloud", Title: "KRAFT CLOUD COMMANDS"})
cmd.AddCommand(cloud.NewCmd())
cmd.AddGroup(&cobra.Group{ID: "kraftcloud-img", Title: "KRAFT CLOUD IMAGE COMMANDS"})
cmd.AddGroup(&cobra.Group{ID: "kraftcloud-instance", Title: "KRAFT CLOUD INSTANCE COMMANDS"})
cmd.AddGroup(&cobra.Group{ID: "misc", Title: "MISCELLANEOUS COMMANDS"})
cmd.AddCommand(login.NewCmd())
cmd.AddCommand(version.NewCmd())
return cmd
}
func (k *KraftOptions) Run(_ context.Context, args []string) error {
return pflag.ErrHelp
}
func Main(args []string) int {
cmd := NewCmd()
ctx := signals.SetupSignalContext()
copts := &cli.CliOptions{}
for _, o := range []cli.CliOption{
cli.WithDefaultConfigManager(cmd),
cli.WithDefaultIOStreams(),
cli.WithDefaultPluginManager(),
cli.WithDefaultLogger(),
cli.WithDefaultHTTPClient(),
} {
if err := o(copts); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
// Set up the config manager in the context if it is available
if copts.ConfigManager != nil {
ctx = config.WithConfigManager(ctx, copts.ConfigManager)
}
// Set up the logger in the context if it is available
if copts.Logger != nil {
ctx = log.WithLogger(ctx, copts.Logger)
}
// Set up the iostreams in the context if it is available
if copts.IOStreams != nil {
ctx = iostreams.WithIOStreams(ctx, copts.IOStreams)
}
// Add the kraftkit version to the debug logs
log.G(ctx).Debugf("kraftkit %s", kitversion.Version())
if !config.G[config.KraftKit](ctx).NoCheckUpdates {
if err := kitupdate.Check(ctx); err != nil {
log.G(ctx).Debugf("could not check for updates: %v", err)
log.G(ctx).Debug("")
log.G(ctx).Debug("to turn off this check, set:")
log.G(ctx).Debug("")
log.G(ctx).Debug("\texport KRAFTKIT_NO_CHECK_UPDATES=true")
log.G(ctx).Debug("")
log.G(ctx).Debug("or use the globally accessible flag '--no-check-updates'")
}
}
if err := bootstrap.InitKraftkit(ctx); err != nil {
log.G(ctx).Errorf("could not init kraftkit: %v", err)
os.Exit(1)
}
return cmdfactory.Main(ctx, cmd)
}