Skip to content

Commit

Permalink
Merge pull request #37 from edoardottt/devel
Browse files Browse the repository at this point in the history
Devel: add reqs command
  • Loading branch information
edoardottt committed Oct 7, 2023
2 parents bc8bc60 + 14bc966 commit 0667fbf
Show file tree
Hide file tree
Showing 8 changed files with 179 additions and 4 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ Available Commands:
info Get info about a package or a specific version of that
project Get info about a project (GitHub, GitLab, or BitBucket)
query Get info about multiple package versions using a query
reqs Get info about a package's requirements

Flags:
-h, --help help for depsdev
Expand Down Expand Up @@ -138,6 +139,14 @@ depsdev graph npm slice-ansi 6.0.0

<br>

Get information about the package requirements for a given version in a system-specific format.

```console
depsdev reqs npm slice-ansi 6.0.0
```

<br>

**Use depsdev as a Go module**

```Go
Expand Down
39 changes: 39 additions & 0 deletions cmd/reqs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package cmd

import (
"fmt"
"log"

"github.com/edoardottt/depsdev/pkg/input"
"github.com/edoardottt/depsdev/pkg/output"
"github.com/spf13/cobra"
)

var reqsCmd = &cobra.Command{
Use: "reqs package-manager package-name version",
Short: "Get info about a package's requirements",
Long: `Returns the requirements for a given version in a system-specific format.
Requirements are currently available for Maven, npm and NuGet.`,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 3 {
return fmt.Errorf("%s %w", "three", input.ErrArgumentsLeast)
}
if !input.Contains(args[0], []string{"npm", "maven", "nuget"}) {
return input.ErrInvalidPackageManagerForRequirements
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
d, err := api.GetRequirements(args[0], args[1], args[2])
if err != nil {
log.Fatal(err)
}

dJSON, err := output.IndentJSON(d)
if err != nil {
log.Fatalf(err.Error())
}

fmt.Println(dJSON)
},
}
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,5 @@ func init() {
rootCmd.AddCommand(projectCmd)
rootCmd.AddCommand(queryCmd)
rootCmd.AddCommand(graphCmd)
rootCmd.AddCommand(reqsCmd)
}
13 changes: 13 additions & 0 deletions pkg/depsdev/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,16 @@ func getQuery(c *client.Client, query string) (Package, error) {

return response, nil
}

// GetRequirements returns the requirements for a given version in a system-specific format.
// Requirements are currently available for Maven, npm and NuGet.
func (a *API) GetRequirements(packageManager, packageName, version string) (Requirements, error) {
var response Requirements

var path = fmt.Sprintf(GetRequirementsPath, packageManager, url.PathEscape(packageName), version)
if err := a.client.Get(path, &response); err != nil {
return Requirements{}, err
}

return response, nil
}
99 changes: 99 additions & 0 deletions pkg/depsdev/requirements.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package depsdev

type Requirements struct {
Nuget *NugetRequirement `json:"nuget,omitempty"`
Npm *NpmRequirement `json:"npm,omitempty"`
Maven *MavenRequirement `json:"maven,omitempty"`
}

type NugetRequirement struct {
DependependencyGroups []DependependencyGroup `json:"dependencyGroups,omitempty"`
}

type DependependencyGroup struct {
TargetFramework string `json:"targetFramework,omitempty"`
Dependencies []Dependency `json:"dependencies,omitempty"`
}

type Dependency struct {
Name string `json:"name,omitempty"`
Requirement string `json:"requirement,omitempty"`
Version string `json:"version,omitempty"`
System string `json:"system,omitempty"`
Classifier string `json:"classifier,omitempty"`
Type string `json:"type,omitempty"`
Scope string `json:"scope,omitempty"`
Optional string `json:"optional,omitempty"`
Exclusions []string `json:"exclusions,omitempty"`
}

type NpmRequirement struct {
Dependencies NpmDependency `json:"dependencies,omitempty"`
Bundled []Bundled `json:"bundled,omitempty"`
}

type Bundled struct {
Path string `json:"path,omitempty"`
Name string `json:"name,omitempty"`
Version string `json:"version,omitempty"`
Dependencies []NpmDependency `json:"dependencies,omitempty"`
}

type NpmDependency struct {
Dependencies []Dependency `json:"dependencies"`
DevDependencies []Dependency `json:"devDependencies"`
OptionalDependencies []Dependency `json:"optionalDependencies"`
PeerDependencies []Dependency `json:"peerDependencies"`
BundleDependencies []string `json:"bundleDependencies"`
}

type MavenRequirement struct {
ID string `json:"id,omitempty"`
Parent Dependency `json:"parent,omitempty"`
Activation Activation `json:"activation,omitempty"`
Dependencies []Dependency `json:"dependencies,omitempty"`
DependencyManagement []Dependency `json:"dependency_management,omitempty"`
Properties []Property `json:"properties,omitempty"`
Repositories []MavenRepository `json:"repositories,omitempty"`
Profiles []MavenRequirement `json:"profiles,omitempty"`
}

type Property struct {
Name string `json:"name,omitempty"`
Value string `json:"value,omitempty"`
}

type MavenRepository struct {
ID string `json:"id,omitempty"`
URL string `json:"url,omitempty"`
Layout string `json:"layout,omitempty"`
ReleasesEnabled string `json:"releases_enabled,omitempty"`
SnapshotsEnabled string `json:"snapshots_enabled,omitempty"`
}

type Activation struct {
JDK JDK `json:"jdk,omitempty"`
OS OS `json:"os,omitempty"`
Property PropertyWrapper `json:"property,omitempty"`
File File `json:"file,omitempty"`
}

type JDK struct {
JDK string `json:"jdk,omitempty"`
}

type OS struct {
Name string `json:"name,omitempty"`
Family string `json:"family,omitempty"`
Arch string `json:"arch,omitempty"`
Version string `json:"version,omitempty"`
}

type PropertyWrapper struct {
Property Property `json:"property,omitempty"`
}

type File struct {
Exists string `json:"exists,omitempty"`
Missing string `json:"missing,omitempty"`
}
1 change: 1 addition & 0 deletions pkg/depsdev/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ const (
GetProjectPath = `/projects/%s`
GetAdvisoryPath = `/advisories/%s`
GetQueryPath = `/query`
GetRequirementsPath = `/systems/%s/packages/%s/versions/%s:requirements`
)
10 changes: 10 additions & 0 deletions pkg/input/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,13 @@ func IsValidPackageManager(input string) bool {

return false
}

func Contains(v string, sl []string) bool {
for _, item := range sl {
if strings.ToLower(v) == item {
return true
}
}

return false
}
11 changes: 7 additions & 4 deletions pkg/input/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@ Free access to dependencies, licenses, advisories, and other critical health and

package input

import "errors"
import (
"errors"
)

var (
ErrArgumentLeast = errors.New("argument must be specified")
ErrArgumentsLeast = errors.New("arguments must be specified")
ErrInvalidPackageManager = errors.New("invalid package manager specified")
ErrArgumentLeast = errors.New("argument must be specified")
ErrArgumentsLeast = errors.New("arguments must be specified")
ErrInvalidPackageManager = errors.New("invalid package manager specified")
ErrInvalidPackageManagerForRequirements = errors.New("invalid package manager specified: requirements are currently available only for Maven, npm and NuGet")
)

0 comments on commit 0667fbf

Please sign in to comment.