Skip to content

Commit

Permalink
eliminate dep from demo plugin to cli (#746)
Browse files Browse the repository at this point in the history
## Description

<!-- 
Please do not leave this blank 
This PR [adds/removes/fixes/replaces] the [feature/bug/etc]. 
-->

Please include a summary of the changes and the related issue. Please
also include relevant motivation and context. List any dependencies that
are required for this change.


## What type of PR is this? (check all applicable)

- [ ] 🍕 Feature
- [x] 🎇 Restructuring
- [ ] 🐛 Bug Fix
- [ ] 📝 Documentation Update
- [ ] 🎨 Style
- [ ] 🧑‍💻 Code Refactor
- [ ] 🔥 Performance Improvements
- [x] ✅ Test
- [ ] 🤖 Build
- [ ] 🔁 CI
- [ ] 📦 Chore (Release)
- [ ] ⏩ Revert

## Related Tickets & Documents

<!-- 
Please use this format link issue numbers: Fixes #123

https://docs.github.com/en/free-pro-team@latest/github/managing-your-work-on-github/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword
-->
- Related Issue # (issue)
- Closes # (issue)
- Fixes # (issue)
> Remove if not applicable

## Screenshots

<!-- Visual changes require screenshots -->


## Added tests?

- [ ] 👍 yes
- [ ] 🙅 no, because they aren't needed
- [ ] 🙋 no, because I need help
- [ ] Separate ticket for tests # (issue/pr)

Please describe the tests that you ran to verify your changes. Provide
instructions so we can reproduce. Please also list any relevant details
for your test configuration


## Added to documentation?

- [ ] 📜 README.md
- [ ] 🙅 no documentation needed

## Checklist:

- [ ] My code follows the style guidelines of this project
- [ ] I have performed a self-review of my code
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [ ] My changes generate no new warnings
- [ ] I have added tests that prove my fix is effective or that my
feature works
- [ ] New and existing unit tests pass locally with my changes
- [ ] Any dependent changes have been merged and published in downstream
modules
  • Loading branch information
mandelsoft authored Apr 26, 2024
1 parent 09bb41c commit 4b9a12f
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 40 deletions.
126 changes: 96 additions & 30 deletions cmds/demoplugin/valuesets/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,24 @@

//go:build unix

package valuesets
package valuesets_test

import (
"bytes"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
. "github.com/open-component-model/ocm/pkg/env"
. "github.com/open-component-model/ocm/pkg/env/builder"
. "github.com/open-component-model/ocm/pkg/testutils"

"github.com/open-component-model/ocm/cmds/ocm/testhelper"
"github.com/open-component-model/ocm/pkg/common/accessio"
"github.com/spf13/pflag"

"github.com/open-component-model/ocm/pkg/contexts/ocm/attrs/plugincacheattr"
"github.com/open-component-model/ocm/pkg/contexts/ocm/attrs/plugindirattr"
"github.com/open-component-model/ocm/pkg/contexts/ocm/labels/routingslip"
"github.com/open-component-model/ocm/pkg/contexts/ocm/plugin/cache"
"github.com/open-component-model/ocm/pkg/contexts/ocm/registration"
"github.com/open-component-model/ocm/pkg/contexts/ocm/repositories/composition"
"github.com/open-component-model/ocm/pkg/signing/handlers/rsa"
)

const ARCH = "/tmp/ctf"
Expand All @@ -25,39 +30,100 @@ const VERS = "1.0.0"
const PROV = "acme.org"

var _ = Describe("demoplugin", func() {
var env *testhelper.TestEnv
/*
Context("cli", func() {
var env *testhelper.TestEnv
BeforeEach(func() {
env = testhelper.NewTestEnv(testhelper.TestData())
cache.DirectoryCache.Reset()
plugindirattr.Set(env.OCMContext(), "testdata")
env.OCMCommonTransport(ARCH, accessio.FormatDirectory, func() {
env.ComponentVersion(COMP, VERS, func() {
env.Provider(PROV)
})
})
env.RSAKeyPair(PROV)
})
AfterEach(func() {
env.Cleanup()
})
It("add check routing slip entry", func() {
buf := bytes.NewBuffer(nil)
MustBeSuccessful(env.CatchOutput(buf).Execute("add", "routingslip", ARCH, PROV, "check", "--checkStatus", "test=passed", "--checkMessage", "test=25 tests successful"))
Expect(buf.String()).To(Equal(""))
buf.Reset()
MustBeSuccessful(env.CatchOutput(buf).Execute("get", "routingslip", ARCH, PROV))
Expect(buf.String()).To(StringMatchTrimmedWithContext(`
COMPONENT-VERSION NAME TYPE TIMESTAMP DESCRIPTION
acme.org/test:1.0.0 acme.org check .{20} test: passed
`))
buf.Reset()
MustBeSuccessful(env.CatchOutput(buf).Execute("get", "routingslip", ARCH, PROV, "-oyaml"))
Expect(buf.String()).To(StringMatchTrimmedWithContext(`message: 25 tests successful`))
})
})
*/

Context("lib", func() {
var env *Builder

BeforeEach(func() {
env = testhelper.NewTestEnv()
BeforeEach(func() {
env = NewBuilder(TestData())

cache.DirectoryCache.Reset()
plugindirattr.Set(env.OCMContext(), "testdata")
cache.DirectoryCache.Reset()
plugindirattr.Set(env.OCMContext(), "testdata")

env.OCMCommonTransport(ARCH, accessio.FormatDirectory, func() {
env.ComponentVersion(COMP, VERS, func() {
env.Provider(PROV)
registry := plugincacheattr.Get(env)
Expect(registration.RegisterExtensions(env)).To(Succeed())
p := registry.Get("demo")
Expect(p).NotTo(BeNil())

env.OCMCompositionRepository("test", func() {
env.ComponentVersion(COMP, VERS, func() {
env.Provider(PROV)
})
})
env.RSAKeyPair(PROV)
})
env.RSAKeyPair(PROV)
})

AfterEach(func() {
env.Cleanup()
})
AfterEach(func() {
env.Cleanup()
})

It("add check routing slip entry", func() {
fs := &pflag.FlagSet{}
prov := routingslip.For(env).CreateConfigTypeSetConfigProvider()
configopts := prov.CreateOptions()
configopts.AddFlags(fs)

It("add check routing slip entry", func() {
buf := bytes.NewBuffer(nil)
MustBeSuccessful(env.CatchOutput(buf).Execute("add", "routingslip", ARCH, PROV, "check", "--checkStatus", "test=passed", "--checkMessage", "test=25 tests successful"))
Expect(buf.String()).To(Equal(""))
MustBeSuccessful(fs.Parse([]string{"--checkStatus", "test=passed", "--checkMessage", "test=25 tests successful"}))
prov.SetTypeName("check")
data := Must(prov.GetConfigFor(configopts))

buf.Reset()
MustBeSuccessful(env.CatchOutput(buf).Execute("get", "routingslip", ARCH, PROV))
Expect(buf.String()).To(StringMatchTrimmedWithContext(`
COMPONENT-VERSION NAME TYPE TIMESTAMP DESCRIPTION
acme.org/test:1.0.0 acme.org check .{20} test: passed
Expect(data).To(YAMLEqual(`
type: check
checks:
test:
status: passed
message: 25 tests successful
`))
buf.Reset()
MustBeSuccessful(env.CatchOutput(buf).Execute("get", "routingslip", ARCH, PROV, "-oyaml"))
Expect(buf.String()).To(StringMatchTrimmedWithContext(`message: 25 tests successful`))

entry := Must(routingslip.NewGenericEntry("", data))
MustBeSuccessful(entry.Validate(env.OCMContext()))

repo := composition.NewRepository(env, "test")
defer Close(repo, "repo")

cv := Must(repo.LookupComponentVersion(COMP, VERS))
defer Close(cv, "cv")

Must(routingslip.AddEntry(cv, PROV, rsa.Algorithm, entry, nil))
})
})
})
2 changes: 1 addition & 1 deletion cmds/ocm/commands/ocmcmds/routingslips/add/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ $ ocm add routingslip ghcr.io/mandelsoft/ocm//ocmdemoinstaller:0.0.1-dev mandels
}

func (o *Command) AddFlags(fs *pflag.FlagSet) {
o.prov = routingslip.For(o.OCMContext()).CreateConfigTypeSetConfigProvider().(flagsets.ExplicitlyTypedConfigTypeOptionSetConfigProvider)
o.prov = routingslip.For(o.OCMContext()).CreateConfigTypeSetConfigProvider()
o.configopts = o.prov.CreateOptions()
o.configopts.AddFlags(fs)

Expand Down
41 changes: 38 additions & 3 deletions pkg/cobrautils/flagsets/flagsetscheme/scheme.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,43 @@ type VersionTypedObjectType[T runtime.VersionedTypedObject] interface {

////////////////////////////////////////////////////////////////////////////////

// ExtendedTypeScheme is the appropriately extended scheme interface based on
// runtime.TypeScheme supporting an extended config provider interface.
type ExtendedTypeScheme[T runtime.VersionedTypedObject, R VersionTypedObjectType[T], P flagsets.ConfigTypeOptionSetConfigProvider] interface {
descriptivetype.TypeScheme[T, R]

CreateConfigTypeSetConfigProvider() P

Unwrap() TypeScheme[T, R]
}

type _TypeScheme[T runtime.VersionedTypedObject, R VersionTypedObjectType[T]] interface {
TypeScheme[T, R]
}

type typeSchemeWrapper[T runtime.VersionedTypedObject, R VersionTypedObjectType[T], P flagsets.ConfigTypeOptionSetConfigProvider] struct {
_TypeScheme[T, R]
}

func (s *typeSchemeWrapper[T, R, P]) CreateConfigTypeSetConfigProvider() P {
return s._TypeScheme.CreateConfigTypeSetConfigProvider().(P)
}

func (s *typeSchemeWrapper[T, R, P]) Unwrap() TypeScheme[T, R] {
return s._TypeScheme
}

// NewTypeSchemeWrapper wraps a [TypeScheme] into a scheme returning a specialized config provider
// by casting the result. The type scheme constructor provides different implementations based on its
// arguments. This method here can be used to provide a type scheme returning the correct type.
func NewTypeSchemeWrapper[T runtime.VersionedTypedObject, R VersionTypedObjectType[T], P flagsets.ConfigTypeOptionSetConfigProvider](s TypeScheme[T, R]) ExtendedTypeScheme[T, R, P] {
return &typeSchemeWrapper[T, R, P]{s}
}

// TypeScheme is the appropriately extended scheme interface based on
// runtime.TypeScheme.
type TypeScheme[T runtime.VersionedTypedObject, R VersionTypedObjectType[T]] interface {
descriptivetype.TypeScheme[T, R]

CreateConfigTypeSetConfigProvider() flagsets.ConfigTypeOptionSetConfigProvider
ExtendedTypeScheme[T, R, flagsets.ConfigTypeOptionSetConfigProvider]
}

type _typeScheme[T runtime.VersionedTypedObject, R VersionTypedObjectType[T]] interface {
Expand Down Expand Up @@ -62,6 +93,10 @@ func NewTypeScheme[T runtime.VersionedTypedObject, R VersionTypedObjectType[T],
}
}

func (s *typeScheme[T, R, S]) Unwrap() TypeScheme[T, R] {
return s
}

func (t *typeScheme[T, R, S]) CreateConfigTypeSetConfigProvider() flagsets.ConfigTypeOptionSetConfigProvider {
var prov flagsets.ConfigTypeOptionSetConfigProvider
if t.typeOption == "" {
Expand Down
3 changes: 2 additions & 1 deletion pkg/contexts/ocm/attrs/plugincacheattr/attr.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ const (

////////////////////////////////////////////////////////////////////////////////

func Get(ctx ocm.Context) plugins.Set {
func Get(ctxp ocm.ContextProvider) plugins.Set {
ctx := ctxp.OCMContext()
path := plugindirattr.Get(ctx)

// avoid dead lock reading attribute during attribute creation
Expand Down
12 changes: 9 additions & 3 deletions pkg/contexts/ocm/labels/routingslip/internal/entrytypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ import (
"reflect"
"strings"

"github.com/mandelsoft/goutils/sliceutils"
"github.com/modern-go/reflect2"

"github.com/open-component-model/ocm/pkg/cobrautils/flagsets"
"github.com/open-component-model/ocm/pkg/cobrautils/flagsets/flagsetscheme"
"github.com/open-component-model/ocm/pkg/contexts/ocm/cpi"
"github.com/open-component-model/ocm/pkg/errors"
Expand Down Expand Up @@ -42,14 +44,18 @@ type (

////////////////////////////////////////////////////////////////////////////////

type EntryTypeScheme = flagsetscheme.TypeScheme[Entry, EntryType]
type EntryTypeScheme = flagsetscheme.ExtendedTypeScheme[Entry, EntryType, flagsets.ExplicitlyTypedConfigTypeOptionSetConfigProvider]

func unwrapTypeScheme(s EntryTypeScheme) flagsetscheme.TypeScheme[Entry, EntryType] {
return s.Unwrap()
}

func NewEntryTypeScheme(base ...EntryTypeScheme) EntryTypeScheme {
return flagsetscheme.NewTypeScheme[Entry, EntryType, EntryTypeScheme]("Entry type", "entry", "", "routing slip entry specification", "Entry Specification Options", &UnknownEntry{}, true, base...)
return flagsetscheme.NewTypeSchemeWrapper[Entry, EntryType, flagsets.ExplicitlyTypedConfigTypeOptionSetConfigProvider](flagsetscheme.NewTypeScheme[Entry, EntryType, flagsetscheme.TypeScheme[Entry, EntryType]]("Entry type", "entry", "", "routing slip entry specification", "Entry Specification Options", &UnknownEntry{}, true, sliceutils.Transform(base, unwrapTypeScheme)...))
}

func NewStrictEntryTypeScheme(base ...EntryTypeScheme) EntryTypeScheme {
return flagsetscheme.NewTypeScheme[Entry, EntryType, EntryTypeScheme]("Entry type", "entry", "", "routing slip entry specification", "Entry Specification Options", nil, false, base...)
return flagsetscheme.NewTypeSchemeWrapper[Entry, EntryType, flagsets.ExplicitlyTypedConfigTypeOptionSetConfigProvider](flagsetscheme.NewTypeScheme[Entry, EntryType, flagsetscheme.TypeScheme[Entry, EntryType]]("Entry type", "entry", "", "routing slip entry specification", "Entry Specification Options", nil, false, sliceutils.Transform(base, unwrapTypeScheme)...))
}

func CreateEntry(t runtime.TypedObject) (Entry, error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ var _ = Describe("Test Environment", func() {
})

It("handles plugin based entry type", func() {
prov := routingslip.For(env.OCMContext()).CreateConfigTypeSetConfigProvider().(flagsets.ExplicitlyTypedConfigTypeOptionSetConfigProvider)
prov := routingslip.For(env.OCMContext()).CreateConfigTypeSetConfigProvider()
configopts := prov.CreateOptions()
Expect(sliceutils.Transform(configopts.Options(), transformer.GetName[flagsets.Option, string])).To(ConsistOf(
"entry", "comment", // default settings
Expand Down
3 changes: 2 additions & 1 deletion pkg/contexts/ocm/registration/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ import (
)

// RegisterExtensions registers all the extension provided by the found plugin.
func RegisterExtensions(ctx ocm.Context) error {
func RegisterExtensions(ctxp ocm.ContextProvider) error {
ctx := ctxp.OCMContext()
pi := plugincacheattr.Get(ctx)

logger := Logger(ctx)
Expand Down

0 comments on commit 4b9a12f

Please sign in to comment.