11package main
22
33import (
4+ "context"
45 _ "embed"
56 "flag"
67 "fmt"
78 "os"
9+ "path/filepath"
810
11+ "github.com/containerd/plugin"
912 "github.com/moby/buildkit/frontend/gateway/grpcclient"
1013 "github.com/moby/buildkit/util/appcontext"
1114 "github.com/moby/buildkit/util/bklog"
1215 "github.com/project-dalec/dalec/frontend"
1316 "github.com/project-dalec/dalec/internal/frontendapi"
14- "github.com/project-dalec/dalec/internal/testrunner "
17+ "github.com/project-dalec/dalec/internal/plugins "
1518 "github.com/sirupsen/logrus"
1619 "google.golang.org/grpc/grpclog"
20+
21+ _ "github.com/project-dalec/dalec/internal/commands"
1722)
1823
1924const (
2025 Package = "github.com/project-dalec/dalec/cmd/frontend"
21-
22- credHelperSubcmd = "credential-helper"
2326)
2427
2528func init () {
@@ -28,43 +31,62 @@ func init() {
2831}
2932
3033func main () {
31- fs := flag .CommandLine
32- fs .Usage = func () {
33- fmt .Fprintf (os .Stderr , `usage: %s [subcommand [args...]]` , os .Args [0 ])
34- }
34+ flags := flag .NewFlagSet (filepath .Base (os .Args [0 ]), flag .ExitOnError )
3535
36- if err := fs .Parse (os .Args ); err != nil {
36+ if err := flags .Parse (os .Args [ 1 :] ); err != nil {
3737 bklog .L .WithError (err ).Fatal ("error parsing frontend args" )
3838 os .Exit (70 ) // 70 is EX_SOFTWARE, meaning internal software error occurred
39+ }
3940
41+ ctx := appcontext .Context ()
42+ if flags .NArg () == 0 {
43+ dalecMain (ctx )
44+ return
4045 }
4146
42- subCmd := fs .Arg (1 )
43-
44- // NOTE: for subcommands we take args[2:]
45- // skip args[0] (the executable) and args[1] (the subcommand)
46-
47- // each "sub-main" function handles its own exit
48- switch subCmd {
49- case credHelperSubcmd :
50- args := flag .Args ()[2 :]
51- gomodMain (args )
52- case testrunner .StepRunnerCmdName :
53- args := flag .Args ()[2 :]
54- testrunner .StepCmd (args )
55- case testrunner .CheckFilesCmdName :
56- args := flag .Args ()[2 :]
57- testrunner .CheckFilesCmd (args )
58- case frontend .TestErrorCmdName :
59- args := flag .Args ()[2 :]
60- frontend .TestErrorCmd (args )
61- default :
62- dalecMain ()
47+ h , err := lookupCmd (ctx , flags .Arg (0 ))
48+ if err != nil {
49+ bklog .L .WithError (err ).Fatal ("error handling command" )
50+ os .Exit (70 ) // 70 is EX_SOFTWARE, meaning internal software error occurred
51+ }
52+
53+ if h == nil {
54+ fmt .Fprintln (os .Stderr , "unknown subcommand:" , flags .Arg (1 ))
55+ fmt .Fprintln (os .Stderr , "full args:" , flag .Args ())
56+ fmt .Fprintln (os .Stderr , "If you see this message this is probably a bug in dalec." )
57+ os .Exit (64 ) // 64 is EX_USAGE, meaning command line usage error
6358 }
59+
60+ h .HandleCmd (ctx , flags .Args ()[1 :])
6461}
6562
66- func dalecMain () {
67- ctx := appcontext .Context ()
63+ func lookupCmd (ctx context.Context , cmd string ) (plugins.CmdHandler , error ) {
64+ set := plugin .NewPluginSet ()
65+ filter := func (r * plugins.Registration ) bool {
66+ return r .Type != plugins .TypeCmd || r .ID != cmd
67+ }
68+
69+ for _ , r := range plugins .Graph (filter ) {
70+ cfg := plugin .NewContext (ctx , set , nil )
71+
72+ p := r .Init (cfg )
73+
74+ v , err := p .Instance ()
75+ if plugin .IsSkipPlugin (err ) {
76+ continue
77+ }
78+
79+ if err != nil {
80+ return nil , err
81+ }
82+
83+ return v .(plugins.CmdHandler ), nil
84+ }
85+
86+ return nil , nil
87+ }
88+
89+ func dalecMain (ctx context.Context ) {
6890 mux , err := frontendapi .NewBuildRouter (ctx )
6991 if err != nil {
7092 bklog .L .WithError (err ).Fatal ("error creating frontend router" )
0 commit comments