cmd

package module
v2.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2023 License: Apache-2.0 Imports: 23 Imported by: 2

README

KrakenD Cobra

An adapter of the cobra lib for the KrakenD framework

Package cmd defines the cobra command structs and an execution method for adding an improved CLI to KrakenD based api gateways

Basic example

package main

import (
	"os"

	"github.com/krakendio/krakend-cobra/v2"
	"github.com/krakendio/krakend-viper/v2"
	"github.com/luraproject/lura/v2/config"
	"github.com/luraproject/lura/v2/logging"
	"github.com/luraproject/lura/v2/proxy"
	krakendgin "github.com/luraproject/lura/v2/router/gin"
)

func main() {

	cmd.Execute(viper.New(), func(serviceConfig config.ServiceConfig) {
		logger, _ := logging.NewLogger("DEBUG", os.Stdout, "")
		krakendgin.DefaultFactory(proxy.DefaultFactory(logger), logger).New().Run(serviceConfig)
	})

}

Available commands

The cmd package includes four commands: check, check-plugin, help and run.

  1. check validates the received config file.
  2. check-plugin validates the dependencies shared between the binary and a plugin.
  3. help displays details about any command.
  4. run executes the passed executor once the received flags overwrite the parsed config.
$ ./krakend
 ╓▄█                          ▄▄▌                               ╓██████▄µ
▐███  ▄███╨▐███▄██H╗██████▄  ║██▌ ,▄███╨ ▄██████▄  ▓██▌█████▄  ███▀╙╙▀▀███╕
▐███▄███▀  ▐█████▀"╙▀▀"╙▀███ ║███▄███┘  ███▀""▀███ ████▀╙▀███H ███     ╙███
▐██████▌   ▐███⌐  ,▄████████M║██████▄  ║██████████M███▌   ███H ███     ,███
▐███╨▀███µ ▐███   ███▌  ,███M║███╙▀███  ███▄```▄▄` ███▌   ███H ███,,,╓▄███▀
▐███  ╙███▄▐███   ╙█████████M║██▌  ╙███▄`▀███████╨ ███▌   ███H █████████▀
                     ``                     `'`
Version: undefined

The API Gateway builder

Usage:
  krakend [command]

Available Commands:
  check        Validates that the configuration file is valid.
  check-plugin Checks your plugin dependencies are compatible.
  help         Help about any command
  run          Runs the KrakenD server.

Flags:
  -h, --help   help for krakend

Use "krakend [command] --help" for more information about a command.

Documentation

Overview

Package cmd defines the cobra command structs and an execution method for adding an improved CLI to KrakenD based api gateways

Index

Constants

This section is empty.

Variables

View Source
var (
	DefaultRoot    Root
	RootCommand    Command
	RunCommand     Command
	CheckCommand   Command
	PluginCommand  Command
	VersionCommand Command
	AuditCommand   Command
)
View Source
var IsTTY = isatty.IsTerminal(os.Stderr.Fd())
View Source
var RunRouterFunc = func(cfg config.ServiceConfig) (err error) {
	defer func() {
		if r := recover(); r != nil {
			err = errors.New(r.(string))
		}
	}()

	gin.SetMode(gin.ReleaseMode)
	cfg.Debug = cfg.Debug || debug > 0
	if port != 0 {
		cfg.Port = port
	}

	ctx, cancel := context.WithTimeout(context.Background(), time.Second)
	krakendgin.DefaultFactory(proxy.DefaultFactory(logging.NoOp), logging.NoOp).NewWithContext(ctx).Run(cfg)
	cancel()
	return nil
}
View Source
var SchemaURL = "https://www.krakend.io/schema/v%s/krakend.json"

Functions

func Execute

func Execute(configParser config.Parser, f Executor)

Execute sets up the cmd package with the received configuration parser and executor and delegates the CLI execution to the cobra lib

func ExecuteRoot

func ExecuteRoot(configParser config.Parser, f Executor, root Root)

func GetConfigFlag

func GetConfigFlag() string

func GetConfigParser

func GetConfigParser() config.Parser

func GetDebugFlag

func GetDebugFlag() bool

Types

type Command

type Command struct {
	Cmd   *cobra.Command
	Flags []FlagBuilder
	// contains filtered or unexported fields
}

func NewCommand

func NewCommand(command *cobra.Command, flags ...FlagBuilder) Command

func (*Command) AddFlag

func (c *Command) AddFlag(f FlagBuilder)

func (*Command) AddSubCommand

func (c *Command) AddSubCommand(cmd *cobra.Command)

func (*Command) BuildFlags

func (c *Command) BuildFlags()

type Executor

type Executor func(config.ServiceConfig)

Executor defines the function that requires a service description

type FlagBuilder

type FlagBuilder func(*cobra.Command)

func BoolFlagBuilder

func BoolFlagBuilder(dst *bool, long, short string, defaultValue bool, help string) FlagBuilder

func CountFlagBuilder

func CountFlagBuilder(dst *int, long, short, help string) FlagBuilder

func DurationFlagBuilder

func DurationFlagBuilder(dst *time.Duration, long, short string, defaultValue time.Duration, help string) FlagBuilder

func Float64FlagBuilder

func Float64FlagBuilder(dst *float64, long, short string, defaultValue float64, help string) FlagBuilder

func IntFlagBuilder

func IntFlagBuilder(dst *int, long, short string, defaultValue int, help string) FlagBuilder

func StringFlagBuilder

func StringFlagBuilder(dst *string, long, short, defaultValue, help string) FlagBuilder

type LastSourcer added in v2.3.0

type LastSourcer interface {
	LastSource() ([]byte, error)
}

type Root

type Root struct {
	Command
	SubCommands []Command
	// contains filtered or unexported fields
}

func NewRoot

func NewRoot(root Command, subCommands ...Command) Root

func (*Root) Build

func (r *Root) Build()

func (Root) Execute

func (r Root) Execute(configParser config.Parser, f Executor)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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