golang.org/x/tools/gopls@v0.15.3/internal/vulncheck/govulncheck/handler.go (about)

     1  // Copyright 2023 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Code generated by copying from golang.org/x/vuln@v1.0.1 (go run copier.go); DO NOT EDIT.
     6  
     7  package govulncheck
     8  
     9  import (
    10  	"encoding/json"
    11  	"io"
    12  
    13  	"golang.org/x/tools/gopls/internal/vulncheck/osv"
    14  )
    15  
    16  // Handler handles messages to be presented in a vulnerability scan output
    17  // stream.
    18  type Handler interface {
    19  	// Config communicates introductory message to the user.
    20  	Config(config *Config) error
    21  
    22  	// Progress is called to display a progress message.
    23  	Progress(progress *Progress) error
    24  
    25  	// OSV is invoked for each osv Entry in the stream.
    26  	OSV(entry *osv.Entry) error
    27  
    28  	// Finding is called for each vulnerability finding in the stream.
    29  	Finding(finding *Finding) error
    30  }
    31  
    32  // HandleJSON reads the json from the supplied stream and hands the decoded
    33  // output to the handler.
    34  func HandleJSON(from io.Reader, to Handler) error {
    35  	dec := json.NewDecoder(from)
    36  	for dec.More() {
    37  		msg := Message{}
    38  		// decode the next message in the stream
    39  		if err := dec.Decode(&msg); err != nil {
    40  			return err
    41  		}
    42  		// dispatch the message
    43  		var err error
    44  		if msg.Config != nil {
    45  			err = to.Config(msg.Config)
    46  		}
    47  		if msg.Progress != nil {
    48  			err = to.Progress(msg.Progress)
    49  		}
    50  		if msg.OSV != nil {
    51  			err = to.OSV(msg.OSV)
    52  		}
    53  		if msg.Finding != nil {
    54  			err = to.Finding(msg.Finding)
    55  		}
    56  		if err != nil {
    57  			return err
    58  		}
    59  	}
    60  	return nil
    61  }