-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathmain.go
More file actions
99 lines (79 loc) · 2.56 KB
/
main.go
File metadata and controls
99 lines (79 loc) · 2.56 KB
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
package main
import (
"context"
_ "embed"
"flag"
"fmt"
"os"
"path/filepath"
"github.com/containerd/plugin"
"github.com/moby/buildkit/frontend/gateway/grpcclient"
"github.com/moby/buildkit/util/appcontext"
"github.com/moby/buildkit/util/bklog"
"github.com/project-dalec/dalec/frontend"
"github.com/project-dalec/dalec/internal/frontendapi"
"github.com/project-dalec/dalec/internal/plugins"
"github.com/sirupsen/logrus"
"google.golang.org/grpc/grpclog"
_ "github.com/project-dalec/dalec/internal/commands"
)
const (
Package = "github.com/project-dalec/dalec/cmd/frontend"
)
func init() {
bklog.L.Logger.SetOutput(os.Stderr)
grpclog.SetLoggerV2(grpclog.NewLoggerV2WithVerbosity(bklog.L.WriterLevel(logrus.InfoLevel), bklog.L.WriterLevel(logrus.WarnLevel), bklog.L.WriterLevel(logrus.ErrorLevel), 1))
}
func main() {
flags := flag.NewFlagSet(filepath.Base(os.Args[0]), flag.ExitOnError)
if err := flags.Parse(os.Args[1:]); err != nil {
bklog.L.WithError(err).Fatal("error parsing frontend args")
os.Exit(70) // 70 is EX_SOFTWARE, meaning internal software error occurred
}
ctx := appcontext.Context()
if flags.NArg() == 0 {
dalecMain(ctx)
return
}
h, err := lookupCmd(ctx, flags.Arg(0))
if err != nil {
bklog.L.WithError(err).Fatal("error handling command")
os.Exit(70) // 70 is EX_SOFTWARE, meaning internal software error occurred
}
if h == nil {
fmt.Fprintln(os.Stderr, "unknown subcommand:", flags.Arg(1))
fmt.Fprintln(os.Stderr, "full args:", flag.Args())
fmt.Fprintln(os.Stderr, "If you see this message this is probably a bug in dalec.")
os.Exit(64) // 64 is EX_USAGE, meaning command line usage error
}
h.HandleCmd(ctx, flags.Args()[1:])
}
func lookupCmd(ctx context.Context, cmd string) (plugins.CmdHandler, error) {
set := plugin.NewPluginSet()
filter := func(r *plugins.Registration) bool {
return r.Type != plugins.TypeCmd || r.ID != cmd
}
for _, r := range plugins.Graph(filter) {
cfg := plugin.NewContext(ctx, set, nil)
p := r.Init(cfg)
v, err := p.Instance()
if plugin.IsSkipPlugin(err) {
continue
}
if err != nil {
return nil, err
}
return v.(plugins.CmdHandler), nil
}
return nil, nil
}
func dalecMain(ctx context.Context) {
mux, err := frontendapi.NewBuildRouter(ctx)
if err != nil {
bklog.L.WithError(err).Fatal("error creating frontend router")
}
if err := grpcclient.RunFromEnvironment(ctx, mux.Handler(frontend.WithTargetForwardingHandler)); err != nil {
bklog.L.WithError(err).Fatal("error running frontend")
os.Exit(70) // 70 is EX_SOFTWARE, meaning internal software error occurred
}
}