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 1151e7c3ad check empty execute 2 years ago
example add example hidden option 2 years ago
input add hidden option 2 years ago
output add hidden option 2 years ago
.drone.yml add config example (#3) 2 years ago
.gitignore first commit 4 years ago
.golangci.yml add config example (#3) 2 years ago
LICENSE first commit 4 years ago
README.md update readme 4 years ago
app.go add hidden option 2 years ago
app_test.go add value vithh error (#1) 2 years ago
command.go check empty execute 2 years ago
command_test.go check empty execute 2 years ago
console.go add hidden option 2 years ago
console_test.go update input/outpu 4 years ago
doc.go add value vithh error (#1) 2 years ago
error.go check empty execute 2 years ago
go.mod add value vithh error (#1) 2 years ago
go.sum add value vithh error (#1) 2 years ago
help.go add hidden option 2 years ago
list.go add hidden option 2 years ago
register.go check empty execute 2 years ago
register_test.go add value vithh error (#1) 2 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())
	}
}