node

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2024 License: BSD-3-Clause Imports: 18 Imported by: 10

Documentation

Overview

Package node defines the Builder type, which builds an CLI application to controle a node.

The application will have a start command by default and it provides a function to create actions that will eventually be executed on the running node. See the example.

Document Last Review: 13.10.2020

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ActionTemplate

type ActionTemplate interface {
	// Execute processes a command received from the CLI on the daemon.
	Execute(Context) error
}

ActionTemplate is an extension of the cli.Action interface to allow an action to send a request to the daemon.

type Builder

type Builder interface {
	// SetCommand creates a new command and returns its builder.
	SetCommand(name string) cli.CommandBuilder

	// SetStartFlags appends a list of flags that will be used to create the
	// start command.
	SetStartFlags(...cli.Flag)

	// MakeAction creates a CLI action from a given template. The template must
	// implements the handler that will be executed on the daemon.
	MakeAction(ActionTemplate) cli.Action
}

Builder is the builder that will be provided to the initializers, which can create commands and actions.

type CLIBuilder

type CLIBuilder struct {
	cli.Builder
	// contains filtered or unexported fields
}

CLIBuilder is an application builder that will build a CLI to start and control a node.

- implements node.Builder - implements cli.Builder

func NewBuilder

func NewBuilder(inits ...Initializer) *CLIBuilder

NewBuilder returns a new empty builder.

func NewBuilderWithCfg

func NewBuilderWithCfg(sigs chan os.Signal, out io.Writer, inits ...Initializer) *CLIBuilder

NewBuilderWithCfg returns a new empty builder with specific configurations.

func (*CLIBuilder) Build

func (b *CLIBuilder) Build() cli.Application

Build implements node.Builder. It returns the application.

Example
package main

import (
	"fmt"
	"os"

	"go.dedis.ch/dela/cli"
)

func main() {
	builder := NewBuilder(exampleController{})

	cmd := builder.SetCommand("bye")

	cmd.SetFlags(cli.StringFlag{
		Name:  "name",
		Usage: "set the name",
		Value: "Bob",
	})

	// This action is only executed on the CLI process. It is also possible to
	// call commands on the daemon after it has been started with "start".
	cmd.SetAction(func(flags cli.Flags) error {
		fmt.Printf("Bye, %s!", flags.String("name"))
		return nil
	})

	app := builder.Build()

	err := app.Run([]string{os.Args[0], "bye", "--name", "Alice"})
	if err != nil {
		panic("app failed: " + err.Error())
	}

}

// Hello is an example of a component that can be injected and resolved on the
// daemon side.
type Hello interface {
	SayTo(name string)
}

type simpleHello struct{}

func (simpleHello) SayTo(name string) {
	fmt.Printf("Hello, %s!", name)
}

// helloAction is an example of an action template to be executed on the daemon.
//
// - implements node.ActionTemplate
type helloAction struct{}

// Execute implements node.ActionTemplate. It resolves the hello component and
// say hello to the name defined by the flag.
func (tmpl helloAction) Execute(ctx Context) error {
	var hello Hello
	err := ctx.Injector.Resolve(&hello)
	if err != nil {
		return err
	}

	hello.SayTo(ctx.Flags.String("name"))

	return nil
}

// exampleController is an example of a controller passed to the builder. It
// defines the command available and the component that are injected when the
// daemon is started.
//
// - implements node.Initializer
type exampleController struct{}

// SetCommands implements node.Initializer. It defines the hello command.
func (exampleController) SetCommands(builder Builder) {
	cmd := builder.SetCommand("hello")

	// Set an action that will be executed on the daemon.
	cmd.SetAction(builder.MakeAction(helloAction{}))

	cmd.SetDescription("Say hello")
	cmd.SetFlags(cli.StringFlag{
		Name:  "name",
		Usage: "set the name",
		Value: "Bob",
	})
}

// OnStart implements node.Initializer. It injects the hello component.
func (exampleController) OnStart(flags cli.Flags, inj Injector) error {
	inj.Inject(simpleHello{})

	return nil
}

// OnStop implements node.Initializer.
func (exampleController) OnStop(Injector) error {
	return nil
}
Output:

Bye, Alice!

func (*CLIBuilder) MakeAction

func (b *CLIBuilder) MakeAction(tmpl ActionTemplate) cli.Action

MakeAction implements node.Builder. It creates a CLI action from the template.

func (*CLIBuilder) SetStartFlags

func (b *CLIBuilder) SetStartFlags(flags ...cli.Flag)

SetStartFlags implements node.Builder. It appends the given flags to the list of flags that will be used to create the start command.

type Client

type Client interface {
	Send([]byte) error
}

Client is the interface to send a message to the daemon.

type Context

type Context struct {
	Injector Injector
	Flags    cli.Flags
	Out      io.Writer
}

Context is the context available to the action when being invoked. It provides the dependency injector alongside with the input and output.

type Daemon

type Daemon interface {
	Listen() error
	Close() error
}

Daemon is an IPC socket to communicate between a CLI and a node running.

type DaemonFactory

type DaemonFactory interface {
	ClientFromContext(cli.Flags) (Client, error)
	DaemonFromContext(cli.Flags) (Daemon, error)
}

DaemonFactory is an interface to create a daemon and clients to connect to it.

type FlagSet

type FlagSet map[string]interface{}

FlagSet is a serializable flag set implementation. It allows to pack the flags coming from a CLI application and send them to a daemon.

- implements cli.Flags

func (FlagSet) Bool

func (fset FlagSet) Bool(name string) bool

Bool implements cli.Flags. It return the boolean associated with the flag if it is set, otherwise it returns false.

func (FlagSet) Duration

func (fset FlagSet) Duration(name string) time.Duration

Duration implements cli.Flags. It returns the duration associated with the flag name if it is set, otherwise it returns zero.

func (FlagSet) Int

func (fset FlagSet) Int(name string) int

Int implements cli.Flags. It returns the integer associated with the flag if it is set, otherwise it returns zero.

func (FlagSet) Path

func (fset FlagSet) Path(name string) string

Path implements cli.Flags. It returns the path associated with the flag name if it is set, otherwise it returns an empty string.

func (FlagSet) String

func (fset FlagSet) String(name string) string

func (FlagSet) StringSlice

func (fset FlagSet) StringSlice(name string) []string

StringSlice implements cli.Flags. It returns the slice of strings associated with the flag name if it is set, otherwise it returns nil.

type Initializer

type Initializer interface {
	// SetCommands populates the builder with the commands of the controller.
	SetCommands(Builder)

	// OnStart starts the components of the initializer and populates the
	// injector.
	OnStart(cli.Flags, Injector) error

	// OnStop stops the components and cleans the resources.
	OnStop(Injector) error
}

Initializer is the interface that a module can implement to set its own commands and inject the dependencies that will be resolved in the actions.

type Injector

type Injector interface {
	// Resolve populates the input with the dependency if any compatible exists.
	Resolve(interface{}) error

	// Inject stores the dependency to be resolved later on.
	Inject(interface{})
}

Injector is a dependency injection abstraction.

func NewInjector

func NewInjector() Injector

NewInjector returns a empty injector.

Directories

Path Synopsis
Package main implements a ledger based on in-memory components.
Package main implements a ledger based on in-memory components.

Jump to

Keyboard shortcuts

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