Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
andrey ad5cf18535 Merge pull request 'check empty execute' (#6) from execute into master il y a 2 ans
example add example hidden option il y a 2 ans
input add hidden option il y a 2 ans
output add hidden option il y a 2 ans
.drone.yml add config example (#3) il y a 2 ans
.gitignore first commit il y a 3 ans
.golangci.yml add config example (#3) il y a 2 ans
LICENSE first commit il y a 3 ans
README.md update readme il y a 3 ans
app.go add hidden option il y a 2 ans
app_test.go add value vithh error (#1) il y a 2 ans
command.go check empty execute il y a 2 ans
command_test.go check empty execute il y a 2 ans
console.go add hidden option il y a 2 ans
console_test.go update input/outpu il y a 3 ans
doc.go add value vithh error (#1) il y a 2 ans
error.go check empty execute il y a 2 ans
go.mod add value vithh error (#1) il y a 2 ans
go.sum add value vithh error (#1) il y a 2 ans
help.go add hidden option il y a 2 ans
list.go add hidden option il y a 2 ans
register.go check empty execute il y a 2 ans
register_test.go add value vithh error (#1) il y a 2 ans

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())
	}
}