diff --git a/cmd/example/main.go b/cmd/example/main.go index 72ce717..2479323 100644 --- a/cmd/example/main.go +++ b/cmd/example/main.go @@ -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) } @@ -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 } @@ -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 } @@ -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 } @@ -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 } diff --git a/internal/client/client.go b/internal/client/client.go index b2c663a..434588e 100644 --- a/internal/client/client.go +++ b/internal/client/client.go @@ -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 { @@ -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) } @@ -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 { @@ -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) } @@ -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 { @@ -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) } @@ -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 { @@ -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) } diff --git a/pkg/v2/types/types.go b/pkg/v2/types/types.go index 7dad632..603be6e 100644 --- a/pkg/v2/types/types.go +++ b/pkg/v2/types/types.go @@ -28,7 +28,7 @@ import ( // Dependency represents request arguments for various endpoints. type Dependency struct { PackageName string - PackageType *string + PackageType string PackageVersion *string }