Skip to content

Commit 6d5bf49

Browse files
committed
Write directly to spdxlicenses files
1 parent 3d84e52 commit 6d5bf49

File tree

2 files changed

+46
-58
lines changed

2 files changed

+46
-58
lines changed

cmd/exceptions.go

+16-20
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package main
33
import (
44
"encoding/json"
55
"fmt"
6-
"io/ioutil"
76
"os"
87
)
98

@@ -42,34 +41,31 @@ func extractExceptionLicenseIDs() error {
4241
}
4342

4443
// create slice of exception license IDs that are not deprecated
45-
var nonDeprecatedExceptionLicenseIDs []string
44+
var exceptionLicenseIDs []string
4645
for _, e := range exceptionData.Exceptions {
4746
if !e.IsDeprecated {
48-
nonDeprecatedExceptionLicenseIDs = append(nonDeprecatedExceptionLicenseIDs, e.LicenseID)
47+
exceptionLicenseIDs = append(exceptionLicenseIDs, e.LicenseID)
4948
}
5049
}
5150

52-
// save non-deprecated license IDs followed by a comma with one per line in file license_ids.txt
53-
nonDeprecatedExceptionLicenseIDsTxt := []byte{}
54-
for _, id := range nonDeprecatedExceptionLicenseIDs {
55-
nonDeprecatedExceptionLicenseIDsTxt = append(nonDeprecatedExceptionLicenseIDsTxt, []byte(" \"")...)
56-
nonDeprecatedExceptionLicenseIDsTxt = append(nonDeprecatedExceptionLicenseIDsTxt, []byte(id)...)
57-
nonDeprecatedExceptionLicenseIDsTxt = append(nonDeprecatedExceptionLicenseIDsTxt, []byte("\",")...)
58-
nonDeprecatedExceptionLicenseIDsTxt = append(nonDeprecatedExceptionLicenseIDsTxt, []byte("\n")...)
59-
}
60-
err = ioutil.WriteFile("exception_ids.txt", nonDeprecatedExceptionLicenseIDsTxt, 0600)
61-
if err != nil {
62-
return err
63-
}
51+
// generate the GetExceptions() function in get_exceptions.go
52+
getExceptionsContents := []byte(`package spdxlicenses
6453
65-
// save non-deprecated license IDs to json array in file exception_ids.json
66-
nonDeprecatedExceptionLicenseIDsJSON, err := json.Marshal(nonDeprecatedExceptionLicenseIDs)
67-
if err != nil {
68-
return err
54+
func GetExceptions() []string {
55+
return []string{
56+
`)
57+
for _, id := range exceptionLicenseIDs {
58+
getExceptionsContents = append(getExceptionsContents, ` "`+id+`",
59+
`...)
6960
}
70-
err = ioutil.WriteFile("exception_ids.json", nonDeprecatedExceptionLicenseIDsJSON, 0600)
61+
getExceptionsContents = append(getExceptionsContents, ` }
62+
}
63+
`...)
64+
65+
err = os.WriteFile("../spdxexp/spdxlicenses/get_exceptions.go", getExceptionsContents, 0600)
7166
if err != nil {
7267
return err
7368
}
69+
7470
return nil
7571
}

cmd/license.go

+30-38
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package main
33
import (
44
"encoding/json"
55
"fmt"
6-
"io/ioutil"
76
"os"
87
)
98

@@ -43,61 +42,54 @@ func extractLicenseIDs() error {
4342
return err
4443
}
4544

46-
// create two slices of license IDs, one for deprecated and one for not deprecated
45+
// create two slices of license IDs, one for deprecated and one for active
46+
var activeLicenseIDs []string
4747
var deprecatedLicenseIDs []string
48-
var nonDeprecatedLicenseIDs []string
4948
for _, l := range licenseData.Licenses {
5049
if l.IsDeprecated {
5150
deprecatedLicenseIDs = append(deprecatedLicenseIDs, l.LicenseID)
5251
} else {
53-
nonDeprecatedLicenseIDs = append(nonDeprecatedLicenseIDs, l.LicenseID)
52+
activeLicenseIDs = append(activeLicenseIDs, l.LicenseID)
5453
}
5554
}
5655

57-
// save deprecated license IDs followed by a comma with one per line in file deprecated_license_ids.txt
58-
deprecatedLicenseIDsTxt := []byte{}
59-
for _, id := range deprecatedLicenseIDs {
60-
deprecatedLicenseIDsTxt = append(deprecatedLicenseIDsTxt, []byte(" \"")...)
61-
deprecatedLicenseIDsTxt = append(deprecatedLicenseIDsTxt, []byte(id)...)
62-
deprecatedLicenseIDsTxt = append(deprecatedLicenseIDsTxt, []byte("\",")...)
63-
deprecatedLicenseIDsTxt = append(deprecatedLicenseIDsTxt, []byte("\n")...)
64-
}
65-
err = ioutil.WriteFile("deprecated_license_ids.txt", deprecatedLicenseIDsTxt, 0600)
66-
if err != nil {
67-
return err
68-
}
56+
// generate the GetLicenses() function in get_licenses.go
57+
getLicensesContents := []byte(`package spdxlicenses
6958
70-
// save deprecated license IDs to json array in file deprecated_license_ids.json
71-
deprecatedLicenseIDsJSON, err := json.Marshal(deprecatedLicenseIDs)
72-
if err != nil {
73-
return err
74-
}
75-
err = ioutil.WriteFile("deprecated_license_ids.json", deprecatedLicenseIDsJSON, 0600)
76-
if err != nil {
77-
return err
59+
func GetLicenses() []string {
60+
return []string{
61+
`)
62+
for _, id := range activeLicenseIDs {
63+
getLicensesContents = append(getLicensesContents, ` "`+id+`",
64+
`...)
7865
}
66+
getLicensesContents = append(getLicensesContents, ` }
67+
}
68+
`...)
7969

80-
// save non-deprecated license IDs followed by a comma with one per line in file license_ids.txt
81-
nonDeprecatedLicenseIDsTxt := []byte{}
82-
for _, id := range nonDeprecatedLicenseIDs {
83-
nonDeprecatedLicenseIDsTxt = append(nonDeprecatedLicenseIDsTxt, []byte(" \"")...)
84-
nonDeprecatedLicenseIDsTxt = append(nonDeprecatedLicenseIDsTxt, []byte(id)...)
85-
nonDeprecatedLicenseIDsTxt = append(nonDeprecatedLicenseIDsTxt, []byte("\",")...)
86-
nonDeprecatedLicenseIDsTxt = append(nonDeprecatedLicenseIDsTxt, []byte("\n")...)
87-
}
88-
err = ioutil.WriteFile("license_ids.txt", nonDeprecatedLicenseIDsTxt, 0600)
70+
err = os.WriteFile("../spdxexp/spdxlicenses/get_licenses.go", getLicensesContents, 0600)
8971
if err != nil {
9072
return err
9173
}
9274

93-
// save non-deprecated license IDs to json array in file license_ids.json
94-
nonDeprecatedLicenseIDsJSON, err := json.Marshal(nonDeprecatedLicenseIDs)
95-
if err != nil {
96-
return err
75+
// generate the GetDeprecated() function in get_deprecated.go
76+
getDeprecatedContents := []byte(`package spdxlicenses
77+
78+
func GetDeprecated() []string {
79+
return []string{
80+
`)
81+
for _, id := range deprecatedLicenseIDs {
82+
getDeprecatedContents = append(getDeprecatedContents, ` "`+id+`",
83+
`...)
9784
}
98-
err = ioutil.WriteFile("license_ids.json", nonDeprecatedLicenseIDsJSON, 0600)
85+
getDeprecatedContents = append(getDeprecatedContents, ` }
86+
}
87+
`...)
88+
89+
err = os.WriteFile("../spdxexp/spdxlicenses/get_deprecated.go", getDeprecatedContents, 0600)
9990
if err != nil {
10091
return err
10192
}
93+
10294
return nil
10395
}

0 commit comments

Comments
 (0)