github.com/quay/claircore@v1.5.28/docs/libindex_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  )
    11  
    12  func Example_libindex() {
    13  	ctx := context.TODO()
    14  
    15  	// ANCHOR: new
    16  	opts := new(libindex.Options)
    17  	// Populate with desired settings...
    18  	lib, err := libindex.New(ctx, opts, http.DefaultClient)
    19  	if err != nil {
    20  		panic(err)
    21  	}
    22  	defer lib.Close(ctx) // Remember to cleanup when done.
    23  	// ANCHOR_END: new
    24  
    25  	// ANCHOR: index
    26  	m := new(claircore.Manifest)
    27  	// Populate somehow ...
    28  	ir, err := lib.Index(ctx, m)
    29  	if err != nil {
    30  		panic(err)
    31  	}
    32  	// ANCHOR_END: index
    33  	_ = ir
    34  
    35  	// ANCHOR: indexreport
    36  	ir, ok, err := lib.IndexReport(ctx, m.Hash)
    37  	if err != nil {
    38  		panic(err)
    39  	}
    40  	// ANCHOR_END: indexreport
    41  	_ = ok
    42  
    43  	var prevState string
    44  	// ANCHOR: state
    45  	state, err := lib.State(ctx)
    46  	if err != nil {
    47  		panic(err)
    48  	}
    49  	if state == prevState {
    50  		// Nothing to do.
    51  		return
    52  	}
    53  	// Otherwise, re-index manifest.
    54  	// ANCHOR_END: state
    55  
    56  	// ANCHOR: affectedmanifests
    57  	var vulns []claircore.Vulnerability
    58  	affected, err := lib.AffectedManifests(ctx, vulns)
    59  	if err != nil {
    60  		panic(err)
    61  	}
    62  	for manifest, vulns := range affected.VulnerableManifests {
    63  		for _, vuln := range vulns {
    64  			fmt.Printf("vuln affecting manifest %s: %+v", manifest, vuln)
    65  		}
    66  	}
    67  	// ANCHOR_END: affectedmanifests
    68  }