package node
import "go.dedis.ch/dela/cli/node"
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 ¶
- type ActionTemplate
- type Builder
- type CLIBuilder
- func NewBuilder(inits ...Initializer) *CLIBuilder
- func NewBuilderWithCfg(sigs chan os.Signal, out io.Writer, inits ...Initializer) *CLIBuilder
- func (b *CLIBuilder) Build() cli.Application
- func (b *CLIBuilder) MakeAction(tmpl ActionTemplate) cli.Action
- func (b *CLIBuilder) SetStartFlags(flags ...cli.Flag)
- type Client
- type Context
- type Daemon
- type DaemonFactory
- type FlagSet
- func (fset FlagSet) Bool(name string) bool
- func (fset FlagSet) Duration(name string) time.Duration
- func (fset FlagSet) Int(name string) int
- func (fset FlagSet) Path(name string) string
- func (fset FlagSet) String(name string) string
- func (fset FlagSet) StringSlice(name string) []string
- type Initializer
- type Injector
Examples ¶
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 ¶
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.
Code:play
Output: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
}
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 ¶
Client is the interface to send a message to the daemon.
type Context ¶
Context is the context available to the action when being invoked. It provides the dependency injector alongside with the input and output.
type Daemon ¶
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 ¶
Bool implements cli.Flags. It return the boolean associated with the flag if it is set, otherwise it returns false.
func (FlagSet) 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 ¶
Int implements cli.Flags. It returns the integer associated with the flag if it is set, otherwise it returns zero.
func (FlagSet) Path ¶
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 (FlagSet) StringSlice ¶
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.
Source Files ¶
builder.go daemon.go flagset.go injector.go node.go
Directories ¶
Path | Synopsis |
---|---|
cli/node/memcoin | Package main implements a ledger based on in-memory components. |
- Version
- v0.1.0 (latest)
- Published
- Apr 10, 2024
- Platform
- linux/amd64
- Imports
- 18 packages
- Last checked
- 1 month ago –
Tools for package owners.