Skip to content

Commit

Permalink
Add support for summary, metadata, and alternatives.
Browse files Browse the repository at this point in the history
This change adds support for the following v2 API endpoints
* `/v2/summary`, which returns an aggregate view of the dependency
* `/v2/pkg`, which returns more detailed information about a package
* `/v2/alternatives`, which returns packages which can be used in
  place of the given one

Note: this also contains a very minor breaking change that will impact
existing users; we changed the client constructor ruotines from
returning a pointer-to-struct to returning an interface, which
unfortunately requires users to accomodate their code. Despite being a
breaking change, we won't bump the major version because we consider
this sdk unstable at the moment.
  • Loading branch information
blkt committed Nov 14, 2024
1 parent a2cea2e commit 0ae8d59
Show file tree
Hide file tree
Showing 8 changed files with 973 additions and 312 deletions.
101 changes: 101 additions & 0 deletions cmd/example/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// Copyright 2024 Stacklok, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//nolint:revive
package main

import (
"context"
"flag"
"fmt"
"os"

v2client "github.com/stacklok/trusty-sdk-go/pkg/v2/client"
v2types "github.com/stacklok/trusty-sdk-go/pkg/v2/types"
)

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

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

switch endpoint {
case "summary":
if err := summary(ctx, client, pname); 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 {
fmt.Fprintf(os.Stderr, "error calling endpoint: %s\n", err)
os.Exit(1)
}
case "alternatives":
if err := alternatives(ctx, client, pname); err != nil {
fmt.Fprintf(os.Stderr, "error calling endpoint: %s\n", err)
os.Exit(1)
}
case "":
fmt.Fprintf(os.Stderr, "endpoint is mandatory\n")
os.Exit(1)
default:
fmt.Fprintf(os.Stderr, "invalid method: %s\n", endpoint)
os.Exit(1)
}
}

func summary(ctx context.Context, client v2client.Trusty, pname string) error {
res, err := client.Summary(ctx, &v2types.Dependency{
PackageName: pname,
})
if err != nil {
return err
}

fmt.Printf("%+v\n", res)
return nil
}

func pkg(ctx context.Context, client v2client.Trusty, pname string) error {
res, err := client.PackageMetadata(ctx, &v2types.Dependency{
PackageName: pname,
})
if err != nil {
return err
}

fmt.Printf("%+v\n", res)
fmt.Printf("STATUS: %+v\n", *res.Status)
fmt.Printf("MALICIOUS: %+v\n", res.Malicious)
for _, contributor := range res.Contributors {
fmt.Printf("CONTRIBUTOR: %+v\n", contributor)
}
return nil
}

func alternatives(ctx context.Context, client v2client.Trusty, pname string) error {
res, err := client.Alternatives(ctx, &v2types.Dependency{
PackageName: pname,
})
if err != nil {
return err
}

fmt.Printf("%+v\n", res)
return nil
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ toolchain go1.23.1
require (
github.com/BurntSushi/toml v1.4.0
github.com/google/go-github/v66 v66.0.0
github.com/google/uuid v1.6.0
github.com/package-url/packageurl-go v0.1.3
github.com/stretchr/testify v1.9.0
golang.org/x/oauth2 v0.23.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ github.com/google/go-github/v66 v66.0.0 h1:ADJsaXj9UotwdgK8/iFZtv7MLc8E8WBl62WLd
github.com/google/go-github/v66 v66.0.0/go.mod h1:+4SO9Zkuyf8ytMj0csN1NR/5OTR+MfqPp8P8dVlcvY4=
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/nozzle/throttler v0.0.0-20180817012639-2ea982251481 h1:Up6+btDp321ZG5/zdSLo48H9Iaq0UQGthrhWC6pCxzE=
github.com/nozzle/throttler v0.0.0-20180817012639-2ea982251481/go.mod h1:yKZQO8QE2bHlgozqWDiRVqTFlLQSj30K/6SAK8EeYFw=
github.com/package-url/packageurl-go v0.1.3 h1:4juMED3hHiz0set3Vq3KeQ75KD1avthoXLtmE3I0PLs=
Expand Down
Loading

0 comments on commit 0ae8d59

Please sign in to comment.