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

feat: packages tracker ux logic #187

Closed
wants to merge 11 commits into from
2 changes: 1 addition & 1 deletion cmd/in/in.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ import (
"fmt"
"runtime"

"github.com/cli/cli/pkg/iostreams"
"github.com/listendev/lstn/internal/project"
"github.com/listendev/lstn/pkg/cmd/arguments"
"github.com/listendev/lstn/pkg/cmd/groups"
"github.com/listendev/lstn/pkg/cmd/iostreams"
"github.com/listendev/lstn/pkg/cmd/options"
"github.com/listendev/lstn/pkg/cmd/packagesprinter"
pkgcontext "github.com/listendev/lstn/pkg/context"
Expand Down
2 changes: 1 addition & 1 deletion cmd/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"time"

"github.com/XANi/goneric"
"github.com/cli/cli/pkg/iostreams"
"github.com/listendev/lstn/cmd/in"
"github.com/listendev/lstn/cmd/scan"
"github.com/listendev/lstn/cmd/to"
Expand All @@ -37,6 +36,7 @@ import (
"github.com/listendev/lstn/pkg/cmd/flagusages"
"github.com/listendev/lstn/pkg/cmd/groups"
pkghelp "github.com/listendev/lstn/pkg/cmd/help"
"github.com/listendev/lstn/pkg/cmd/iostreams"
"github.com/listendev/lstn/pkg/cmd/options"
lstnviper "github.com/listendev/lstn/pkg/cmd/viper"
pkgcontext "github.com/listendev/lstn/pkg/context"
Expand Down
53 changes: 23 additions & 30 deletions cmd/scan/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,19 @@ package scan
import (
"context"
"fmt"
"os"
"runtime"

"github.com/Masterminds/semver/v3"
"github.com/XANi/goneric"
"github.com/cli/cli/pkg/iostreams"
"github.com/google/go-github/v50/github"
"github.com/listendev/lstn/internal/project"
"github.com/listendev/lstn/pkg/cmd"
"github.com/listendev/lstn/pkg/cmd/arguments"
"github.com/listendev/lstn/pkg/cmd/groups"
"github.com/listendev/lstn/pkg/cmd/iostreams"
"github.com/listendev/lstn/pkg/cmd/options"
"github.com/listendev/lstn/pkg/cmd/packagesprinter"
"github.com/listendev/lstn/pkg/cmd/packagestracker"
pkgcontext "github.com/listendev/lstn/pkg/context"
"github.com/listendev/lstn/pkg/listen"
"github.com/listendev/lstn/pkg/npm"
Expand Down Expand Up @@ -86,8 +87,7 @@ The verdicts it returns are listed by the name of each package and its specified
return nil
}

io := c.Context().Value(pkgcontext.IOStreamsKey).(*iostreams.IOStreams)
io.StartProgressIndicator()
io := ctx.Value(pkgcontext.IOStreamsKey).(*iostreams.IOStreams)

// Obtain the target directory that we want to listen in
targetDir, err := arguments.GetDirectory(args)
Expand All @@ -114,38 +114,31 @@ The verdicts it returns are listed by the name of each package and its specified
return fmt.Errorf("there are no dependencies to process for the currently selected sets of dependencies")
}

// Process one dependency set at once
tablePrinter := packagesprinter.NewTablePrinter(io)
combinedResponse := []listen.Package{}
for _, deps := range deps {
// Create list of verdicts requests
reqs, bulkErr := listen.NewBulkVerdictsRequestsFromMap(deps)
if bulkErr != nil {
return err
}

// Query for verdicts about specific package versions in bulk...
res, resJSON, resErr := listen.BulkPackages(reqs, listen.WithContext(ctx), listen.WithJSONOptions(scanOpts.JSONFlags))
mapDeps := packagestracker.ConvertToMapOfDependencies(deps)

if resErr != nil {
return err
packagesResponse, err := packagestracker.TrackPackages(ctx, mapDeps, func(depName string, depVersion *semver.Version) (*listen.Response, error) {
depArgs := []string{depName, depVersion.String()}
req, reqErr := listen.NewVerdictsRequest(depArgs)
if reqErr != nil {
return nil, reqErr
}

res, resJSON, resErr := listen.Packages(
req,
listen.WithContext(ctx),
listen.WithJSONOptions(scanOpts.JSONFlags),
)
if resJSON != nil {
fmt.Fprintf(os.Stdout, "%s", resJSON)
fmt.Fprintf(io.Out, "%s", resJSON)
}

// Appending the results of the current dependency set
if res != nil {
combinedResponse = append(combinedResponse, *res...)
}
}
return res, resErr
})

if scanOpts.JSON {
return nil
if err != nil {
return err
}

err = tablePrinter.RenderPackages((*listen.Response)(&combinedResponse))
tablePrinter := packagesprinter.NewTablePrinter(io)
err = tablePrinter.RenderPackages(packagesResponse)
if err != nil {
return err
}
Expand All @@ -169,7 +162,7 @@ The verdicts it returns are listed by the name of each package and its specified
rep.WithContext(ctx)

req := request.Report{
Packages: combinedResponse,
Packages: *packagesResponse,
GitHubPullCommentReport: request.GitHubPullCommentReport{
Owner: scanOpts.Reporter.GitHub.Owner,
Repo: scanOpts.Reporter.GitHub.Repo,
Expand Down
79 changes: 38 additions & 41 deletions cmd/to/to.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@ import (
"runtime"

"github.com/Masterminds/semver/v3"
"github.com/cli/cli/pkg/iostreams"
"github.com/listendev/lstn/internal/project"
"github.com/listendev/lstn/pkg/cmd/arguments"
"github.com/listendev/lstn/pkg/cmd/groups"
"github.com/listendev/lstn/pkg/cmd/iostreams"
"github.com/listendev/lstn/pkg/cmd/options"
"github.com/listendev/lstn/pkg/cmd/packagesprinter"
"github.com/listendev/lstn/pkg/cmd/packagestracker"
pkgcontext "github.com/listendev/lstn/pkg/context"
"github.com/listendev/lstn/pkg/listen"
"github.com/listendev/lstn/pkg/npm"
Expand Down Expand Up @@ -103,58 +104,54 @@ It lists out the verdicts of all the versions of the input package name.`,
return nil
}

var res *listen.Response
var resJSON []byte
var resErr error
io := ctx.Value(pkgcontext.IOStreamsKey).(*iostreams.IOStreams)

io := c.Context().Value(pkgcontext.IOStreamsKey).(*iostreams.IOStreams)
io.StartProgressIndicator()
defer io.StopProgressIndicator()

versions, multiple := ctx.Value(pkgcontext.VersionsCollection).(semver.Collection)
if multiple {
nv := len(versions)

names := make([]string, nv)
for i := 0; i < nv; i++ {
names[i] = args[0]
}

// Create list of verdicts requests
reqs, multipleErr := listen.NewBulkVerdictsRequests(names, versions)
if multipleErr != nil {
return multipleErr
}

// Query for verdicts about specific package versions...
res, resJSON, resErr = listen.BulkPackages(reqs, listen.WithContext(ctx), listen.WithJSONOptions(toOpts.JSONFlags))
versions, _ := ctx.Value(pkgcontext.VersionsCollection).(semver.Collection)
depName := args[0]
constraints := ""
if len(args) > 1 {
constraints = args[1]
}
depsList := packagestracker.ListOfDependencies{}

goto EXIT
if len(versions) == 0 {
depsList = append(depsList, packagestracker.Dependency{
Name: depName,
})
}
for _, v := range versions {
depsList = append(depsList, packagestracker.Dependency{
Name: depName,
Version: v,
})
}
allDeps := map[string]packagestracker.ListOfDependencies{
constraints: depsList,
}

// Query for one single package version...
// Or for all the package versions listen.dev owns of the target package
{
// New block so it's safe to skip variable declarations
req, reqErr := listen.NewVerdictsRequest(args)
res, err := packagestracker.TrackPackages(ctx, allDeps, func(depName string, depVersion *semver.Version) (*listen.Response, error) {
depArgs := []string{depName}
if depVersion != nil {
depArgs = append(depArgs, depVersion.String())
}
req, reqErr := listen.NewVerdictsRequest(depArgs)
if reqErr != nil {
return reqErr
return nil, reqErr
}

res, resJSON, resErr = listen.Packages(
res, resJSON, resErr := listen.Packages(
req,
listen.WithContext(ctx),
listen.WithJSONOptions(toOpts.JSONFlags),
)
}
if resJSON != nil {
fmt.Fprintf(io.Out, "%s", resJSON)
}

EXIT:
if resErr != nil {
return err
}
return res, resErr
})

if resJSON != nil {
fmt.Fprintf(io.Out, "%s", resJSON)
if err != nil {
return err
}

if res == nil {
Expand Down
9 changes: 8 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ require (
github.com/imdario/mergo v0.3.13
github.com/itchyny/gojq v0.12.11
github.com/jarcoal/httpmock v1.3.0
github.com/jedib0t/go-pretty v4.3.0+incompatible
github.com/jedib0t/go-pretty/v6 v6.4.6
github.com/leodido/go-npmpackagename v0.2.0
github.com/matishsiao/goInfo v0.0.0-00010101000000-000000000000
github.com/mitchellh/mapstructure v1.5.0
github.com/muja/goconfig v0.0.0-20180417074348-0a635507dddc
github.com/spf13/cobra v1.6.1
github.com/spf13/pflag v1.0.5
Expand All @@ -36,6 +39,7 @@ require (
github.com/Microsoft/go-winio v0.6.0 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20230217124315-7d5c6f04bbb8 // indirect
github.com/acomagu/bufpipe v1.0.3 // indirect
github.com/asaskevich/govalidator v0.0.0-20200907205600-7a23bdc65eef // indirect
github.com/aymanbagabas/go-osc52 v1.0.3 // indirect
github.com/briandowns/spinner v1.11.1 // indirect
github.com/cli/safeexec v1.0.0 // indirect
Expand All @@ -46,6 +50,8 @@ require (
github.com/fatih/color v1.13.0 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/go-git/gcfg v1.5.0 // indirect
github.com/go-openapi/errors v0.20.2 // indirect
github.com/go-openapi/strfmt v0.21.5 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
Expand All @@ -62,8 +68,8 @@ require (
github.com/mattn/go-isatty v0.0.16 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/muesli/termenv v0.13.0 // indirect
github.com/oklog/ulid v1.3.1 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.0.5 // indirect
github.com/pjbgf/sha1cd v0.2.3 // indirect
Expand All @@ -79,6 +85,7 @@ require (
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/subosito/gotenv v1.4.1 // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
go.mongodb.org/mongo-driver v1.10.0 // indirect
golang.org/x/crypto v0.7.0 // indirect
golang.org/x/mod v0.8.0 // indirect
golang.org/x/net v0.8.0 // indirect
Expand Down
Loading