-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
203 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
31
cmds/ocm/commands/ocmcmds/common/inputs/types/npm/input_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
13
cmds/ocm/commands/ocmcmds/common/inputs/types/npm/suite_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters