Skip to content

Commit

Permalink
Make package type mandatory.
Browse files Browse the repository at this point in the history
  • Loading branch information
blkt committed Nov 21, 2024
1 parent 8f2034e commit 6802cdc
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 34 deletions.
56 changes: 35 additions & 21 deletions cmd/example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,32 +26,38 @@ import (
)

func main() {
var endpoint, pname string
var endpoint, pname, ptype string
flag.StringVar(&endpoint, "endpoint", "", "Trusty API endpoint to call")
flag.StringVar(&pname, "pname", "", "Package name")
flag.StringVar(&ptype, "ptype", "", "Package type")
flag.Parse()

ctx := context.Background()
client := v2client.New()

input := &v2types.Dependency{
PackageName: pname,
PackageType: ptype,
}

switch endpoint {
case "summary":
if err := summary(ctx, client, pname); err != nil {
if err := summary(ctx, client, input); err != nil {
fmt.Fprintf(os.Stderr, "error calling endpoint: %s\n", err)
os.Exit(1)
}
case "pkg-meta":
if err := pkg(ctx, client, pname); err != nil {
if err := pkg(ctx, client, input); err != nil {
fmt.Fprintf(os.Stderr, "error calling endpoint: %s\n", err)
os.Exit(1)
}
case "alternatives":
if err := alternatives(ctx, client, pname); err != nil {
if err := alternatives(ctx, client, input); err != nil {
fmt.Fprintf(os.Stderr, "error calling endpoint: %s\n", err)
os.Exit(1)
}
case "provenance":
if err := provenance(ctx, client, pname); err != nil {
if err := provenance(ctx, client, input); err != nil {
fmt.Fprintf(os.Stderr, "error calling endpoint: %s\n", err)
os.Exit(1)
}
Expand All @@ -64,10 +70,12 @@ func main() {
}
}

func summary(ctx context.Context, client v2client.Trusty, pname string) error {
res, err := client.Summary(ctx, &v2types.Dependency{
PackageName: pname,
})
func summary(
ctx context.Context,
client v2client.Trusty,
input *v2types.Dependency,
) error {
res, err := client.Summary(ctx, input)
if err != nil {
return err
}
Expand All @@ -76,10 +84,12 @@ func summary(ctx context.Context, client v2client.Trusty, pname string) error {
return nil
}

func pkg(ctx context.Context, client v2client.Trusty, pname string) error {
res, err := client.PackageMetadata(ctx, &v2types.Dependency{
PackageName: pname,
})
func pkg(
ctx context.Context,
client v2client.Trusty,
input *v2types.Dependency,
) error {
res, err := client.PackageMetadata(ctx, input)
if err != nil {
return err
}
Expand All @@ -93,10 +103,12 @@ func pkg(ctx context.Context, client v2client.Trusty, pname string) error {
return nil
}

func alternatives(ctx context.Context, client v2client.Trusty, pname string) error {
res, err := client.Alternatives(ctx, &v2types.Dependency{
PackageName: pname,
})
func alternatives(
ctx context.Context,
client v2client.Trusty,
input *v2types.Dependency,
) error {
res, err := client.Alternatives(ctx, input)
if err != nil {
return err
}
Expand All @@ -105,10 +117,12 @@ func alternatives(ctx context.Context, client v2client.Trusty, pname string) err
return nil
}

func provenance(ctx context.Context, client v2client.Trusty, pname string) error {
res, err := client.Provenance(ctx, &v2types.Dependency{
PackageName: pname,
})
func provenance(
ctx context.Context,
client v2client.Trusty,
input *v2types.Dependency,
) error {
res, err := client.Provenance(ctx, input)
if err != nil {
return err
}
Expand Down
28 changes: 16 additions & 12 deletions internal/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,9 @@ func (t *Trusty) Summary(
if dep.PackageName == "" {
return nil, fmt.Errorf("dependency has no name defined")
}
if dep.PackageType == "" {
return nil, fmt.Errorf("dependency has no ecosystem defined")
}

u, err := urlFor(t.Options.BaseURL, v2SummaryPath)
if err != nil {
Expand All @@ -347,9 +350,7 @@ func (t *Trusty) Summary(
// package_version.
q := u.Query()
q.Set("package_name", dep.PackageName)
if dep.PackageType != nil && *dep.PackageType != "" {
q.Set("package_type", strings.ToLower(*dep.PackageType))
}
q.Set("package_type", strings.ToLower(dep.PackageType))
if dep.PackageVersion != nil && *dep.PackageVersion != "" {
q.Set("package_version", *dep.PackageVersion)
}
Expand All @@ -369,6 +370,9 @@ func (t *Trusty) PackageMetadata(
if dep.PackageName == "" {
return nil, fmt.Errorf("dependency has no name defined")
}
if dep.PackageType == "" {
return nil, fmt.Errorf("dependency has no ecosystem defined")
}

u, err := urlFor(t.Options.BaseURL, v2PkgPath)
if err != nil {
Expand All @@ -379,9 +383,7 @@ func (t *Trusty) PackageMetadata(
// package_version.
q := u.Query()
q.Set("package_name", dep.PackageName)
if dep.PackageType != nil && *dep.PackageType != "" {
q.Set("package_type", strings.ToLower(*dep.PackageType))
}
q.Set("package_type", strings.ToLower(dep.PackageType))
if dep.PackageVersion != nil && *dep.PackageVersion != "" {
q.Set("package_version", *dep.PackageVersion)
}
Expand All @@ -399,6 +401,9 @@ func (t *Trusty) Alternatives(
if dep.PackageName == "" {
return nil, fmt.Errorf("dependency has no name defined")
}
if dep.PackageType == "" {
return nil, fmt.Errorf("dependency has no ecosystem defined")
}

u, err := urlFor(t.Options.BaseURL, v2Alternatives)
if err != nil {
Expand All @@ -409,9 +414,7 @@ func (t *Trusty) Alternatives(
// package_version.
q := u.Query()
q.Set("package_name", dep.PackageName)
if dep.PackageType != nil && *dep.PackageType != "" {
q.Set("package_type", strings.ToLower(*dep.PackageType))
}
q.Set("package_type", strings.ToLower(dep.PackageType))
if dep.PackageVersion != nil && *dep.PackageVersion != "" {
q.Set("package_version", *dep.PackageVersion)
}
Expand All @@ -429,6 +432,9 @@ func (t *Trusty) Provenance(
if dep.PackageName == "" {
return nil, fmt.Errorf("dependency has no name defined")
}
if dep.PackageType == "" {
return nil, fmt.Errorf("dependency has no ecosystem defined")
}

u, err := urlFor(t.Options.BaseURL, v2Provenance)
if err != nil {
Expand All @@ -439,9 +445,7 @@ func (t *Trusty) Provenance(
// package_version.
q := u.Query()
q.Set("package_name", dep.PackageName)
if dep.PackageType != nil && *dep.PackageType != "" {
q.Set("package_type", strings.ToLower(*dep.PackageType))
}
q.Set("package_type", strings.ToLower(dep.PackageType))
if dep.PackageVersion != nil && *dep.PackageVersion != "" {
q.Set("package_version", *dep.PackageVersion)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/v2/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
// Dependency represents request arguments for various endpoints.
type Dependency struct {
PackageName string
PackageType *string
PackageType string
PackageVersion *string
}

Expand Down

0 comments on commit 6802cdc

Please sign in to comment.