generated from skanehira/go-cli-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from kyoshidajp/dart_pub
Support dart pub
- Loading branch information
Showing
9 changed files
with
119 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters