Skip to content

Commit 15f5953

Browse files
committed
WIP verb
1 parent 57c1fdc commit 15f5953

3 files changed

Lines changed: 102 additions & 1 deletion

File tree

bootstrap/rrs.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ import (
1212
"github.com/hephbuild/heph/utils/ads"
1313
"github.com/hephbuild/heph/utils/sets"
1414
"github.com/hephbuild/heph/worker2/poolwait"
15+
"os"
16+
"path/filepath"
17+
"strings"
1518
)
1619

1720
var errHasExprDep = errors.New("has expr, bailing out")
@@ -262,3 +265,64 @@ func GenerateRRs(ctx context.Context, e *scheduler.Scheduler, m specs.Matcher, t
262265

263266
return generateRRs(ctx, e.Graph, m, targs, opts, false)
264267
}
268+
269+
func GenerateRRsFromVerb(ctx context.Context, cwd, verb, arg string, e *scheduler.Scheduler, opts targetrun.RequestOpts, plain, gen bool) (targetrun.Requests, error) {
270+
var pkgPath string
271+
if filepath.IsAbs(arg) {
272+
pkgPath = arg
273+
} else {
274+
pkgPath = filepath.Join(cwd, arg)
275+
}
276+
277+
targ := ""
278+
279+
info, err := os.Stat(pkgPath)
280+
if err != nil {
281+
return nil, err
282+
}
283+
if !info.IsDir() {
284+
targ = filepath.Base(pkgPath)
285+
pkgPath = filepath.Dir(pkgPath)
286+
}
287+
288+
pkg, err := filepath.Rel(e.Root.Root.Abs(), pkgPath)
289+
if err != nil {
290+
return nil, err
291+
}
292+
if strings.Contains(pkg, "..") {
293+
return nil, fmt.Errorf("not in repo")
294+
}
295+
if pkg == "." {
296+
pkg = ""
297+
}
298+
299+
matcher := specs.TargetAddr{
300+
Package: pkg,
301+
Name: verb,
302+
}
303+
304+
for {
305+
log.Debugf("Attempting to find target %v, arg: %v ", matcher, targ)
306+
307+
rrs, err := GenerateRRs(ctx, e, matcher, []string{targ}, opts, plain, gen)
308+
if err != nil {
309+
var nferr specs.TargetNotFoundErr
310+
if errors.As(err, &nferr) && nferr.String == matcher.String() {
311+
if matcher.Package == "" {
312+
return nil, fmt.Errorf("not target found for to %v in %v", verb, pkgPath)
313+
}
314+
315+
targ = filepath.Join(filepath.Base(matcher.Package), targ)
316+
matcher.Package = filepath.Dir(matcher.Package)
317+
if matcher.Package == "." {
318+
matcher.Package = ""
319+
}
320+
continue
321+
}
322+
323+
return nil, err
324+
}
325+
326+
return rrs, nil
327+
}
328+
}

cmd/heph/root.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,10 @@ var rootCmd = &cobra.Command{
155155
Version: utils.Version,
156156
SilenceUsage: true,
157157
SilenceErrors: true,
158+
Args: cobra.ArbitraryArgs,
159+
PreRunE: func(cmd *cobra.Command, args []string) error {
160+
return cmd.PersistentPreRunE(cmd, args)
161+
},
158162
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
159163
lvl, err := log.ParseLevel(*logLevel)
160164
if err != nil {
@@ -186,6 +190,39 @@ var rootCmd = &cobra.Command{
186190
return nil
187191
},
188192
RunE: func(cmd *cobra.Command, args []string) error {
193+
ctx := cmd.Context()
194+
195+
if len(args) > 0 {
196+
verb := args[0]
197+
arg := ""
198+
if len(args) > 1 {
199+
arg = args[1]
200+
}
201+
202+
bs, err := schedulerInit(ctx, func(bootstrap.BaseBootstrap) error {
203+
return bootstrap.BlockReadStdin(args)
204+
})
205+
if err != nil {
206+
return err
207+
}
208+
209+
rrs, err := bootstrap.GenerateRRsFromVerb(ctx, bs.Cwd, verb, arg, bs.Scheduler, getRROpts(), *plain, !*noGen)
210+
if err != nil {
211+
return err
212+
}
213+
214+
for _, req := range rrs {
215+
log.Info(req.Target.Addr, req.Args)
216+
}
217+
218+
err = bootstrap.Run(ctx, bs.Scheduler, rrs, getRunOpts(), true)
219+
if err != nil {
220+
return err
221+
}
222+
223+
return nil
224+
}
225+
189226
err := cmd.Help()
190227
if err != nil {
191228
return err

x/BUILD

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
target(
44
name = "noop",
5-
run = "echo ran",
5+
run = "echo $@; echo ran",
66
cache = False,
77
)
88

0 commit comments

Comments
 (0)