CLI framework for Go.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
andrey1s 596026ae92 add map input 3 weeks ago
example move flag to pkg value 3 weeks ago
input add map input 3 weeks ago
output move kv to label 3 weeks ago
.drone.yml first commit 1 month ago
.gitignore first commit 1 month ago
.golangci.yml first commit 1 month ago
LICENSE first commit 1 month ago
README.md update readme 1 month ago
app.go add chain input 3 weeks ago
app_test.go first commit 1 month ago
command.go first commit 1 month ago
command_test.go update input/outpu 1 month ago
console.go add map input 3 weeks ago
console_test.go update input/outpu 1 month ago
doc.go first commit 1 month ago
go.mod first commit 1 month ago
go.sum first commit 1 month ago
help.go move flag to pkg value 3 weeks ago
list.go move descriptor 1 month ago
register.go first commit 1 month ago
register_test.go first commit 1 month ago

README.md

Console

Build Status Go Report Card GoDoc

Creating a Command

Commands are defined in struct extending pkg/command/create_user.go. For example, you may want a command to create a user:

package command

import (
	"context"

	"gitoa.ru/go-4devs/console"
	"gitoa.ru/go-4devs/console/input"
	"gitoa.ru/go-4devs/console/output"
)

func CreateUser() *console.Command {
	return &console.Command{
		Name: "app:create-user",
		Execute: func(ctx context.Context, in input.Input, out output.Output) error {
			return nil
		},
	}
}

Configure command

func CreateUser() *console.Command {
	return &console.Command{
        //...
		Description: "Creates a new user.",
		Help:        "This command allows you to create a user...",
	}
}

Add arguments

func CreateUser(required bool) *console.Command {
	return &console.Command{
        //....
		Configure: func(ctx context.Context, cfg *input.Definition) error {
			var opts []func(*input.Argument)
			if required {
				opts = append(opts, argument.Required)
			}
			cfg.SetArgument("password", "User password", opts...)

			return nil
		},
	}
}

Registering the Command

cmd/console/main.go

package main

import (
	"context"

	"gitoa.ru/go-4devs/console"
	"gitoa.ru/go-4devs/console/example/pkg/command"
)

func main() {
	console.
		New().
		Add(
			command.CreateUser(false),
		).
		Execute(context.Background())
}

Executing the Command

build command go build -o bin/console cmd/console/main.go run command `bin/console app:create-user``

Console Output

The Execute field has access to the output stream to write messages to the console:

func CreateUser(required bool) *console.Command {
	return &console.Command{
        // ....
		Execute: func(ctx context.Context, in input.Input, out output.Output) error {
			// outputs a message followed by a "\n"
			out.Println(ctx, "User Creator")
			out.Println(ctx, "Whoa!")

			// outputs a message without adding a "\n" at the end of the line
			out.Print(ctx, "You are about to ", "create a user.")

			return nil
		},
	}
}

Now, try build and executing the command:

bin/console app:create-user
User Creator
Whoa!
You are about to create a user.

Console Input

Use input options or arguments to pass information to the command:

func CreateUser(required bool) *console.Command {
	return &console.Command{
		Configure: func(ctx context.Context, cfg *input.Definition) error {
			var opts []func(*input.Argument)
			if required {
				opts = append(opts, argument.Required)
			}
			cfg.
				SetArgument("username", "The username of the user.", argument.Required).
				SetArgument("password", "User password", opts...)

			return nil
		},
		Execute: func(ctx context.Context, in input.Input, out output.Output) error {
			// outputs a message followed by a "\n"
			out.Println(ctx, "User Creator")
			out.Println(ctx, "Username: ", in.Argument(ctx, "username").String())

			return nil
		},
	}
}

Now, you can pass the username to the command:

bin/console app:create-user AwesomeUsername
User Creator
Username: AwesomeUsername

Testing Commands

package command_test

import (
	"bytes"
	"context"
	"testing"

	"gitoa.ru/go-4devs/console"
	"gitoa.ru/go-4devs/console/example/pkg/command"
	"gitoa.ru/go-4devs/console/input/array"
	"gitoa.ru/go-4devs/console/output"
)

func TestCreateUser(t *testing.T) {
	ctx := context.Background()
	in := input.Array{}
    in.SetArgument("username","andrey")
	buf := bytes.Buffer{}
	out := output.Buffer(&buf)

	console.Run(ctx, command.CreateUser(false), in, out)

	expect := `User Creator
Username: andrey
`

	if expect != buf.String() {
		t.Errorf("expect: %s, got:%s", expect, buf.String())
	}
}