github.com/noqcks/syft@v0.0.0-20230920222752-a9e2c4e288e5/syft/pkg/cataloger/common/cpe/dictionary/index-generator/main.go (about)

     1  // This program downloads the latest CPE dictionary from NIST and processes it into a JSON file that can be embedded into Syft for more accurate CPE results.
     2  package main
     3  
     4  import (
     5  	"errors"
     6  	"flag"
     7  	"fmt"
     8  	"log"
     9  	"net/http"
    10  	"os"
    11  )
    12  
    13  func mainE() error {
    14  	var outputFilename string
    15  	flag.StringVar(&outputFilename, "o", "", "file location to save CPE index")
    16  	flag.Parse()
    17  
    18  	if outputFilename == "" {
    19  		return errors.New("-o is required")
    20  	}
    21  
    22  	// Download and decompress file
    23  	fmt.Println("Fetching CPE dictionary...")
    24  	resp, err := http.Get(cpeDictionaryURL)
    25  	if err != nil {
    26  		return fmt.Errorf("unable to get CPE dictionary: %w", err)
    27  	}
    28  	defer resp.Body.Close()
    29  
    30  	fmt.Println("Generating index...")
    31  	dictionaryJSON, err := generateIndexedDictionaryJSON(resp.Body)
    32  	if err != nil {
    33  		return err
    34  	}
    35  
    36  	// Write CPE index (JSON data) to disk
    37  	err = os.WriteFile(outputFilename, dictionaryJSON, 0600)
    38  	if err != nil {
    39  		return fmt.Errorf("unable to write processed CPE dictionary to file: %w", err)
    40  	}
    41  
    42  	fmt.Println("Done!")
    43  
    44  	return nil
    45  }
    46  
    47  // errExit prints an error and exits with a non-zero exit code.
    48  func errExit(err error) {
    49  	log.Printf("command failed: %s", err)
    50  	os.Exit(1)
    51  }
    52  
    53  func main() {
    54  	if err := mainE(); err != nil {
    55  		errExit(err)
    56  	}
    57  }