Skip to content

Commit

Permalink
fix output tests + cleanup import cycles
Browse files Browse the repository at this point in the history
  • Loading branch information
mandelsoft committed Apr 3, 2023
1 parent 353b42c commit aefe80e
Show file tree
Hide file tree
Showing 35 changed files with 335 additions and 166 deletions.
7 changes: 4 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ build: ${SOURCES}
go build -ldflags $(BUILD_FLAGS) -o bin/ocm ./cmds/ocm
go build -ldflags $(BUILD_FLAGS) -o bin/helminstaller ./cmds/helminstaller
go build -ldflags $(BUILD_FLAGS) -o bin/demo ./cmds/demoplugin
go build -ldflags $(BUILD_FLAGS) -o bin/ecrplugin ./cmds/ecrplugin


.PHONY: install-requirements
Expand All @@ -37,15 +38,15 @@ prepare: generate format build test check

.PHONY: format
format:
@$(REPO_ROOT)/hack/format.sh $(REPO_ROOT)/pkg $(REPO_ROOT)/cmds/ocm $(REPO_ROOT)/cmds/helminstaller
@$(REPO_ROOT)/hack/format.sh $(REPO_ROOT)/pkg $(REPO_ROOT)/cmds/ocm $(REPO_ROOT)/cmds/helminstaller $(REPO_ROOT)/cmds/ecrplugin $(REPO_ROOT)/cmds/demoplugin

.PHONY: check
check:
@$(REPO_ROOT)/hack/check.sh --golangci-lint-config=./.golangci.yaml $(REPO_ROOT)/cmds/ocm $(REPO_ROOT)/cmds/helminstaller/... $(REPO_ROOT)/pkg/...
@$(REPO_ROOT)/hack/check.sh --golangci-lint-config=./.golangci.yaml $(REPO_ROOT)/cmds/ocm $(REPO_ROOT)/cmds/helminstaller/... $(REPO_ROOT)/cmds/ecrplugin/... $(REPO_ROOT)/cmds/demoplugin/... $(REPO_ROOT)/pkg/...

.PHONY: force-test
force-test:
@go test --count=1 $(REPO_ROOT)/cmds/ocm $(REPO_ROOT)/cmds/helminstaller $(REPO_ROOT)/cmds/ocm/... $(REPO_ROOT)/pkg/...
@go test --count=1 $(REPO_ROOT)/cmds/ocm $(REPO_ROOT)/cmds/helminstaller $(REPO_ROOT)/cmds/ocm/... $(REPO_ROOT)/cmds/ecrplugin/... $(REPO_ROOT)/cmds/demoplugin/... $(REPO_ROOT)/pkg/...

.PHONY: test
test:
Expand Down
8 changes: 5 additions & 3 deletions cmds/demoplugin/accessmethods/demo.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
"strings"

"github.com/mandelsoft/filepath/pkg/filepath"
"github.com/open-component-model/ocm/cmds/demoplugin/common"

"github.com/open-component-model/ocm/cmds/demoplugin/common"
"github.com/open-component-model/ocm/cmds/demoplugin/config"
"github.com/open-component-model/ocm/pkg/cobrautils/flagsets"
"github.com/open-component-model/ocm/pkg/contexts/credentials"
Expand All @@ -35,8 +35,10 @@ type AccessSpec struct {
MediaType string `json:"mediaType,omitempty"`
}

const OPT_PATH = "path"
const OPT_MEDIA = "mediaType"
const (
OPT_PATH = "path"
OPT_MEDIA = "mediaType"
)

type AccessMethod struct {
ppi.AccessMethodBase
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
"github.com/open-component-model/ocm/pkg/contexts/clictx"
"github.com/open-component-model/ocm/pkg/contexts/ocm/attrs/plugincacheattr"
"github.com/open-component-model/ocm/pkg/contexts/ocm/plugin"
"github.com/open-component-model/ocm/pkg/contexts/ocm/plugin/descriptor"
"github.com/open-component-model/ocm/pkg/contexts/ocm/plugin/plugins"
"github.com/open-component-model/ocm/pkg/contexts/ocm/plugin/ppi"
"github.com/open-component-model/ocm/pkg/errors"
)

Expand Down Expand Up @@ -70,7 +70,7 @@ func (h *TypeHandler) Get(elemspec utils.ElemSpec) ([]output.Object, error) {
if p == nil {
objs := Lookup(elemspec.String(), cache)
if len(objs) == 0 {
return nil, errors.ErrNotFound(ppi.KIND_PLUGIN, elemspec.String())
return nil, errors.ErrNotFound(descriptor.KIND_PLUGIN, elemspec.String())
}
return objs, nil
}
Expand Down
3 changes: 1 addition & 2 deletions cmds/ocm/commands/ocmcmds/plugins/describe/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

handler "github.com/open-component-model/ocm/cmds/ocm/commands/ocmcmds/common/handlers/pluginhdlr"
"github.com/open-component-model/ocm/cmds/ocm/commands/ocmcmds/names"
common2 "github.com/open-component-model/ocm/cmds/ocm/commands/ocmcmds/plugins/common"
"github.com/open-component-model/ocm/cmds/ocm/commands/verbs"
"github.com/open-component-model/ocm/cmds/ocm/pkg/utils"
"github.com/open-component-model/ocm/pkg/cobrautils"
Expand Down Expand Up @@ -81,7 +80,7 @@ func (a *action) Add(e interface{}) error {
p := handler.Elem(e)

out, buf := common.NewBufferedPrinter()
common2.DescribePlugin(p, out)
DescribePlugin(p, out)
if a.Count > 1 {
a.Printer.Printf("----------------------\n")
}
Expand Down
1 change: 1 addition & 0 deletions cmds/ocm/commands/ocmcmds/plugins/describe/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ Actions:
test action
Versions:
- v1 (best matching)
Handler accepts standard credentials
----------------------
Plugin Name: test
Plugin Version: v1
Expand Down
134 changes: 134 additions & 0 deletions cmds/ocm/commands/ocmcmds/plugins/describe/describe.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
// SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company and Open Component Model contributors.
//
// SPDX-License-Identifier: Apache-2.0

package describe

import (
"encoding/json"
"strings"

"github.com/open-component-model/ocm/pkg/common"
"github.com/open-component-model/ocm/pkg/contexts/ocm/plugin"
plugincommon "github.com/open-component-model/ocm/pkg/contexts/ocm/plugin/common"
utils2 "github.com/open-component-model/ocm/pkg/utils"
)

func DescribePlugin(p plugin.Plugin, out common.Printer) {
out.Printf("Plugin Name: %s\n", p.Name())
out.Printf("Plugin Version: %s\n", p.Version())
out.Printf("Path: %s\n", p.Path())

if !p.IsValid() {
out.Printf("Status: %s\n", p.Error())
return
}
out.Printf("Status: %s\n", "valid")
var caps []string
d := p.GetDescriptor()
if len(d.AccessMethods) > 0 {
caps = append(caps, "Access Methods")
}
if len(d.Uploaders) > 0 {
caps = append(caps, "Repository Uploaders")
}
if len(d.Downloaders) > 0 {
caps = append(caps, "Resource Downloaders")
}
if len(d.Actions) > 0 {
caps = append(caps, "Actions")
}
if len(caps) == 0 {
out.Printf("Capabilities: none\n")
} else {
out.Printf("Capabilities: %s\n", strings.Join(caps, ", "))
}
src := p.GetSource()
if src != nil {
out.Printf("Source:\n")
out.Printf(" Component: %s\n", src.Component)
out.Printf(" Version: %s\n", src.Version)
out.Printf(" Resource: %s\n", src.Resource)
u := src.Repository.AsUniformSpec(p.Context())
data, _ := json.Marshal(src.Repository)
out.Printf(" Repository: %s\n", u.String())
out.Printf(" Specification: %s\n", string(data))
} else {
out.Printf("Source: manually installed\n")
}
out.Printf("\n")
out.Printf("Description: \n")
if d.Long == "" {
out.Printf("%s\n", utils2.IndentLines(d.Short, " "))
} else {
out.Printf("%s\n", utils2.IndentLines(d.Long, " "))
}
if len(d.AccessMethods) > 0 {
out.Printf("\n")
out.Printf("Access Methods:\n")
plugincommon.DescribeAccessMethods(d, out)
}
if len(d.Uploaders) > 0 {
out.Printf("\n")
// a working type inference would be really great
plugincommon.ListElements[plugin.UploaderDescriptor, plugin.UploaderKey]("Repository Uploaders", d.Uploaders, out)
}
if len(d.Downloaders) > 0 {
out.Printf("\n")
plugincommon.ListElements[plugin.DownloaderDescriptor, plugin.DownloaderKey]("Resource Downloaders", d.Downloaders, out)
}
if len(d.Actions) > 0 {
out.Printf("\n")
out.Printf("Actions:\n")
plugincommon.DescribeActions(d, out)
}
}

func DescribePluginDescriptor(d *plugin.Descriptor, out common.Printer) {
out.Printf("Plugin Name: %s\n", d.PluginName)
out.Printf("Plugin Version: %s\n", d.PluginVersion)

var caps []string
if len(d.AccessMethods) > 0 {
caps = append(caps, "Access Methods")
}
if len(d.Uploaders) > 0 {
caps = append(caps, "Repository Uploaders")
}
if len(d.Downloaders) > 0 {
caps = append(caps, "Resource Downloaders")
}
if len(d.Actions) > 0 {
caps = append(caps, "Actions")
}
if len(caps) == 0 {
out.Printf("Capabilities: none\n")
} else {
out.Printf("Capabilities: %s\n", strings.Join(caps, ", "))
}
out.Printf("Description: \n")
if d.Long == "" {
out.Printf("%s\n", utils2.IndentLines(d.Short, " "))
} else {
out.Printf("%s\n", utils2.IndentLines(d.Long, " "))
}
if len(d.AccessMethods) > 0 {
out.Printf("\n")
out.Printf("Access Methods:\n")
plugincommon.DescribeAccessMethods(d, out)
}
if len(d.Uploaders) > 0 {
out.Printf("\n")
// a working type inference would be really great
plugincommon.ListElements[plugin.UploaderDescriptor, plugin.UploaderKey]("Repository Uploaders", d.Uploaders, out)
}
if len(d.Downloaders) > 0 {
out.Printf("\n")
plugincommon.ListElements[plugin.DownloaderDescriptor, plugin.DownloaderKey]("Resource Downloaders", d.Downloaders, out)
}
if len(d.Actions) > 0 {
out.Printf("\n")
out.Printf("Actions:\n")
plugincommon.DescribeActions(d, out)
}
}
4 changes: 2 additions & 2 deletions cmds/ocm/commands/ocmcmds/plugins/get/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ import (

handler "github.com/open-component-model/ocm/cmds/ocm/commands/ocmcmds/common/handlers/pluginhdlr"
"github.com/open-component-model/ocm/cmds/ocm/commands/ocmcmds/names"
"github.com/open-component-model/ocm/cmds/ocm/commands/ocmcmds/plugins/common"
"github.com/open-component-model/ocm/cmds/ocm/commands/verbs"
"github.com/open-component-model/ocm/cmds/ocm/pkg/output"
"github.com/open-component-model/ocm/cmds/ocm/pkg/processing"
"github.com/open-component-model/ocm/cmds/ocm/pkg/utils"
"github.com/open-component-model/ocm/pkg/contexts/clictx"
"github.com/open-component-model/ocm/pkg/contexts/ocm/plugin"
"github.com/open-component-model/ocm/pkg/contexts/ocm/plugin/common"
"github.com/open-component-model/ocm/pkg/generics"
utils2 "github.com/open-component-model/ocm/pkg/utils"
)
Expand Down Expand Up @@ -88,7 +88,7 @@ var outputs = output.NewOutputs(getRegular, output.Outputs{
}).AddManifestOutputs()

func getRegular(opts *output.Options) output.Output {
return TableOutput(opts, mapGetRegularOutput, "FEATURES").New()
return TableOutput(opts, mapGetRegularOutput, "CAPABILITIES").New()
}

func getWide(opts *output.Options) output.Output {
Expand Down
7 changes: 4 additions & 3 deletions cmds/ocm/commands/ocmcmds/plugins/get/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

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

. "github.com/open-component-model/ocm/cmds/ocm/testhelper"
. "github.com/open-component-model/ocm/pkg/testutils"
)
Expand Down Expand Up @@ -39,16 +40,16 @@ var _ = Describe("Test Environment", func() {
Expect(env.CatchOutput(buf).Execute("-X", "plugindir="+path, "get", "plugins")).To(Succeed())
Expect(buf.String()).To(StringEqualTrimmedWithContext(
`
PLUGIN VERSION SOURCE DESCRIPTION
test v1 local a test plugin without function
PLUGIN VERSION SOURCE DESCRIPTION CAPABILITIES
test v1 local a test plugin without function accessmethods
`))
})
It("get plugins with additional info", func() {
buf := bytes.NewBuffer(nil)
Expect(env.CatchOutput(buf).Execute("-X", "plugindir="+path, "get", "plugins", "-o", "wide")).To(Succeed())
Expect(buf.String()).To(StringEqualTrimmedWithContext(
`
PLUGIN VERSION SOURCE DESCRIPTION ACCESSMETHODS UPLOADERS DOWNLOADERS
PLUGIN VERSION SOURCE DESCRIPTION ACCESSMETHODS UPLOADERS DOWNLOADERS ACTIONS
test v1 local a test plugin without function test[v1]
`))
})
Expand Down
1 change: 1 addition & 0 deletions docs/pluginreference/plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ apabilities of the plugin.

* [plugin <b>accessmethod</b>](plugin_accessmethod.md) &mdash; access method operations
* [plugin <b>action</b>](plugin_action.md) &mdash; action operations
* [plugin <b>describe</b>](plugin_describe.md) &mdash; describe plugin
* [plugin <b>download</b>](plugin_download.md) &mdash; download blob into filesystem
* [plugin <b>info</b>](plugin_info.md) &mdash; show plugin descriptor
* [plugin <b>upload</b>](plugin_upload.md) &mdash; upload specific operations
Expand Down
24 changes: 24 additions & 0 deletions docs/pluginreference/plugin_describe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
## plugin describe &mdash; Describe Plugin

### Synopsis

```
plugin describe [<options>]
```

### Options

```
-h, --help help for describe
```

### Description

Display a detailed description of the capabilities of this OCM plugin.

### SEE ALSO

##### Parents

* [plugin](plugin.md) &mdash; OCM Plugin

Binary file added ecrplugin
Binary file not shown.
3 changes: 2 additions & 1 deletion pkg/contexts/ocm/accessmethods/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/open-component-model/ocm/pkg/contexts/credentials/identity/hostpath"
"github.com/open-component-model/ocm/pkg/contexts/ocm/cpi"
"github.com/open-component-model/ocm/pkg/contexts/ocm/plugin"
"github.com/open-component-model/ocm/pkg/contexts/ocm/plugin/descriptor"
"github.com/open-component-model/ocm/pkg/contexts/ocm/plugin/ppi"
"github.com/open-component-model/ocm/pkg/errors"
)
Expand Down Expand Up @@ -48,7 +49,7 @@ func (p *PluginHandler) Info(spec *AccessSpec) (*ppi.AccessSpecInfo, error) {
func (p *PluginHandler) AccessMethod(spec *AccessSpec, cv cpi.ComponentVersionAccess) (cpi.AccessMethod, error) {
mspec := p.GetAccessMethodDescriptor(spec.GetKind(), spec.GetVersion())
if mspec == nil {
return nil, errors.ErrNotFound(errors.KIND_ACCESSMETHOD, spec.GetType(), ppi.KIND_PLUGIN, p.Name())
return nil, errors.ErrNotFound(errors.KIND_ACCESSMETHOD, spec.GetType(), descriptor.KIND_PLUGIN, p.Name())
}
info, err := p.Info(spec)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/contexts/ocm/blobhandler/generic/plugin/blobhandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/open-component-model/ocm/pkg/contexts/credentials/identity/hostpath"
"github.com/open-component-model/ocm/pkg/contexts/ocm/cpi"
"github.com/open-component-model/ocm/pkg/contexts/ocm/plugin"
"github.com/open-component-model/ocm/pkg/contexts/ocm/plugin/ppi"
"github.com/open-component-model/ocm/pkg/contexts/ocm/plugin/descriptor"
"github.com/open-component-model/ocm/pkg/errors"
)

Expand All @@ -29,7 +29,7 @@ func New(p plugin.Plugin, name string, target json.RawMessage) (cpi.BlobHandler,

ud := p.GetUploaderDescriptor(name)
if ud == nil {
return nil, errors.ErrUnknown(ppi.KIND_UPLOADER, name, p.Name())
return nil, errors.ErrUnknown(descriptor.KIND_UPLOADER, name, p.Name())
}

var info *plugin.UploadTargetSpecInfo
Expand Down
4 changes: 2 additions & 2 deletions pkg/contexts/ocm/download/handlers/plugin/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/open-component-model/ocm/pkg/contexts/ocm/cpi"
"github.com/open-component-model/ocm/pkg/contexts/ocm/download"
"github.com/open-component-model/ocm/pkg/contexts/ocm/plugin"
"github.com/open-component-model/ocm/pkg/contexts/ocm/plugin/ppi"
"github.com/open-component-model/ocm/pkg/contexts/ocm/plugin/descriptor"
"github.com/open-component-model/ocm/pkg/errors"
)

Expand All @@ -25,7 +25,7 @@ type pluginHandler struct {
func New(p plugin.Plugin, name string) (download.Handler, error) {
dd := p.GetDownloaderDescriptor(name)
if dd == nil {
return nil, errors.ErrUnknown(ppi.KIND_DOWNLOADER, name, p.Name())
return nil, errors.ErrUnknown(descriptor.KIND_DOWNLOADER, name, p.Name())
}

return &pluginHandler{
Expand Down
Loading

0 comments on commit aefe80e

Please sign in to comment.