Skip to content

Commit

Permalink
Merge pull request #34 from kyoshidajp/dart_pub
Browse files Browse the repository at this point in the history
Support dart pub
  • Loading branch information
kyoshidajp authored Nov 3, 2023
2 parents a89126f + f4af5ed commit b9b74c5
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ However, some libraries have archived their source code repositories or have had

| language | package manager | dependencies file (e.g.) | status |
| -------- | ------------- | -- | :----: |
| Dart | pub | pubspec.lock | :heavy_check_mark: |
| Go | golang | go.mod | :heavy_check_mark: |
| JavaScript | npm | package-lock.json | :heavy_check_mark: |
| JavaScript | yarn | yarn.lock | :heavy_check_mark: |
Expand Down
86 changes: 86 additions & 0 deletions cmd/dart/pub/pub.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package dart

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

"github.com/aquasecurity/go-dep-parser/pkg/dart/pub"
parser_io "github.com/aquasecurity/go-dep-parser/pkg/io"
"github.com/aquasecurity/go-dep-parser/pkg/types"
)

const PUB_REGISTRY_API = "https://pub.dev/api/packages/%s"

type PubRegistryResponse struct {
Latest struct {
Pubspec struct {
Repository string `json:"repository"`
Homepage string `json:"homepage"`
IssueTracker string `json:"issue_tracker"`
} `json:"pubspec"`
} `json:"latest"`
}

type Pub struct {
lib types.Library
}

func (n *Pub) fetchURLFromRegistry(client http.Client) (string, error) {
url := fmt.Sprintf(PUB_REGISTRY_API, n.lib.Name)
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return "", err
}

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

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

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

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

if PubRegistryResponse.Latest.Pubspec.Repository != "" {
return PubRegistryResponse.Latest.Pubspec.Repository, nil
}

if PubRegistryResponse.Latest.Pubspec.Homepage != "" {
return PubRegistryResponse.Latest.Pubspec.Homepage, nil
}

return PubRegistryResponse.Latest.Pubspec.IssueTracker, nil
}

type PubDoctor struct {
HTTPClient http.Client
}

func NewPubDoctor() *PubDoctor {
return &PubDoctor{}
}

func (d *PubDoctor) Libraries(r parser_io.ReadSeekerAt) []types.Library {
p := pub.NewParser()
libs, _, _ := p.Parse(r)
return libs
}

func (d *PubDoctor) SourceCodeURL(lib types.Library) (string, error) {
pub := Pub{lib: lib}
url, err := pub.fetchURLFromRegistry(d.HTTPClient)
return url, err
}
25 changes: 25 additions & 0 deletions cmd/dart/pub/testdata/podspec.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Generated by pub
# See https://dart.dev/tools/pub/glossary#lockfile
packages:
crypto:
dependency: "direct main"
description:
name: crypto
url: "https://pub.flutter-io.cn"
source: hosted
version: "3.0.2"
flutter_test:
dependency: "direct dev"
description: flutter
source: sdk
version: "0.0.0"
uuid:
dependency: transitive
description:
name: uuid
url: "https://pub.flutter-io.cn"
source: hosted
version: "3.0.6"
sdks:
dart: ">=2.18.0 <3.0.0"
flutter: ">=3.3.0"
2 changes: 2 additions & 0 deletions cmd/diagnose.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
parser_io "github.com/aquasecurity/go-dep-parser/pkg/io"
"github.com/aquasecurity/go-dep-parser/pkg/types"
"github.com/fatih/color"
dart "github.com/kyoshidajp/dep-doctor/cmd/dart/pub"
"github.com/kyoshidajp/dep-doctor/cmd/github"
"github.com/kyoshidajp/dep-doctor/cmd/golang"
"github.com/kyoshidajp/dep-doctor/cmd/nodejs"
Expand Down Expand Up @@ -216,6 +217,7 @@ var doctors = Doctors{
"yarn": nodejs.NewYarnDoctor(),
"pip": python.NewPipDoctor(),
"pipenv": python.NewPipenvDoctor(),
"pub": dart.NewPubDoctor(),
"npm": nodejs.NewNPMDoctor(),
"composer": php.NewComposerDoctor(),
"golang": golang.NewGolangDoctor(),
Expand Down
2 changes: 1 addition & 1 deletion cmd/golang/proxy_golang.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (g *ProxyGolang) fetchURLFromRegistry(client http.Client) (string, error) {

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

Expand Down
2 changes: 1 addition & 1 deletion cmd/nodejs/nodejs.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (n *Nodejs) fetchURLFromRegistry(client http.Client) (string, error) {

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

Expand Down
2 changes: 1 addition & 1 deletion cmd/php/packagist.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (p *Packagist) fetchURLFromRegistry(client http.Client) (string, error) {

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

Expand Down
2 changes: 1 addition & 1 deletion cmd/python/pypi.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (p *Pypi) fetchURLFromRegistry(client http.Client) (string, error) {

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

Expand Down
2 changes: 1 addition & 1 deletion cmd/ruby/ruby_gems.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (g *RubyGems) fetchURLFromRegistry(client http.Client) (string, error) {

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)
m := fmt.Sprintf("Got status code: %d from %s", resp.StatusCode, url)
return "", errors.New(m)
}

Expand Down

0 comments on commit b9b74c5

Please sign in to comment.