github.com/quay/claircore@v1.5.28/docs/libvuln_test.go (about)

     1  package docs
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"net/http"
     7  
     8  	"github.com/quay/claircore"
     9  	"github.com/quay/claircore/libindex"
    10  	"github.com/quay/claircore/libvuln"
    11  )
    12  
    13  func Example_libvuln() {
    14  	ctx := context.TODO()
    15  
    16  	// ANCHOR: new
    17  	opts := new(libvuln.Options)
    18  	// Populate with desired settings...
    19  	lib, err := libvuln.New(ctx, opts)
    20  	if err != nil {
    21  		panic(err)
    22  	}
    23  	defer lib.Close(ctx)
    24  	// ANCHOR_END: new
    25  
    26  	liopts := new(libindex.Options)
    27  	// Populate with desired settings...
    28  	indexer, err := libindex.New(ctx, liopts, http.DefaultClient)
    29  	if err != nil {
    30  		panic(err)
    31  	}
    32  	defer indexer.Close(ctx)
    33  	// ANCHOR: scan
    34  	m := new(claircore.Manifest)
    35  	// Populate somehow ...
    36  	ir, err := indexer.Index(ctx, m)
    37  	if err != nil {
    38  		panic(err)
    39  	}
    40  	vr, err := lib.Scan(ctx, ir)
    41  	if err != nil {
    42  		panic(err)
    43  	}
    44  	// ANCHOR_END: scan
    45  	_ = vr
    46  
    47  	// ANCHOR: ops
    48  	ops, err := lib.UpdateOperations(ctx, `updater`)
    49  	if err != nil {
    50  		panic(err)
    51  	}
    52  	// ANCHOR_END: ops
    53  	// ANCHOR: ops_print
    54  	for updater, ops := range ops {
    55  		fmt.Printf("ops for updater %s, %+v", updater, ops)
    56  	}
    57  	// ANCHOR_END: ops_print
    58  	// ANCHOR: ops_diff
    59  	for upd, ops := range ops {
    60  		fmt.Printf("updater: %v", upd)
    61  		diff, err := lib.UpdateDiff(ctx, ops[1].Ref, ops[0].Ref)
    62  		if err != nil {
    63  			panic(err)
    64  		}
    65  		for _, vuln := range diff.Added {
    66  			fmt.Printf("vuln %+v added in %v", vuln, diff.Cur.Ref)
    67  		}
    68  	}
    69  	// ANCHOR_END: ops_diff
    70  }