From 15f59531680bf8d827fd47bc3112da5045bd4833 Mon Sep 17 00:00:00 2001 From: Raphael Vigee Date: Sun, 28 Apr 2024 22:23:00 +0100 Subject: [PATCH] WIP verb --- bootstrap/rrs.go | 64 ++++++++++++++++++++++++++++++++++++++++++++++++ cmd/heph/root.go | 37 ++++++++++++++++++++++++++++ x/BUILD | 2 +- 3 files changed, 102 insertions(+), 1 deletion(-) diff --git a/bootstrap/rrs.go b/bootstrap/rrs.go index 966b3523..94ae0219 100644 --- a/bootstrap/rrs.go +++ b/bootstrap/rrs.go @@ -12,6 +12,9 @@ import ( "github.com/hephbuild/heph/utils/ads" "github.com/hephbuild/heph/utils/sets" "github.com/hephbuild/heph/worker2/poolwait" + "os" + "path/filepath" + "strings" ) var errHasExprDep = errors.New("has expr, bailing out") @@ -262,3 +265,64 @@ func GenerateRRs(ctx context.Context, e *scheduler.Scheduler, m specs.Matcher, t return generateRRs(ctx, e.Graph, m, targs, opts, false) } + +func GenerateRRsFromVerb(ctx context.Context, cwd, verb, arg string, e *scheduler.Scheduler, opts targetrun.RequestOpts, plain, gen bool) (targetrun.Requests, error) { + var pkgPath string + if filepath.IsAbs(arg) { + pkgPath = arg + } else { + pkgPath = filepath.Join(cwd, arg) + } + + targ := "" + + info, err := os.Stat(pkgPath) + if err != nil { + return nil, err + } + if !info.IsDir() { + targ = filepath.Base(pkgPath) + pkgPath = filepath.Dir(pkgPath) + } + + pkg, err := filepath.Rel(e.Root.Root.Abs(), pkgPath) + if err != nil { + return nil, err + } + if strings.Contains(pkg, "..") { + return nil, fmt.Errorf("not in repo") + } + if pkg == "." { + pkg = "" + } + + matcher := specs.TargetAddr{ + Package: pkg, + Name: verb, + } + + for { + log.Debugf("Attempting to find target %v, arg: %v ", matcher, targ) + + rrs, err := GenerateRRs(ctx, e, matcher, []string{targ}, opts, plain, gen) + if err != nil { + var nferr specs.TargetNotFoundErr + if errors.As(err, &nferr) && nferr.String == matcher.String() { + if matcher.Package == "" { + return nil, fmt.Errorf("not target found for to %v in %v", verb, pkgPath) + } + + targ = filepath.Join(filepath.Base(matcher.Package), targ) + matcher.Package = filepath.Dir(matcher.Package) + if matcher.Package == "." { + matcher.Package = "" + } + continue + } + + return nil, err + } + + return rrs, nil + } +} diff --git a/cmd/heph/root.go b/cmd/heph/root.go index a6804e2d..89050d84 100644 --- a/cmd/heph/root.go +++ b/cmd/heph/root.go @@ -155,6 +155,10 @@ var rootCmd = &cobra.Command{ Version: utils.Version, SilenceUsage: true, SilenceErrors: true, + Args: cobra.ArbitraryArgs, + PreRunE: func(cmd *cobra.Command, args []string) error { + return cmd.PersistentPreRunE(cmd, args) + }, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { lvl, err := log.ParseLevel(*logLevel) if err != nil { @@ -186,6 +190,39 @@ var rootCmd = &cobra.Command{ return nil }, RunE: func(cmd *cobra.Command, args []string) error { + ctx := cmd.Context() + + if len(args) > 0 { + verb := args[0] + arg := "" + if len(args) > 1 { + arg = args[1] + } + + bs, err := schedulerInit(ctx, func(bootstrap.BaseBootstrap) error { + return bootstrap.BlockReadStdin(args) + }) + if err != nil { + return err + } + + rrs, err := bootstrap.GenerateRRsFromVerb(ctx, bs.Cwd, verb, arg, bs.Scheduler, getRROpts(), *plain, !*noGen) + if err != nil { + return err + } + + for _, req := range rrs { + log.Info(req.Target.Addr, req.Args) + } + + err = bootstrap.Run(ctx, bs.Scheduler, rrs, getRunOpts(), true) + if err != nil { + return err + } + + return nil + } + err := cmd.Help() if err != nil { return err diff --git a/x/BUILD b/x/BUILD index 1d6738eb..86296689 100644 --- a/x/BUILD +++ b/x/BUILD @@ -2,7 +2,7 @@ target( name = "noop", - run = "echo ran", + run = "echo $@; echo ran", cache = False, )