Skip to content

Commit

Permalink
Merge pull request #17 from chaitin/image-caller-interface
Browse files Browse the repository at this point in the history
允许直接使用 ID 列表触发插件扫描
  • Loading branch information
aegistudio authored Apr 11, 2022
2 parents 095519a + ee6a231 commit bbb56fd
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
14 changes: 14 additions & 0 deletions go/cmd/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,20 @@ func ScanImage(
return ScanImages(ctx, rang, []api.Image{image}, opts...)
}

// ScanImageIDs with a runtime and a list of IDs provided.
func ScanImageIDs(
ctx context.Context, rang plugin.ExecRange,
runtime api.Runtime, ids []string, opts ...plugin.ExecOption,
) error {
iter, err := plugin.IterateTyped(rang, "image")
if err != nil {
return err
}
return ScanIDs(ctx, iter, runtime, ids,
plugin.WithPrependArgs("--id"),
plugin.WithExecOptions(opts...))
}

// imageExactIDs specifies whether the argument list specifies
// ID instead of searchable names.
var imageExactIDs bool
Expand Down
24 changes: 24 additions & 0 deletions go/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,27 @@ func Scan(
})
return err
}

// ScanIDs attempts to load a root object while passing a list
// of IDs that will be acceptable.
func ScanIDs(
ctx context.Context, iter plugin.ExecIterator,
obj interface{}, ids []string, opts ...plugin.ExecOption,
) error {
objVal := reflect.ValueOf(obj)
objTyp := objVal.Type()
val, ok := partitioners.Load(objTyp)
if !ok {
panic(fmt.Sprintf("undefined partition %q", objTyp))
}
root, pids := val.(partitioner)(objVal)
if len(pids) > 0 {
panic(fmt.Sprintf("invalid root object with ID"))
}
if len(ids) == 0 {
return nil
}
return plugin.Exec(ctx, iter, ids,
plugin.WithPrependArgs("--mode", root.Mode()),
root.Options(), plugin.WithExecOptions(opts...))
}

0 comments on commit bbb56fd

Please sign in to comment.