1*ba677afaSXin Li// Package builder is used to create tools-golang data structures for a given 2*ba677afaSXin Li// directory path's contents, with hashes, etc. filled in and with empty 3*ba677afaSXin Li// license data. 4*ba677afaSXin Li// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later 5*ba677afaSXin Lipackage builder 6*ba677afaSXin Li 7*ba677afaSXin Liimport ( 8*ba677afaSXin Li "fmt" 9*ba677afaSXin Li 10*ba677afaSXin Li "github.com/spdx/tools-golang/builder/builder2v1" 11*ba677afaSXin Li "github.com/spdx/tools-golang/builder/builder2v2" 12*ba677afaSXin Li "github.com/spdx/tools-golang/builder/builder2v3" 13*ba677afaSXin Li "github.com/spdx/tools-golang/spdx/common" 14*ba677afaSXin Li "github.com/spdx/tools-golang/spdx/v2_1" 15*ba677afaSXin Li "github.com/spdx/tools-golang/spdx/v2_2" 16*ba677afaSXin Li "github.com/spdx/tools-golang/spdx/v2_3" 17*ba677afaSXin Li) 18*ba677afaSXin Li 19*ba677afaSXin Li// ===== 2.1 builder ===== 20*ba677afaSXin Li 21*ba677afaSXin Li// Config2_1 is a collection of configuration settings for builder 22*ba677afaSXin Li// (for version 2.1 SPDX Documents). A few mandatory fields are set here 23*ba677afaSXin Li// so that they can be repeatedly reused in multiple calls to Build2_1. 24*ba677afaSXin Litype Config2_1 struct { 25*ba677afaSXin Li // NamespacePrefix should be a URI representing a prefix for the 26*ba677afaSXin Li // namespace with which the SPDX Document will be associated. 27*ba677afaSXin Li // It will be used in the DocumentNamespace field in the CreationInfo 28*ba677afaSXin Li // section, followed by the per-Document package name and a random UUID. 29*ba677afaSXin Li NamespacePrefix string 30*ba677afaSXin Li 31*ba677afaSXin Li // CreatorType should be one of "Person", "Organization" or "Tool". 32*ba677afaSXin Li // If not one of those strings, it will be interpreted as "Person". 33*ba677afaSXin Li CreatorType string 34*ba677afaSXin Li 35*ba677afaSXin Li // Creator will be filled in for the given CreatorType. 36*ba677afaSXin Li Creator string 37*ba677afaSXin Li 38*ba677afaSXin Li // PathsIgnored lists certain paths to be omitted from the built document. 39*ba677afaSXin Li // Each string should be a path, relative to the package's dirRoot, 40*ba677afaSXin Li // to a specific file or (for all files in a directory) ending in a slash. 41*ba677afaSXin Li // Prefix the string with "**" to omit all instances of that file / 42*ba677afaSXin Li // directory, regardless of where it is in the file tree. 43*ba677afaSXin Li PathsIgnored []string 44*ba677afaSXin Li 45*ba677afaSXin Li // TestValues is used to pass fixed values for testing purposes 46*ba677afaSXin Li // only, and should be set to nil for production use. It is only 47*ba677afaSXin Li // exported so that it will be accessible within builder2v1. 48*ba677afaSXin Li TestValues map[string]string 49*ba677afaSXin Li} 50*ba677afaSXin Li 51*ba677afaSXin Li// Build2_1 creates an SPDX Document (version 2.1), returning that document or 52*ba677afaSXin Li// error if any is encountered. Arguments: 53*ba677afaSXin Li// - packageName: name of package / directory 54*ba677afaSXin Li// - dirRoot: path to directory to be analyzed 55*ba677afaSXin Li// - config: Config object 56*ba677afaSXin Lifunc Build2_1(packageName string, dirRoot string, config *Config2_1) (*v2_1.Document, error) { 57*ba677afaSXin Li // build Package section first -- will include Files and make the 58*ba677afaSXin Li // package verification code available 59*ba677afaSXin Li pkg, err := builder2v1.BuildPackageSection2_1(packageName, dirRoot, config.PathsIgnored) 60*ba677afaSXin Li if err != nil { 61*ba677afaSXin Li return nil, err 62*ba677afaSXin Li } 63*ba677afaSXin Li 64*ba677afaSXin Li ci, err := builder2v1.BuildCreationInfoSection2_1(config.CreatorType, config.Creator, config.TestValues) 65*ba677afaSXin Li if err != nil { 66*ba677afaSXin Li return nil, err 67*ba677afaSXin Li } 68*ba677afaSXin Li 69*ba677afaSXin Li rln, err := builder2v1.BuildRelationshipSection2_1(packageName) 70*ba677afaSXin Li if err != nil { 71*ba677afaSXin Li return nil, err 72*ba677afaSXin Li } 73*ba677afaSXin Li 74*ba677afaSXin Li doc := &v2_1.Document{ 75*ba677afaSXin Li SPDXVersion: "SPDX-2.1", 76*ba677afaSXin Li DataLicense: "CC0-1.0", 77*ba677afaSXin Li SPDXIdentifier: common.ElementID("DOCUMENT"), 78*ba677afaSXin Li DocumentName: packageName, 79*ba677afaSXin Li DocumentNamespace: fmt.Sprintf("%s%s-%s", config.NamespacePrefix, packageName, pkg.PackageVerificationCode), 80*ba677afaSXin Li CreationInfo: ci, 81*ba677afaSXin Li Packages: []*v2_1.Package{pkg}, 82*ba677afaSXin Li Relationships: []*v2_1.Relationship{rln}, 83*ba677afaSXin Li } 84*ba677afaSXin Li 85*ba677afaSXin Li return doc, nil 86*ba677afaSXin Li} 87*ba677afaSXin Li 88*ba677afaSXin Li// ===== 2.2 builder ===== 89*ba677afaSXin Li 90*ba677afaSXin Li// Config2_2 is a collection of configuration settings for builder 91*ba677afaSXin Li// (for version 2.2 SPDX Documents). A few mandatory fields are set here 92*ba677afaSXin Li// so that they can be repeatedly reused in multiple calls to Build2_2. 93*ba677afaSXin Litype Config2_2 struct { 94*ba677afaSXin Li // NamespacePrefix should be a URI representing a prefix for the 95*ba677afaSXin Li // namespace with which the SPDX Document will be associated. 96*ba677afaSXin Li // It will be used in the DocumentNamespace field in the CreationInfo 97*ba677afaSXin Li // section, followed by the per-Document package name and a random UUID. 98*ba677afaSXin Li NamespacePrefix string 99*ba677afaSXin Li 100*ba677afaSXin Li // CreatorType should be one of "Person", "Organization" or "Tool". 101*ba677afaSXin Li // If not one of those strings, it will be interpreted as "Person". 102*ba677afaSXin Li CreatorType string 103*ba677afaSXin Li 104*ba677afaSXin Li // Creator will be filled in for the given CreatorType. 105*ba677afaSXin Li Creator string 106*ba677afaSXin Li 107*ba677afaSXin Li // PathsIgnored lists certain paths to be omitted from the built document. 108*ba677afaSXin Li // Each string should be a path, relative to the package's dirRoot, 109*ba677afaSXin Li // to a specific file or (for all files in a directory) ending in a slash. 110*ba677afaSXin Li // Prefix the string with "**" to omit all instances of that file / 111*ba677afaSXin Li // directory, regardless of where it is in the file tree. 112*ba677afaSXin Li PathsIgnored []string 113*ba677afaSXin Li 114*ba677afaSXin Li // TestValues is used to pass fixed values for testing purposes 115*ba677afaSXin Li // only, and should be set to nil for production use. It is only 116*ba677afaSXin Li // exported so that it will be accessible within builder2v2. 117*ba677afaSXin Li TestValues map[string]string 118*ba677afaSXin Li} 119*ba677afaSXin Li 120*ba677afaSXin Li// Build2_2 creates an SPDX Document (version 2.2), returning that document or 121*ba677afaSXin Li// error if any is encountered. Arguments: 122*ba677afaSXin Li// - packageName: name of package / directory 123*ba677afaSXin Li// - dirRoot: path to directory to be analyzed 124*ba677afaSXin Li// - config: Config object 125*ba677afaSXin Lifunc Build2_2(packageName string, dirRoot string, config *Config2_2) (*v2_2.Document, error) { 126*ba677afaSXin Li // build Package section first -- will include Files and make the 127*ba677afaSXin Li // package verification code available 128*ba677afaSXin Li pkg, err := builder2v2.BuildPackageSection2_2(packageName, dirRoot, config.PathsIgnored) 129*ba677afaSXin Li if err != nil { 130*ba677afaSXin Li return nil, err 131*ba677afaSXin Li } 132*ba677afaSXin Li 133*ba677afaSXin Li ci, err := builder2v2.BuildCreationInfoSection2_2(config.CreatorType, config.Creator, config.TestValues) 134*ba677afaSXin Li if err != nil { 135*ba677afaSXin Li return nil, err 136*ba677afaSXin Li } 137*ba677afaSXin Li 138*ba677afaSXin Li rln, err := builder2v2.BuildRelationshipSection2_2(packageName) 139*ba677afaSXin Li if err != nil { 140*ba677afaSXin Li return nil, err 141*ba677afaSXin Li } 142*ba677afaSXin Li 143*ba677afaSXin Li doc := &v2_2.Document{ 144*ba677afaSXin Li SPDXVersion: "SPDX-2.2", 145*ba677afaSXin Li DataLicense: "CC0-1.0", 146*ba677afaSXin Li SPDXIdentifier: common.ElementID("DOCUMENT"), 147*ba677afaSXin Li DocumentName: packageName, 148*ba677afaSXin Li DocumentNamespace: fmt.Sprintf("%s%s-%s", config.NamespacePrefix, packageName, pkg.PackageVerificationCode), 149*ba677afaSXin Li CreationInfo: ci, 150*ba677afaSXin Li Packages: []*v2_2.Package{pkg}, 151*ba677afaSXin Li Relationships: []*v2_2.Relationship{rln}, 152*ba677afaSXin Li } 153*ba677afaSXin Li 154*ba677afaSXin Li return doc, nil 155*ba677afaSXin Li} 156*ba677afaSXin Li 157*ba677afaSXin Li// ===== 2.3 builder ===== 158*ba677afaSXin Li 159*ba677afaSXin Li// Config2_3 is a collection of configuration settings for builder 160*ba677afaSXin Li// (for version 2.3 SPDX Documents). A few mandatory fields are set here 161*ba677afaSXin Li// so that they can be repeatedly reused in multiple calls to Build2_3. 162*ba677afaSXin Litype Config2_3 struct { 163*ba677afaSXin Li // NamespacePrefix should be a URI representing a prefix for the 164*ba677afaSXin Li // namespace with which the SPDX Document will be associated. 165*ba677afaSXin Li // It will be used in the DocumentNamespace field in the CreationInfo 166*ba677afaSXin Li // section, followed by the per-Document package name and a random UUID. 167*ba677afaSXin Li NamespacePrefix string 168*ba677afaSXin Li 169*ba677afaSXin Li // CreatorType should be one of "Person", "Organization" or "Tool". 170*ba677afaSXin Li // If not one of those strings, it will be interpreted as "Person". 171*ba677afaSXin Li CreatorType string 172*ba677afaSXin Li 173*ba677afaSXin Li // Creator will be filled in for the given CreatorType. 174*ba677afaSXin Li Creator string 175*ba677afaSXin Li 176*ba677afaSXin Li // PathsIgnored lists certain paths to be omitted from the built document. 177*ba677afaSXin Li // Each string should be a path, relative to the package's dirRoot, 178*ba677afaSXin Li // to a specific file or (for all files in a directory) ending in a slash. 179*ba677afaSXin Li // Prefix the string with "**" to omit all instances of that file / 180*ba677afaSXin Li // directory, regardless of where it is in the file tree. 181*ba677afaSXin Li PathsIgnored []string 182*ba677afaSXin Li 183*ba677afaSXin Li // TestValues is used to pass fixed values for testing purposes 184*ba677afaSXin Li // only, and should be set to nil for production use. It is only 185*ba677afaSXin Li // exported so that it will be accessible within builder2v3. 186*ba677afaSXin Li TestValues map[string]string 187*ba677afaSXin Li} 188*ba677afaSXin Li 189*ba677afaSXin Li// Build2_3 creates an SPDX Document (version 2.3), returning that document or 190*ba677afaSXin Li// error if any is encountered. Arguments: 191*ba677afaSXin Li// - packageName: name of package / directory 192*ba677afaSXin Li// - dirRoot: path to directory to be analyzed 193*ba677afaSXin Li// - config: Config object 194*ba677afaSXin Lifunc Build2_3(packageName string, dirRoot string, config *Config2_3) (*v2_3.Document, error) { 195*ba677afaSXin Li // build Package section first -- will include Files and make the 196*ba677afaSXin Li // package verification code available 197*ba677afaSXin Li pkg, err := builder2v3.BuildPackageSection2_3(packageName, dirRoot, config.PathsIgnored) 198*ba677afaSXin Li if err != nil { 199*ba677afaSXin Li return nil, err 200*ba677afaSXin Li } 201*ba677afaSXin Li 202*ba677afaSXin Li ci, err := builder2v3.BuildCreationInfoSection2_3(config.CreatorType, config.Creator, config.TestValues) 203*ba677afaSXin Li if err != nil { 204*ba677afaSXin Li return nil, err 205*ba677afaSXin Li } 206*ba677afaSXin Li 207*ba677afaSXin Li rln, err := builder2v3.BuildRelationshipSection2_3(packageName) 208*ba677afaSXin Li if err != nil { 209*ba677afaSXin Li return nil, err 210*ba677afaSXin Li } 211*ba677afaSXin Li 212*ba677afaSXin Li doc := &v2_3.Document{ 213*ba677afaSXin Li SPDXVersion: "SPDX-2.3", 214*ba677afaSXin Li DataLicense: "CC0-1.0", 215*ba677afaSXin Li SPDXIdentifier: common.ElementID("DOCUMENT"), 216*ba677afaSXin Li DocumentName: packageName, 217*ba677afaSXin Li DocumentNamespace: fmt.Sprintf("%s%s-%s", config.NamespacePrefix, packageName, pkg.PackageVerificationCode), 218*ba677afaSXin Li CreationInfo: ci, 219*ba677afaSXin Li Packages: []*v2_3.Package{pkg}, 220*ba677afaSXin Li Relationships: []*v2_3.Relationship{rln}, 221*ba677afaSXin Li } 222*ba677afaSXin Li 223*ba677afaSXin Li return doc, nil 224*ba677afaSXin Li} 225