Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for summary, metadata, and alternatives. #52

Merged
merged 1 commit into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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