From 6d5bf49d2a55bc76a582697961398320334ac386 Mon Sep 17 00:00:00 2001 From: "E. Lynette Rayle" Date: Mon, 13 May 2024 18:41:26 -0400 Subject: [PATCH] Write directly to spdxlicenses files --- cmd/exceptions.go | 36 +++++++++++-------------- cmd/license.go | 68 +++++++++++++++++++++-------------------------- 2 files changed, 46 insertions(+), 58 deletions(-) diff --git a/cmd/exceptions.go b/cmd/exceptions.go index 2dab64b..bbafc30 100644 --- a/cmd/exceptions.go +++ b/cmd/exceptions.go @@ -3,7 +3,6 @@ package main import ( "encoding/json" "fmt" - "io/ioutil" "os" ) @@ -42,34 +41,31 @@ func extractExceptionLicenseIDs() error { } // create slice of exception license IDs that are not deprecated - var nonDeprecatedExceptionLicenseIDs []string + var exceptionLicenseIDs []string for _, e := range exceptionData.Exceptions { if !e.IsDeprecated { - nonDeprecatedExceptionLicenseIDs = append(nonDeprecatedExceptionLicenseIDs, e.LicenseID) + exceptionLicenseIDs = append(exceptionLicenseIDs, e.LicenseID) } } - // save non-deprecated license IDs followed by a comma with one per line in file license_ids.txt - nonDeprecatedExceptionLicenseIDsTxt := []byte{} - for _, id := range nonDeprecatedExceptionLicenseIDs { - nonDeprecatedExceptionLicenseIDsTxt = append(nonDeprecatedExceptionLicenseIDsTxt, []byte(" \"")...) - nonDeprecatedExceptionLicenseIDsTxt = append(nonDeprecatedExceptionLicenseIDsTxt, []byte(id)...) - nonDeprecatedExceptionLicenseIDsTxt = append(nonDeprecatedExceptionLicenseIDsTxt, []byte("\",")...) - nonDeprecatedExceptionLicenseIDsTxt = append(nonDeprecatedExceptionLicenseIDsTxt, []byte("\n")...) - } - err = ioutil.WriteFile("exception_ids.txt", nonDeprecatedExceptionLicenseIDsTxt, 0600) - if err != nil { - return err - } + // generate the GetExceptions() function in get_exceptions.go + getExceptionsContents := []byte(`package spdxlicenses - // save non-deprecated license IDs to json array in file exception_ids.json - nonDeprecatedExceptionLicenseIDsJSON, err := json.Marshal(nonDeprecatedExceptionLicenseIDs) - if err != nil { - return err + func GetExceptions() []string { + return []string{ + `) + for _, id := range exceptionLicenseIDs { + getExceptionsContents = append(getExceptionsContents, ` "`+id+`", +`...) } - err = ioutil.WriteFile("exception_ids.json", nonDeprecatedExceptionLicenseIDsJSON, 0600) + getExceptionsContents = append(getExceptionsContents, ` } +} +`...) + + err = os.WriteFile("../spdxexp/spdxlicenses/get_exceptions.go", getExceptionsContents, 0600) if err != nil { return err } + return nil } diff --git a/cmd/license.go b/cmd/license.go index b404c02..6d96bd3 100644 --- a/cmd/license.go +++ b/cmd/license.go @@ -3,7 +3,6 @@ package main import ( "encoding/json" "fmt" - "io/ioutil" "os" ) @@ -43,61 +42,54 @@ func extractLicenseIDs() error { return err } - // create two slices of license IDs, one for deprecated and one for not deprecated + // create two slices of license IDs, one for deprecated and one for active + var activeLicenseIDs []string var deprecatedLicenseIDs []string - var nonDeprecatedLicenseIDs []string for _, l := range licenseData.Licenses { if l.IsDeprecated { deprecatedLicenseIDs = append(deprecatedLicenseIDs, l.LicenseID) } else { - nonDeprecatedLicenseIDs = append(nonDeprecatedLicenseIDs, l.LicenseID) + activeLicenseIDs = append(activeLicenseIDs, l.LicenseID) } } - // save deprecated license IDs followed by a comma with one per line in file deprecated_license_ids.txt - deprecatedLicenseIDsTxt := []byte{} - for _, id := range deprecatedLicenseIDs { - deprecatedLicenseIDsTxt = append(deprecatedLicenseIDsTxt, []byte(" \"")...) - deprecatedLicenseIDsTxt = append(deprecatedLicenseIDsTxt, []byte(id)...) - deprecatedLicenseIDsTxt = append(deprecatedLicenseIDsTxt, []byte("\",")...) - deprecatedLicenseIDsTxt = append(deprecatedLicenseIDsTxt, []byte("\n")...) - } - err = ioutil.WriteFile("deprecated_license_ids.txt", deprecatedLicenseIDsTxt, 0600) - if err != nil { - return err - } + // generate the GetLicenses() function in get_licenses.go + getLicensesContents := []byte(`package spdxlicenses - // save deprecated license IDs to json array in file deprecated_license_ids.json - deprecatedLicenseIDsJSON, err := json.Marshal(deprecatedLicenseIDs) - if err != nil { - return err - } - err = ioutil.WriteFile("deprecated_license_ids.json", deprecatedLicenseIDsJSON, 0600) - if err != nil { - return err + func GetLicenses() []string { + return []string{ + `) + for _, id := range activeLicenseIDs { + getLicensesContents = append(getLicensesContents, ` "`+id+`", +`...) } + getLicensesContents = append(getLicensesContents, ` } +} +`...) - // save non-deprecated license IDs followed by a comma with one per line in file license_ids.txt - nonDeprecatedLicenseIDsTxt := []byte{} - for _, id := range nonDeprecatedLicenseIDs { - nonDeprecatedLicenseIDsTxt = append(nonDeprecatedLicenseIDsTxt, []byte(" \"")...) - nonDeprecatedLicenseIDsTxt = append(nonDeprecatedLicenseIDsTxt, []byte(id)...) - nonDeprecatedLicenseIDsTxt = append(nonDeprecatedLicenseIDsTxt, []byte("\",")...) - nonDeprecatedLicenseIDsTxt = append(nonDeprecatedLicenseIDsTxt, []byte("\n")...) - } - err = ioutil.WriteFile("license_ids.txt", nonDeprecatedLicenseIDsTxt, 0600) + err = os.WriteFile("../spdxexp/spdxlicenses/get_licenses.go", getLicensesContents, 0600) if err != nil { return err } - // save non-deprecated license IDs to json array in file license_ids.json - nonDeprecatedLicenseIDsJSON, err := json.Marshal(nonDeprecatedLicenseIDs) - if err != nil { - return err + // generate the GetDeprecated() function in get_deprecated.go + getDeprecatedContents := []byte(`package spdxlicenses + + func GetDeprecated() []string { + return []string{ + `) + for _, id := range deprecatedLicenseIDs { + getDeprecatedContents = append(getDeprecatedContents, ` "`+id+`", +`...) } - err = ioutil.WriteFile("license_ids.json", nonDeprecatedLicenseIDsJSON, 0600) + getDeprecatedContents = append(getDeprecatedContents, ` } +} +`...) + + err = os.WriteFile("../spdxexp/spdxlicenses/get_deprecated.go", getDeprecatedContents, 0600) if err != nil { return err } + return nil }