Skip to content

Commit

Permalink
feat: add npm input type
Browse files Browse the repository at this point in the history
  • Loading branch information
Skarlso committed Aug 23, 2024
1 parent e46befa commit 53aee2b
Show file tree
Hide file tree
Showing 11 changed files with 203 additions and 19 deletions.
1 change: 0 additions & 1 deletion api/ocm/extensions/accessmethods/npm/method_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ var _ = Describe("Method", func() {

It("accesses artifact", func() {
acc := npm.New("file://"+NPMPATH, "yargs", "17.7.1")
// acc := npm.New("https://registry.npmjs.org", "yargs", "17.7.1")

m := Must(acc.AccessMethod(cv))
defer m.Close()
Expand Down
45 changes: 27 additions & 18 deletions api/ocm/extensions/accessmethods/options/standard.go
Original file line number Diff line number Diff line change
@@ -1,54 +1,54 @@
package options

// HintOption.
// HintOption .
var HintOption = RegisterOption(NewStringOptionType("hint", "(repository) hint for local artifacts"))

// MediatypeOption.
// MediatypeOption .
var MediatypeOption = RegisterOption(NewStringOptionType("mediaType", "media type for artifact blob representation"))

// SizeOption.
// SizeOption .
var SizeOption = RegisterOption(NewIntOptionType("size", "blob size"))

// DigestOption.
// DigestOption .
var DigestOption = RegisterOption(NewStringOptionType("digest", "blob digest"))

// ReferenceOption.
// ReferenceOption .
var ReferenceOption = RegisterOption(NewStringOptionType("reference", "reference name"))

// PackageOption.
// PackageOption .
var PackageOption = RegisterOption(NewStringOptionType("accessPackage", "package or object name"))

// ArtifactOption.
// ArtifactOption .
var ArtifactOption = RegisterOption(NewStringOptionType("artifactId", "maven artifact id"))

// GroupOption.
// GroupOption .
var GroupOption = RegisterOption(NewStringOptionType("groupId", "maven group id"))

// RepositoryOption.
// RepositoryOption .
var RepositoryOption = RegisterOption(NewStringOptionType("accessRepository", "repository URL"))

// RegistryOption.
// RegistryOption .
var RegistryOption = RegisterOption(NewStringOptionType("accessRegistry", "registry base URL"))

// HostnameOption.
// HostnameOption .
var HostnameOption = RegisterOption(NewStringOptionType("accessHostname", "hostname used for access"))

// CommitOption.
// CommitOption .
var CommitOption = RegisterOption(NewStringOptionType("commit", "git commit id"))

// GlobalAccessOption.
// GlobalAccessOption .
var GlobalAccessOption = RegisterOption(NewValueMapYAMLOptionType("globalAccess", "access specification for global access"))

// RegionOption.
// RegionOption .
var RegionOption = RegisterOption(NewStringOptionType("region", "region name"))

// BucketOption.
// BucketOption .
var BucketOption = RegisterOption(NewStringOptionType("bucket", "bucket name"))

// VersionOption.
// VersionOption .
var VersionOption = RegisterOption(NewStringOptionType("accessVersion", "version for access specification"))

// URLOption.
// URLOption .
var URLOption = RegisterOption(NewStringOptionType("url", "artifact or server url"))

var HTTPHeaderOption = RegisterOption(NewStringSliceMapColonOptionType("header", "http headers"))
Expand All @@ -59,11 +59,20 @@ var HTTPBodyOption = RegisterOption(NewStringOptionType("body", "body of a http

var HTTPRedirectOption = RegisterOption(NewBoolOptionType("noredirect", "http redirect behavior"))

// CommentOption.
// CommentOption .
var CommentOption = RegisterOption(NewStringOptionType("comment", "comment field value"))

// ClassifierOption the optional classifier of a maven resource.
var ClassifierOption = RegisterOption(NewStringOptionType("classifier", "maven classifier"))

// ExtensionOption the optional extension of a maven resource.
var ExtensionOption = RegisterOption(NewStringOptionType("extension", "maven extension name"))

// NPMRegistryOption sets the registry of the npm resource.
var NPMRegistryOption = RegisterOption(NewStringOptionType("registry", "npm package registry"))

// NPMPackageOption sets what package should be fetched from the npm registry.
var NPMPackageOption = RegisterOption(NewStringOptionType("package", "npm package name"))

// NPMVersionOption sets the version of the npm package.
var NPMVersionOption = RegisterOption(NewStringOptionType("version", "npm package version"))
4 changes: 4 additions & 0 deletions cmds/ocm/commands/ocmcmds/common/inputs/options/standard.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ var (
ArtifactOption = options.ArtifactOption
ClassifierOption = options.ClassifierOption
ExtensionOption = options.ExtensionOption

RegistryOption = options.NPMRegistryOption
PackageOption = options.NPMPackageOption
PackageVersionOption = options.NPMVersionOption
)

// string options.
Expand Down
22 changes: 22 additions & 0 deletions cmds/ocm/commands/ocmcmds/common/inputs/types/npm/cli.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package npm

import (
"ocm.software/ocm/api/utils/cobrautils/flagsets"
"ocm.software/ocm/cmds/ocm/commands/ocmcmds/common/inputs/options"
)

func ConfigHandler() flagsets.ConfigOptionTypeSetHandler {
return flagsets.NewConfigOptionTypeSetHandler(
TYPE, AddConfig,
options.RegistryOption,
options.PackageOption,
options.PackageVersionOption,
)
}

func AddConfig(opts flagsets.ConfigOptions, config flagsets.Config) error {
flagsets.AddFieldByOptionP(opts, options.RegistryOption, config, "registry")
flagsets.AddFieldByOptionP(opts, options.PackageOption, config, "package")
flagsets.AddFieldByOptionP(opts, options.PackageVersionOption, config, "version")
return nil
}
31 changes: 31 additions & 0 deletions cmds/ocm/commands/ocmcmds/common/inputs/types/npm/input_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package npm_test

import (
. "github.com/onsi/ginkgo/v2"
. "ocm.software/ocm/cmds/ocm/commands/ocmcmds/common/inputs/testutils"
"ocm.software/ocm/cmds/ocm/commands/ocmcmds/common/inputs/types/npm"

"ocm.software/ocm/cmds/ocm/commands/ocmcmds/common/inputs"
"ocm.software/ocm/cmds/ocm/commands/ocmcmds/common/inputs/options"
)

var _ = Describe("Input Type", func() {
var env *InputTest

BeforeEach(func() {
env = NewInputTest(npm.TYPE)
})

// TODO: try out a file based NPM registry for testing.
It("simple fetch", func() {
env.Set(options.RegistryOption, "https://registry.npmjs.org")
env.Set(options.PackageOption, "yargs")
env.Set(options.PackageVersionOption, "17.7.1")
env.Check(&npm.Spec{
InputSpecBase: inputs.InputSpecBase{},
Registry: "https://registry.npmjs.org",
Package: "yargs",
Version: "17.7.1",
})
})
})
66 changes: 66 additions & 0 deletions cmds/ocm/commands/ocmcmds/common/inputs/types/npm/spec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package npm

import (
"fmt"

"k8s.io/apimachinery/pkg/util/validation/field"

"ocm.software/ocm/api/ocm/extensions/accessmethods/npm"
"ocm.software/ocm/api/ocm/extensions/repositories/composition"
"ocm.software/ocm/api/utils/blobaccess"
"ocm.software/ocm/api/utils/runtime"
"ocm.software/ocm/cmds/ocm/commands/ocmcmds/common/inputs"
)

type Spec struct {
inputs.InputSpecBase `json:",inline"`
// Registry is the base URL of the NPM registry
Registry string `json:"registry"`
// Package is the name of NPM package
Package string `json:"package"`
// Version of the NPM package.
Version string `json:"version"`
}

var _ inputs.InputSpec = (*Spec)(nil)

func New(registry, pkg, version string) *Spec {
return &Spec{
InputSpecBase: inputs.InputSpecBase{
ObjectVersionedType: runtime.ObjectVersionedType{
Type: TYPE,
},
},
Registry: registry,
Package: pkg,
Version: version,
}
}

func (s *Spec) Validate(fldPath *field.Path, ctx inputs.Context, inputFilePath string) field.ErrorList {
var allErrs field.ErrorList

if s.Registry == "" {
pathField := fldPath.Child("Registry")
allErrs = append(allErrs, field.Invalid(pathField, s.Registry, "no registry"))
}

if s.Package == "" {
pathField := fldPath.Child("Package")
allErrs = append(allErrs, field.Invalid(pathField, s.Package, "no package"))
}

return allErrs
}

func (s *Spec) GetBlob(ctx inputs.Context, info inputs.InputResourceInfo) (blobaccess.BlobAccess, string, error) {
access := npm.New(s.Registry, s.Package, s.Version)
ver := composition.NewComponentVersion(ctx, info.ComponentVersion.GetName(), info.ComponentVersion.GetVersion())

blobAccess, err := access.AccessMethod(ver)
if err != nil {
return nil, "", fmt.Errorf("failed to create access method for npm: %w", err)
}

return blobAccess.AsBlobAccess(), "", err
}
13 changes: 13 additions & 0 deletions cmds/ocm/commands/ocmcmds/common/inputs/types/npm/suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package npm_test

import (
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

func TestConfig(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Input Type npm")
}
31 changes: 31 additions & 0 deletions cmds/ocm/commands/ocmcmds/common/inputs/types/npm/type.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package npm

import (
"ocm.software/ocm/cmds/ocm/commands/ocmcmds/common/inputs"
)

const TYPE = "npm"

func init() {
inputs.DefaultInputTypeScheme.Register(inputs.NewInputType(TYPE, &Spec{}, usage, ConfigHandler()))
}

const usage = `
The <code>registry</code> is the url pointing to the npm registry from which a resource is
downloaded.
This blob type specification supports the following fields:
- **<code>registry</code>** *string*
This REQUIRED property describes the url from which the resource is to be
downloaded.
- **<code>package</code>** *string*
The is a REQUIRED property describing the name of the package to download.
- **<code>version</code>** *string*
This is an OPTIONAL property describing the version of the package to download. If
not defined, latest will be used automatically.
`
3 changes: 3 additions & 0 deletions docs/pluginreference/plugin_accessmethod_compose.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,14 @@ The following predefined option types can be used:
- <code>hint</code>: [*string*] (repository) hint for local artifacts
- <code>mediaType</code>: [*string*] media type for artifact blob representation
- <code>noredirect</code>: [*bool*] http redirect behavior
- <code>package</code>: [*string*] npm package name
- <code>reference</code>: [*string*] reference name
- <code>region</code>: [*string*] region name
- <code>registry</code>: [*string*] npm package registry
- <code>size</code>: [*int*] blob size
- <code>url</code>: [*string*] artifact or server url
- <code>verb</code>: [*string*] http request method
- <code>version</code>: [*string*] npm package version

The following predefined value types are supported:

Expand Down
3 changes: 3 additions & 0 deletions docs/pluginreference/plugin_descriptor.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,14 @@ The following predefined option types can be used:
- <code>hint</code>: [*string*] (repository) hint for local artifacts
- <code>mediaType</code>: [*string*] media type for artifact blob representation
- <code>noredirect</code>: [*bool*] http redirect behavior
- <code>package</code>: [*string*] npm package name
- <code>reference</code>: [*string*] reference name
- <code>region</code>: [*string*] region name
- <code>registry</code>: [*string*] npm package registry
- <code>size</code>: [*int*] blob size
- <code>url</code>: [*string*] artifact or server url
- <code>verb</code>: [*string*] http request method
- <code>version</code>: [*string*] npm package version

The following predefined value types are supported:

Expand Down
3 changes: 3 additions & 0 deletions docs/pluginreference/plugin_valueset_compose.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,14 @@ The following predefined option types can be used:
- <code>hint</code>: [*string*] (repository) hint for local artifacts
- <code>mediaType</code>: [*string*] media type for artifact blob representation
- <code>noredirect</code>: [*bool*] http redirect behavior
- <code>package</code>: [*string*] npm package name
- <code>reference</code>: [*string*] reference name
- <code>region</code>: [*string*] region name
- <code>registry</code>: [*string*] npm package registry
- <code>size</code>: [*int*] blob size
- <code>url</code>: [*string*] artifact or server url
- <code>verb</code>: [*string*] http request method
- <code>version</code>: [*string*] npm package version

The following predefined value types are supported:

Expand Down

0 comments on commit 53aee2b

Please sign in to comment.