github.com/CycloneDX/sbom-utility@v0.16.0/cmd/document.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 /* 3 * Licensed to the Apache Software Foundation (ASF) under one or more 4 * contributor license agreements. See the NOTICE file distributed with 5 * this work for additional information regarding copyright ownership. 6 * The ASF licenses this file to You under the Apache License, Version 2.0 7 * (the "License"); you may not use this file except in compliance with 8 * the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 package cmd 20 21 import ( 22 "fmt" 23 24 "github.com/CycloneDX/sbom-utility/schema" 25 "github.com/CycloneDX/sbom-utility/utils" 26 ) 27 28 func LoadInputBOMFileAndDetectSchema() (document *schema.BOM, err error) { 29 getLogger().Enter() 30 defer getLogger().Exit() 31 32 inputFile := utils.GlobalFlags.PersistentFlags.InputFile 33 34 // check for required fields on command 35 getLogger().Tracef("utils.Flags.InputFile: `%s`", inputFile) 36 if inputFile == "" { 37 return nil, fmt.Errorf("invalid input file (-%s): `%s` ", FLAG_FILENAME_INPUT_SHORT, inputFile) 38 } 39 40 // Construct a BOM document object around the input file 41 document = schema.NewBOM(inputFile) 42 43 // Load the raw, candidate BOM (file) as JSON data 44 getLogger().Infof("Attempting to load and unmarshal data from: `%s`...", document.GetFilenameInterpolated()) 45 err = document.UnmarshalBOMAsJSONMap() // i.e., utils.Flags.InputFile 46 if err != nil { 47 return 48 } 49 getLogger().Infof("Successfully unmarshalled data from: `%s`", document.GetFilenameInterpolated()) 50 51 // Search the document keys/values for known BOM formats and schema in the config. file 52 getLogger().Infof("Determining file's BOM format and version...") 53 err = SupportedFormatConfig.FindFormatAndSchema(document) 54 if err != nil { 55 return 56 } 57 58 // Display detected format, version with (optional) schema variant (i.e., if requested on command line) 59 getLogger().Infof("Determined BOM format, version (variant): `%s`, `%s` %s", 60 document.FormatInfo.CanonicalName, 61 document.SchemaInfo.Version, 62 schema.FormatSchemaVariant(document.SchemaInfo.Variant)) 63 getLogger().Infof("Matching BOM schema (for validation): %s", document.SchemaInfo.File) 64 return 65 } 66 67 func LoadBOMFile(inputFile string) (document *schema.BOM, err error) { 68 getLogger().Enter() 69 defer getLogger().Exit() 70 71 if inputFile == "" { 72 return nil, fmt.Errorf("invalid input file (-%s): `%s` ", FLAG_FILENAME_INPUT_SHORT, inputFile) 73 } 74 75 // Construct a BOM document object around the input file 76 document = schema.NewBOM(inputFile) 77 78 // Load the raw, candidate BOM (file) as JSON data 79 getLogger().Infof("Attempting to load and unmarshal data from: `%s`...", document.GetFilenameInterpolated()) 80 err = document.UnmarshalBOMAsJSONMap() // i.e., utils.Flags.InputFile 81 if err != nil { 82 return 83 } 84 getLogger().Infof("Successfully unmarshalled data from: `%s`", document.GetFilenameInterpolated()) 85 86 // Search the document keys/values for known BOM formats and schema in the config. file 87 getLogger().Infof("Determining file's BOM format and version...") 88 err = SupportedFormatConfig.FindFormatAndSchema(document) 89 if err != nil { 90 return 91 } 92 93 // Display detected format, version with (optional) schema variant (i.e., if requested on command line) 94 getLogger().Infof("Determined BOM format, version (variant): `%s`, `%s` %s", 95 document.FormatInfo.CanonicalName, 96 document.SchemaInfo.Version, 97 schema.FormatSchemaVariant(document.SchemaInfo.Variant)) 98 getLogger().Infof("Matching BOM schema (for validation): %s", document.SchemaInfo.File) 99 return 100 }