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 50183d8ae2 remove unused input type 4 years ago
example move flag to pkg value 4 years ago
input remove unused input type 4 years ago
output move flag to pkg value 4 years ago
.drone.yml first commit 4 years ago
.gitignore first commit 4 years ago
.golangci.yml first commit 4 years ago
LICENSE first commit 4 years ago
README.md update readme 4 years ago
app.go update lint 4 years ago
app_test.go first commit 4 years ago
command.go first commit 4 years ago
command_test.go update input/outpu 4 years ago
console.go update input/outpu 4 years ago
console_test.go update input/outpu 4 years ago
doc.go first commit 4 years ago
go.mod first commit 4 years ago
go.sum first commit 4 years ago
help.go move flag to pkg value 4 years ago
list.go move descriptor 4 years ago
register.go first commit 4 years ago
register_test.go first commit 4 years 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())
	}
}