diff --git a/inventory/cargo.go b/inventory/cargo.go index f62f355..4de413a 100644 --- a/inventory/cargo.go +++ b/inventory/cargo.go @@ -1,9 +1,15 @@ package inventory +import "github.com/yookoala/realpath" +import "fmt" +import "github.com/BurntSushi/toml" +import "os" import "encoding/json" import "os/exec" +import "errors" import "bytes" import "path" +import "io/ioutil" func readCargoCrates(packagePath string) ([]Project, error) { var returned []Project @@ -31,13 +37,14 @@ func readCargoCrates(packagePath string) ([]Project, error) { return returned, nil } +// CargoLicenseZeroMap describes metadata for Cargo crates. type CargoLicenseZeroMap struct { - Version string `json:"version"` - Envelopes []ProjectManifestEnvelope `json:"ids"` + Version string `json:"version" toml:"version"` + Envelopes []ProjectManifestEnvelope `json:"ids" toml:"ids"` } type cargoMetadataMap struct { - LicenseZero CargoLicenseZeroMap `json:"licensezero"` + LicenseZero CargoLicenseZeroMap `json:"licensezero" toml:"licensezero"` } type cargoMetadataPackage struct { @@ -67,3 +74,43 @@ func cargoReadMetadata(packagePath string) (*cargoMetadataOutput, error) { } return &parsed, nil } + +type cargoTOMLData struct { + Package cargoTOMLPackage `toml:"package"` +} + +type cargoTOMLPackage struct { + Name string `toml:"name"` + Version string `toml:"version"` + Metadata cargoMetadataMap `toml:"metadata"` +} + +// ReadCargoTOML reads metadata from Cargo.toml. +func ReadCargoTOML(directoryPath string) ([]Project, error) { + var returned []Project + cargoTOML := path.Join(directoryPath, "Cargo.toml") + data, err := ioutil.ReadFile(cargoTOML) + if err != nil { + return nil, err + } + var parsed cargoTOMLData + if _, err := toml.Decode(string(data), &parsed); err != nil { + return nil, errors.New("could not parse Cargo.toml") + } + fmt.Printf("%+v\n", parsed) + for _, envelope := range parsed.Package.Metadata.LicenseZero.Envelopes { + project := Project{ + Path: directoryPath, + Envelope: envelope, + } + os.Stdout.WriteString(project.Envelope.Manifest.ProjectID) + realDirectory, err := realpath.Realpath(directoryPath) + if err != nil { + project.Path = realDirectory + } else { + project.Path = directoryPath + } + returned = append(returned, project) + } + return returned, nil +} diff --git a/inventory/generic.go b/inventory/generic.go index 9e5ab0c..a67b96b 100644 --- a/inventory/generic.go +++ b/inventory/generic.go @@ -2,6 +2,7 @@ package inventory import "encoding/json" import "github.com/yookoala/realpath" +import "errors" import "io/ioutil" import "os" import "path" @@ -69,6 +70,24 @@ func findPackageInfo(directoryPath string) *Project { return nil } +// ReadLocalProjects reads project metadata from various files. +func ReadLocalProjects(directoryPath string) ([]Project, error) { + var results []Project + var hadResults = 0 + var readerFunctions = []func(string) ([]Project, error){ReadLicenseZeroJSON, ReadCargoTOML} + for _, readerFunction := range readerFunctions { + projects, err := readerFunction(directoryPath) + if err == nil { + hadResults = hadResults + 1 + results = projects + } + } + if hadResults > 1 { + return nil, errors.New("multiple metadata files") + } + return results, nil +} + // ReadLicenseZeroJSON read metadata from licensezero.json. func ReadLicenseZeroJSON(directoryPath string) ([]Project, error) { var returned []Project diff --git a/inventory/inventory.go b/inventory/inventory.go index 3f7c41b..720712a 100644 --- a/inventory/inventory.go +++ b/inventory/inventory.go @@ -17,22 +17,22 @@ type Project struct { // ProjectManifestEnvelope describes a signed project manifest. type ProjectManifestEnvelope struct { - LicensorSignature string `json:"licensorSignature"` - AgentSignature string `json:"agentSignature"` - Manifest ProjectManifest `json:"license"` + LicensorSignature string `json:"licensorSignature" toml:"licensorSignature"` + AgentSignature string `json:"agentSignature" toml:"agentSignature"` + Manifest ProjectManifest `json:"license" toml:"license"` } // ProjectManifest describes contribution set data from licensezero.json. type ProjectManifest struct { // Note: These declaration must appear in the order so as to // serialize in the correct order for signature verification. - Repository string `json:"homepage"` - Jurisdiction string `json:"jurisdiction"` - Name string `json:"name"` - ProjectID string `json:"projectID"` - PublicKey string `json:"publicKey"` - Terms string `json:"terms"` - Version string `json:"version"` + Repository string `json:"homepage" toml:"homepage"` + Jurisdiction string `json:"jurisdiction" tom:"jurisdiction"` + Name string `json:"name" toml:"name"` + ProjectID string `json:"projectID" toml:"projectID"` + PublicKey string `json:"publicKey" toml:"publicKey"` + Terms string `json:"terms" toml:"terms"` + Version string `json:"version" toml:"version"` } // Projects describes the categorization of projects in inventory. diff --git a/subcommands/readme.go b/subcommands/readme.go index d8ba6cb..c88dddb 100644 --- a/subcommands/readme.go +++ b/subcommands/readme.go @@ -36,7 +36,7 @@ var README = &Subcommand{ } existing = existing + "# Licensing" if len(projectIDs) == 0 { - Fail("No License Zero project metadata in licensezero.json.") + Fail("No License Zero project metadata.") } haveReciprocal := false haveNoncommercial := false @@ -149,7 +149,7 @@ func twoOrMore(values []bool) bool { } func readEntries(directory string) ([]string, []string, error) { - projects, err := inventory.ReadLicenseZeroJSON(directory) + var projects, err = inventory.ReadLocalProjects(directory) if err != nil { return nil, nil, err }