Skip to content

Commit

Permalink
readme: Read Cargo.toml (close #39)
Browse files Browse the repository at this point in the history
  • Loading branch information
kemitchell committed Nov 14, 2019
1 parent 82ff1af commit 7de4481
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 15 deletions.
53 changes: 50 additions & 3 deletions inventory/cargo.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
19 changes: 19 additions & 0 deletions inventory/generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package inventory

import "encoding/json"
import "github.com/yookoala/realpath"
import "errors"
import "io/ioutil"
import "os"
import "path"
Expand Down Expand Up @@ -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
Expand Down
20 changes: 10 additions & 10 deletions inventory/inventory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions subcommands/readme.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 7de4481

Please sign in to comment.