Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for CLI Extensions by OCM Plugins #815

Merged
merged 20 commits into from
Jun 25, 2024

Conversation

mandelsoft
Copy link
Contributor

@mandelsoft mandelsoft commented Jun 14, 2024

What this PR does / why we need it:

The OCM Plugin framework now supports two new features:

  • definition of configuration types (consumed by the plugin)
  • definition of CLI commands (for the OCM CLI)

Additionally it is possible to consume logging configuration from the OCM CLI for all
plugin feature commands.

Examples see coding in cmds/cliplugin

Config Types

Config types are just registered at the Plugin Object;

	p := ppi.NewPlugin("cliplugin", version.Get().String())
        ...
	p.RegisterConfigType(configType)

The argument is just the config type as registered at the ocm library, for example:

const ConfigType = "rhabarber.config.acme.org"

type Config struct {
	runtime.ObjectVersionedType `json:",inline"`
	...
}

func (a *Config) ApplyTo(ctx cfgcpi.Context, target interface{}) error {
        ...
}

func init() {
	configType = cfgcpi.NewConfigType[*Config](ConfigType, usage)
	cfgcpi.RegisterConfigType(configType)
}

CLI Commands

CLI commands are simple configured cobra.Command objects.
They are registered at the plugin object with

        cmd, err := clicmd.NewCLICommand(NewCommand(), clicmd.WithCLIConfig(), clicmd.WithVerb("check"))
	if err != nil {
		os.Exit(1)
	}
	p.RegisterCommand(NewCommand())

with coding similar to


type command struct {
	date string
}

func NewCommand() *cobra.Command {
	cmd := &command{}
	c := &cobra.Command{
		Use:   Name + " <options>",
		Short: "determine whether we are in rhubarb season",
		Long:  "The rhubarb season is between march and april.",
		RunE:  cmd.Run,
	}

	c.Flags().StringVarP(&cmd.date, "date", "d", "", "the date to ask for (MM/DD)")
	return c
}

func (c *command) Run(cmd *cobra.Command, args []string) error {
   ...
}

The plugin programming interface supports the generation of an extension command directly from a
cobra command object using the method NewCLICommand from the ppi.clicmd package.
Otherwise the ppi.Command interface can be implemented without requiring a cobra command..

If the code wants to use the config framework, for example to

  • use the OCM library again
  • access credentials
  • get configured with declared config types

the appropriate command feature must be set.
For the cobra support this is implemented by the option WithCLIConfig.
If set to true, the OCM CLI configuration is available for the config context used in the
CLI code.

The command can be a top-level command or attached to a dedicated verb (and optionally a realm like ocmor oci).
For the cobra support this can be requested by the option WithVerb(...).

If the config framework is used just add the following anonymoud import
for an automated configuration:

import (
        // enable mandelsoft plugin logging configuration.
	_ "github.com/open-component-model/ocm/pkg/contexts/ocm/plugin/ppi/config"
)

The plugin code is then configured with the configuration of the OCM CLI and the config framework
can be used.
If the configuration should be handled by explicit plugin code a handler can be registered with

func init() {
	command.RegisterCommandConfigHandler(yourHandler)
}

It gets a config yaml according to the config objects used by the OCM library.

Logging

To get the logging configuration from the OCM CLI the plugin has be configured with

	p.ForwardLogging()

If the standard mandelsoft logging from the OCM library is used the configuration can
be implemented directly with an anonymous import of

import (
        // enable mandelsoft plugin logging configuration.
	_ "github.com/open-component-model/ocm/pkg/contexts/ocm/plugin/ppi/logging"
)

The plugin code is then configured with the logging configuration of the OCM CLI and the mandelsoft logging frame work
can be used.
If the logging configuration should be handled by explicit plugin code a handler can be registered with

func init() {
	cmds.RegisterLoggingConfigHandler(yourHandler)
}

It gets a logging configuration yaml according to the logging config used by the OCM library (github.com/mandelsoft/logging/config).

Using Plugin command extensions from the OCM library.

The plugin command extensions can also be called without the OCM CLI directly from the OCM library.
Therefore the plugin objects provided by the library can be used.

Logging information and config information must explicitly be configured to be passed to the
plugin.

Therefore the context attribute clicfgattr.Get(ctx) is used. It can be set via clicfgattr.Set(...).
The logging configuration is extracted from the configured configuration object with target type *logging.LoggingConfiguration.

If the code uses an OCM context configured with a (ocm)utils.ConfigureXXX function, the cli config attribute is set accordingly.

Which issue(s) this PR fixes:

@mandelsoft mandelsoft added the area/ipcei Important Project of Common European Interest label Jun 16, 2024
@mandelsoft mandelsoft force-pushed the command branch 2 times, most recently from 0cb4933 to 6bc33eb Compare June 17, 2024 16:24
@mandelsoft mandelsoft force-pushed the command branch 3 times, most recently from 0271ad3 to 5da689e Compare June 19, 2024 14:10
@mandelsoft mandelsoft marked this pull request as ready for review June 19, 2024 14:32
@mandelsoft mandelsoft force-pushed the command branch 2 times, most recently from 5158927 to 144b3cb Compare June 24, 2024 10:01
Makefile Outdated Show resolved Hide resolved
Makefile Show resolved Hide resolved
hilmarf
hilmarf previously approved these changes Jun 25, 2024
mandelsoft and others added 6 commits June 25, 2024 14:53
- verb creation
- additional topic help template
- capability summay in get plugins
Co-authored-by: Hilmar Falkenberg <[email protected]>
@mandelsoft
Copy link
Contributor Author

Just rebased and fixed the linter issues

@mandelsoft mandelsoft merged commit 5de0297 into open-component-model:main Jun 25, 2024
14 of 15 checks passed
@mandelsoft mandelsoft deleted the command branch June 25, 2024 13:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/ipcei Important Project of Common European Interest
Projects
Status: 🔒Closed
Development

Successfully merging this pull request may close these issues.

3 participants