|
1 | 1 | package inventory |
2 | 2 |
|
| 3 | +import "github.com/yookoala/realpath" |
| 4 | +import "fmt" |
| 5 | +import "github.com/BurntSushi/toml" |
| 6 | +import "os" |
3 | 7 | import "encoding/json" |
4 | 8 | import "os/exec" |
| 9 | +import "errors" |
5 | 10 | import "bytes" |
6 | 11 | import "path" |
| 12 | +import "io/ioutil" |
7 | 13 |
|
8 | 14 | func readCargoCrates(packagePath string) ([]Project, error) { |
9 | 15 | var returned []Project |
@@ -31,13 +37,14 @@ func readCargoCrates(packagePath string) ([]Project, error) { |
31 | 37 | return returned, nil |
32 | 38 | } |
33 | 39 |
|
| 40 | +// CargoLicenseZeroMap describes metadata for Cargo crates. |
34 | 41 | 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"` |
37 | 44 | } |
38 | 45 |
|
39 | 46 | type cargoMetadataMap struct { |
40 | | - LicenseZero CargoLicenseZeroMap `json:"licensezero"` |
| 47 | + LicenseZero CargoLicenseZeroMap `json:"licensezero" toml:"licensezero"` |
41 | 48 | } |
42 | 49 |
|
43 | 50 | type cargoMetadataPackage struct { |
@@ -67,3 +74,43 @@ func cargoReadMetadata(packagePath string) (*cargoMetadataOutput, error) { |
67 | 74 | } |
68 | 75 | return &parsed, nil |
69 | 76 | } |
| 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 | +} |
0 commit comments