Skip to content

Commit 7de4481

Browse files
committed
readme: Read Cargo.toml (close #39)
1 parent 82ff1af commit 7de4481

4 files changed

Lines changed: 81 additions & 15 deletions

File tree

inventory/cargo.go

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
package inventory
22

3+
import "github.com/yookoala/realpath"
4+
import "fmt"
5+
import "github.com/BurntSushi/toml"
6+
import "os"
37
import "encoding/json"
48
import "os/exec"
9+
import "errors"
510
import "bytes"
611
import "path"
12+
import "io/ioutil"
713

814
func readCargoCrates(packagePath string) ([]Project, error) {
915
var returned []Project
@@ -31,13 +37,14 @@ func readCargoCrates(packagePath string) ([]Project, error) {
3137
return returned, nil
3238
}
3339

40+
// CargoLicenseZeroMap describes metadata for Cargo crates.
3441
type CargoLicenseZeroMap struct {
35-
Version string `json:"version"`
36-
Envelopes []ProjectManifestEnvelope `json:"ids"`
42+
Version string `json:"version" toml:"version"`
43+
Envelopes []ProjectManifestEnvelope `json:"ids" toml:"ids"`
3744
}
3845

3946
type cargoMetadataMap struct {
40-
LicenseZero CargoLicenseZeroMap `json:"licensezero"`
47+
LicenseZero CargoLicenseZeroMap `json:"licensezero" toml:"licensezero"`
4148
}
4249

4350
type cargoMetadataPackage struct {
@@ -67,3 +74,43 @@ func cargoReadMetadata(packagePath string) (*cargoMetadataOutput, error) {
6774
}
6875
return &parsed, nil
6976
}
77+
78+
type cargoTOMLData struct {
79+
Package cargoTOMLPackage `toml:"package"`
80+
}
81+
82+
type cargoTOMLPackage struct {
83+
Name string `toml:"name"`
84+
Version string `toml:"version"`
85+
Metadata cargoMetadataMap `toml:"metadata"`
86+
}
87+
88+
// ReadCargoTOML reads metadata from Cargo.toml.
89+
func ReadCargoTOML(directoryPath string) ([]Project, error) {
90+
var returned []Project
91+
cargoTOML := path.Join(directoryPath, "Cargo.toml")
92+
data, err := ioutil.ReadFile(cargoTOML)
93+
if err != nil {
94+
return nil, err
95+
}
96+
var parsed cargoTOMLData
97+
if _, err := toml.Decode(string(data), &parsed); err != nil {
98+
return nil, errors.New("could not parse Cargo.toml")
99+
}
100+
fmt.Printf("%+v\n", parsed)
101+
for _, envelope := range parsed.Package.Metadata.LicenseZero.Envelopes {
102+
project := Project{
103+
Path: directoryPath,
104+
Envelope: envelope,
105+
}
106+
os.Stdout.WriteString(project.Envelope.Manifest.ProjectID)
107+
realDirectory, err := realpath.Realpath(directoryPath)
108+
if err != nil {
109+
project.Path = realDirectory
110+
} else {
111+
project.Path = directoryPath
112+
}
113+
returned = append(returned, project)
114+
}
115+
return returned, nil
116+
}

inventory/generic.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package inventory
22

33
import "encoding/json"
44
import "github.com/yookoala/realpath"
5+
import "errors"
56
import "io/ioutil"
67
import "os"
78
import "path"
@@ -69,6 +70,24 @@ func findPackageInfo(directoryPath string) *Project {
6970
return nil
7071
}
7172

73+
// ReadLocalProjects reads project metadata from various files.
74+
func ReadLocalProjects(directoryPath string) ([]Project, error) {
75+
var results []Project
76+
var hadResults = 0
77+
var readerFunctions = []func(string) ([]Project, error){ReadLicenseZeroJSON, ReadCargoTOML}
78+
for _, readerFunction := range readerFunctions {
79+
projects, err := readerFunction(directoryPath)
80+
if err == nil {
81+
hadResults = hadResults + 1
82+
results = projects
83+
}
84+
}
85+
if hadResults > 1 {
86+
return nil, errors.New("multiple metadata files")
87+
}
88+
return results, nil
89+
}
90+
7291
// ReadLicenseZeroJSON read metadata from licensezero.json.
7392
func ReadLicenseZeroJSON(directoryPath string) ([]Project, error) {
7493
var returned []Project

inventory/inventory.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,22 @@ type Project struct {
1717

1818
// ProjectManifestEnvelope describes a signed project manifest.
1919
type ProjectManifestEnvelope struct {
20-
LicensorSignature string `json:"licensorSignature"`
21-
AgentSignature string `json:"agentSignature"`
22-
Manifest ProjectManifest `json:"license"`
20+
LicensorSignature string `json:"licensorSignature" toml:"licensorSignature"`
21+
AgentSignature string `json:"agentSignature" toml:"agentSignature"`
22+
Manifest ProjectManifest `json:"license" toml:"license"`
2323
}
2424

2525
// ProjectManifest describes contribution set data from licensezero.json.
2626
type ProjectManifest struct {
2727
// Note: These declaration must appear in the order so as to
2828
// serialize in the correct order for signature verification.
29-
Repository string `json:"homepage"`
30-
Jurisdiction string `json:"jurisdiction"`
31-
Name string `json:"name"`
32-
ProjectID string `json:"projectID"`
33-
PublicKey string `json:"publicKey"`
34-
Terms string `json:"terms"`
35-
Version string `json:"version"`
29+
Repository string `json:"homepage" toml:"homepage"`
30+
Jurisdiction string `json:"jurisdiction" tom:"jurisdiction"`
31+
Name string `json:"name" toml:"name"`
32+
ProjectID string `json:"projectID" toml:"projectID"`
33+
PublicKey string `json:"publicKey" toml:"publicKey"`
34+
Terms string `json:"terms" toml:"terms"`
35+
Version string `json:"version" toml:"version"`
3636
}
3737

3838
// Projects describes the categorization of projects in inventory.

subcommands/readme.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ var README = &Subcommand{
3636
}
3737
existing = existing + "# Licensing"
3838
if len(projectIDs) == 0 {
39-
Fail("No License Zero project metadata in licensezero.json.")
39+
Fail("No License Zero project metadata.")
4040
}
4141
haveReciprocal := false
4242
haveNoncommercial := false
@@ -149,7 +149,7 @@ func twoOrMore(values []bool) bool {
149149
}
150150

151151
func readEntries(directory string) ([]string, []string, error) {
152-
projects, err := inventory.ReadLicenseZeroJSON(directory)
152+
var projects, err = inventory.ReadLocalProjects(directory)
153153
if err != nil {
154154
return nil, nil, err
155155
}

0 commit comments

Comments
 (0)