plugins

package
v0.1.0-alpha.9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 12, 2025 License: MIT Imports: 13 Imported by: 0

README

Nexlayer CLI Plugins

Welcome to the Nexlayer CLI Plugin System! Extend the capabilities of Nexlayer CLI by building your own plugins. Whether you want to provide AI-powered deployment insights, integrate new commands, or enhance your deployment workflow, our plugin system is designed to be simple, flexible, and powerful.

Table of Contents

Introduction

The Nexlayer CLI supports plugins to let you extend its functionality without modifying the core code. Our plugin system is built for speed and simplicity. It allows you to:

  • Dynamically load and initialize plugins at runtime.
  • Add new CLI commands that integrate seamlessly with Nexlayer Cloud's deployment behavior.
  • Provide structured output that is both human-readable and machine-parsable (JSON) for AI-powered editors like Cursor or Windsurf.

Overview of the Plugin System

The plugin system consists of several key components:

  • Plugin Interface: A contract that every plugin must implement (Name, Description, Version, Commands, Init, and Run).
  • Plugin Manager: Handles scanning a directory for plugins, loading shared libraries (.so files), and aggregating any additional commands.
  • Dependencies Injection: Plugins receive common dependencies (API client, Logger, and UI Manager) so they can interact with Nexlayer Cloud consistently.

This design ensures that plugins integrate perfectly with the Nexlayer CLI, following the same YAML templating and API behavior used for deployments.

Plugin Interface

Every plugin must implement the following interface:

type Plugin interface {
    // Name returns the plugin's name.
    Name() string
    // Description returns a description of what the plugin does.
    Description() string
    // Version returns the plugin version.
    Version() string
    // Commands returns any additional CLI commands provided by the plugin.
    Commands() []*cobra.Command
    // Init initializes the plugin with dependencies.
    Init(deps *PluginDependencies) error
    // Run executes the plugin with the given options.
    Run(opts map[string]interface{}) error
}

Plugins receive their dependencies via the PluginDependencies structure:

type PluginDependencies struct {
    APIClient api.APIClient
    Logger    *observability.Logger
    UIManager ui.Manager
}

The plugin manager will automatically load plugins from the specified directory, look up the exported Plugin symbol, and call its Init method to wire in these dependencies.

Smart Deployments Plugin Example

The Smart Deployments Plugin is a great example of how to extend Nexlayer CLI. It provides AI-powered recommendations for deployment optimizations such as scaling, performance tuning, and pre-deployment audits.

Key Features

Deployment Recommendations:

  • Suggests changes to optimize resource allocation, such as increasing CPU limits for backend pods.

Scaling Advice:

  • Recommends scaling configurations based on current traffic and usage patterns.

Performance Tuning:

  • Identifies potential bottlenecks in your deployment (e.g., suboptimal environment variable settings or misconfigured ports).

Pre-deployment Audit:

  • Runs an audit of your deployment configuration before you deploy.
Example Usage
# Get deployment optimization recommendations:
nexlayer recommend deploy --ai --deploy

# Get resource scaling recommendations:
nexlayer recommend scale --ai --scale

# Get performance tuning recommendations:
nexlayer recommend performance --ai --performance

# Run a full pre-deployment audit:
nexlayer recommend audit --ai --pre-deploy

When the --json flag is added, the plugin outputs its recommendations in structured JSON format, making it easy for AI-powered editors to process the results.

How to Build Your Own Plugin

Building a plugin for Nexlayer CLI is straightforward. Follow these steps:

1. Implement the Plugin Interface

Create a new Go file (e.g., myplugin.go) and implement the Plugin interface. For example:

package main

import (
    "context"
    "fmt"
    "github.com/spf13/cobra"
    "github.com/Nexlayer/nexlayer-cli/pkg/observability"
    "github.com/Nexlayer/nexlayer-cli/pkg/core/api"
    "github.com/Nexlayer/nexlayer-cli/pkg/ui"
)

// MyPlugin is a sample plugin implementation.
type MyPlugin struct{}

func (p *MyPlugin) Name() string {
    return "my-plugin"
}

func (p *MyPlugin) Description() string {
    return "A sample plugin that demonstrates how to extend Nexlayer CLI."
}

func (p *MyPlugin) Version() string {
    return "1.0.0"
}

func (p *MyPlugin) Commands() []*cobra.Command {
    cmd := &cobra.Command{
        Use:   "hello",
        Short: "Prints a hello message",
        Run: func(cmd *cobra.Command, args []string) {
            fmt.Println("Hello from MyPlugin!")
        },
    }
    return []*cobra.Command{cmd}
}

func (p *MyPlugin) Init(deps *PluginDependencies) error {
    // Optionally use deps.APIClient, deps.Logger, or deps.UIManager.
    return nil
}

func (p *MyPlugin) Run(opts map[string]interface{}) error {
    // Implement non-interactive behavior if needed.
    return nil
}

// Export the plugin symbol.
var Plugin MyPlugin
2. Compile as a Shared Library

Use the following command to compile your plugin:

go build -buildmode=plugin -o myplugin.so myplugin.go
3. Place the Plugin

Copy the resulting myplugin.so into the default plugins directory or specify its path when running Nexlayer CLI.

4. Test and Contribute

Run nexlayer plugin list to verify your plugin is loaded. Then, you can start using the commands provided by your plugin and share your work with the Nexlayer community!

Usage Examples

List All Plugins:

nexlayer plugin list

Run a Plugin Command:

nexlayer plugin run my-plugin --name "Sample"

Use the Smart Deployments Plugin:

nexlayer recommend deploy --ai --deploy

Contributing

We welcome contributions from the community! If you have a plugin idea or have built one that enhances Nexlayer CLI, please submit a pull request or open an issue in our GitHub repository.

When contributing your plugin:

  1. Follow the Plugin Interface guidelines.
  2. Provide clear documentation and usage examples.
  3. Ensure your plugin outputs structured logs (with a --json flag) to support both human users and AI-powered code editors.

Additional Resources

Happy developing! Extend Nexlayer CLI to make deploying full-stack AI-powered applications even easier!

Documentation

Overview

Copyright (c) 2025 Nexlayer. All rights reserved.n// Use of this source code is governed by an MIT-stylen// license that can be found in the LICENSE file.nn

Copyright (c) 2025 Nexlayer. All rights reserved.n// Use of this source code is governed by an MIT-stylen// license that can be found in the LICENSE file.nn

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

Manager is responsible for loading, initializing, and managing plugins.

func NewManager

func NewManager(deps *PluginDependencies, pluginsDir string) *Manager

NewManager creates and returns a new Manager instance.

func (*Manager) GetCommands

func (m *Manager) GetCommands() []*cobra.Command

GetCommands aggregates and returns all CLI commands provided by loaded plugins.

func (*Manager) GetPlugin

func (m *Manager) GetPlugin(name string) (Plugin, bool)

GetPlugin returns the plugin instance by name.

func (*Manager) ListPlugins

func (m *Manager) ListPlugins() map[string]string

ListPlugins returns a map of plugin names and their versions.

func (*Manager) LoadPlugin

func (m *Manager) LoadPlugin(path string) error

LoadPlugin loads and initializes a plugin from the specified path. It ensures the plugin is not loaded more than once.

func (*Manager) LoadPluginsFromDir

func (m *Manager) LoadPluginsFromDir(dir string) error

LoadPluginsFromDir loads all plugins (files ending in .so) from the specified directory. If no directory is provided, the default plugins directory is used.

func (*Manager) RunPlugin

func (m *Manager) RunPlugin(name string, opts map[string]interface{}) error

RunPlugin executes the specified plugin with the given options.

type Plugin

type Plugin interface {
	// Name returns the plugin's name.
	Name() string
	// Description returns a description of what the plugin does.
	Description() string
	// Version returns the plugin version.
	Version() string
	// Commands returns any additional CLI commands provided by the plugin.
	Commands() []*cobra.Command
	// Init initializes the plugin with dependencies.
	Init(deps *PluginDependencies) error
	// Run executes the plugin with the given options.
	Run(opts map[string]interface{}) error
}

Plugin is the interface that all Nexlayer plugins must implement. This interface provides methods for name, description, version, additional commands, initialization with dependencies, and execution.

type PluginDependencies

type PluginDependencies struct {
	APIClient api.APIClient
	Logger    *observability.Logger
	UIManager *ui.Manager
}

PluginDependencies contains the dependencies available to plugins.

type SmartDeploymentsPlugin

type SmartDeploymentsPlugin struct {
	// contains filtered or unexported fields
}

SmartDeploymentsPlugin implements the Plugin interface to provide AI-powered recommendations that align with Nexlayer Cloud's templating YAML system and deployment behavior.

var SmartDeploymentsPluginInstance SmartDeploymentsPlugin

Export the plugin instance.

func (*SmartDeploymentsPlugin) Commands

func (p *SmartDeploymentsPlugin) Commands() []*cobra.Command

Commands returns the list of CLI commands provided by the plugin.

func (*SmartDeploymentsPlugin) Description

func (p *SmartDeploymentsPlugin) Description() string

Description returns a short description of what the plugin does.

func (*SmartDeploymentsPlugin) Init

Init initializes the plugin with its dependencies.

func (*SmartDeploymentsPlugin) Name

func (p *SmartDeploymentsPlugin) Name() string

Name returns the plugin's name.

func (*SmartDeploymentsPlugin) Run

func (p *SmartDeploymentsPlugin) Run(opts map[string]interface{}) error

Run executes the plugin in non-interactive mode (if needed).

func (*SmartDeploymentsPlugin) Version

func (p *SmartDeploymentsPlugin) Version() string

Version returns the plugin version.

type TemplateSpec

type TemplateSpec struct {
	Version       string                 `yaml:"version"`
	Schema        map[string]interface{} `yaml:"schema"`
	Components    map[string]interface{} `yaml:"componentTypes"`
	Examples      map[string]interface{} `yaml:"examples"`
	BestPractices map[string]interface{} `yaml:"bestPractices"`
	Validation    map[string]interface{} `yaml:"validation"`
}

TemplateSpec represents the Nexlayer template specification

func LoadTemplateSpec

func LoadTemplateSpec() (*TemplateSpec, error)

LoadTemplateSpec loads the template specification from the YAML file

func (*TemplateSpec) GetComponentDefaults

func (s *TemplateSpec) GetComponentDefaults(componentType, subType string) (map[string]interface{}, error)

GetComponentDefaults returns the default configuration for a given component type

func (*TemplateSpec) ValidateTemplate

func (s *TemplateSpec) ValidateTemplate(template map[string]interface{}) []string

ValidateTemplate validates a template against the specification

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL