Skip to content

Commit

Permalink
Write directly to spdxlicenses files
Browse files Browse the repository at this point in the history
  • Loading branch information
elrayle committed May 13, 2024
1 parent 3d84e52 commit 6d5bf49
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 58 deletions.
36 changes: 16 additions & 20 deletions cmd/exceptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
)

Expand Down Expand Up @@ -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
}
68 changes: 30 additions & 38 deletions cmd/license.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
)

Expand Down Expand Up @@ -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
}

0 comments on commit 6d5bf49

Please sign in to comment.