Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support composer #21

Merged
merged 1 commit into from
Oct 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
| JavaScript | yarn | yarn.lock | :heavy_check_mark: |
| JavaScript | npm | package-lock.json | :heavy_check_mark: |
| Python | pip | requirements.txt | :heavy_check_mark: |
| PHP | composer | composer.lock | :heavy_check_mark: |
| Go | | go.sum | (soon) |

## Install
Expand Down
30 changes: 30 additions & 0 deletions cmd/composer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package cmd

import (
"net/http"

parser_io "github.com/aquasecurity/go-dep-parser/pkg/io"
"github.com/aquasecurity/go-dep-parser/pkg/php/composer"
"github.com/aquasecurity/go-dep-parser/pkg/types"
)

type ComposerDoctor struct {
HTTPClient http.Client
}

func NewComposerDoctor() *ComposerDoctor {
client := &http.Client{}
return &ComposerDoctor{HTTPClient: *client}
}

func (d *ComposerDoctor) Deps(r parser_io.ReadSeekerAt) []types.Library {
p := composer.NewParser()
deps, _, _ := p.Parse(r)
return deps
}

func (d *ComposerDoctor) SourceCodeURL(name string) (string, error) {
packagist := Packagist{name: name}
url, err := packagist.fetchURLFromRegistry(d.HTTPClient)
return url, err
}
9 changes: 5 additions & 4 deletions cmd/diagnose.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,11 @@ var (
)

var doctors = map[string]MedicalTechnician{
"bundler": NewBundlerDoctor(),
"yarn": NewYarnDoctor(),
"pip": NewPipDoctor(),
"npm": NewNPMDoctor(),
"bundler": NewBundlerDoctor(),
"yarn": NewYarnDoctor(),
"pip": NewPipDoctor(),
"npm": NewNPMDoctor(),
"composer": NewComposerDoctor(),
}

var diagnoseCmd = &cobra.Command{
Expand Down
2 changes: 1 addition & 1 deletion cmd/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (githuburl GitHubURL) Parse() (string, string, error) {
return "", "", errors.New("error: Unknown URL")
}
owner = paths[1]
repo = paths[2]
repo = strings.Replace(paths[2], ".git", "", 1)
} else if u.Scheme == "file" {
if paths[0] == "github.com" {
owner = paths[1]
Expand Down
62 changes: 62 additions & 0 deletions cmd/packagist.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package cmd

import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
)

// https://packagist.org/apidoc#get-package-data
const PACKAGIST_REGISTRY_API = "https://repo.packagist.org/p2/%s.json"

/*
type PackagistRegistryResponse struct {
Packages []struct {
Source struct {
URL string `json:"url"`
} `json:"source"`
} `json:"packages"`
}
*/
type PackagistRegistryResponse struct {
Packages map[string][]struct {
Source struct {
URL string `json:"url"`
} `json:"source"`
} `json:"packages"`
}

type Packagist struct {
name string
}

func (p *Packagist) fetchURLFromRegistry(client http.Client) (string, error) {
url := fmt.Sprintf(PACKAGIST_REGISTRY_API, p.name)
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return "", err
}

resp, err := client.Do(req)
if err != nil {
return "", err
}

defer resp.Body.Close()
if resp.StatusCode < 200 || 299 < resp.StatusCode {
m := fmt.Sprintf("Got status code: %d from %s", resp.StatusCode, RUBY_GEMS_REGISTRY_API)
return "", errors.New(m)
}

body, _ := io.ReadAll(resp.Body)

var registryResponse PackagistRegistryResponse
err = json.Unmarshal(body, &registryResponse)
if err != nil {
return "", nil
}

return registryResponse.Packages[p.name][0].Source.URL, nil
}
Loading
Loading