update readme
All checks were successful
Go Action / goaction (pull_request) Successful in 38s

This commit is contained in:
2026-01-06 17:28:59 +03:00
parent 85edd5acec
commit a4b92d112c

View File

@@ -15,15 +15,16 @@ package command
import ( import (
"context" "context"
"gitoa.ru/go-4devs/console"
"gitoa.ru/go-4devs/console/output" "gitoa.ru/go-4devs/console/output"
"gitoa.ru/go-4devs/console/command"
"gitoa.ru/go-4devs/config" "gitoa.ru/go-4devs/config"
) )
func CreateUser() *console.Command { func CreateUser() command.Command {
return &console.Command{ return command.New(
Name: "app:create-user", "app:create-user",
Execute: func(ctx context.Context, in config.Provider, out output.Output) error { "create user",
func(ctx context.Context, in config.Provider, out output.Output) error {
return nil return nil
}, },
} }
@@ -32,23 +33,36 @@ func CreateUser() *console.Command {
## Configure command ## Configure command
```go ```go
func CreateUser() *console.Command { func CreateUser() command.Command {
return &console.Command{ return command.New(
//... "app:create-user",
Description: "Creates a new user.", "Creates a new user.",
Help: "This command allows you to create a user...", Execute,
command.Help( "This command allows you to create a user..."),
} }
} }
func Execute(ctx context.Context, in config.Provider, out output.Output) error{
return nil
}
``` ```
## Add arguments ## Add arguments
```go ```go
func CreateUser(required bool) *console.Command { func CreateUser(required bool) command.Command {
return &console.Command{ return command.New(
//.... "name",
Configure: func(ctx context.Context, cfg config.Definition) error { "description",
Execute,
command.Configure(Configure(required)),
)
}
}
func Configure(required bool) func(ctx context.Context, cfg config.Definition) error {
return func (ctx context.Context, cfg config.Definition) error{
var opts []func(*arg.Option) var opts []func(*arg.Option)
if required { if required {
opts = append(opts, arg.Required) opts = append(opts, arg.Required)
@@ -58,7 +72,6 @@ func CreateUser(required bool) *console.Command {
) )
return nil return nil
},
} }
} }
``` ```
@@ -96,10 +109,15 @@ run command `bin/console app:create-user``
The Execute field has access to the output stream to write messages to the console: The Execute field has access to the output stream to write messages to the console:
```go ```go
func CreateUser(required bool) *console.Command { func CreateUser(required bool) command.Command {
return &console.Command{ return command.New(
// .... "app:user:create",
Execute: func(ctx context.Context, in config.Provider, out output.Output) error { "create user",
Execute,
)
}
func Execute(ctx context.Context, in config.Provider, out output.Output) error {
// outputs a message followed by a "\n" // outputs a message followed by a "\n"
out.Println(ctx, "User Creator") out.Println(ctx, "User Creator")
out.Println(ctx, "Whoa!") out.Println(ctx, "Whoa!")
@@ -108,8 +126,6 @@ func CreateUser(required bool) *console.Command {
out.Print(ctx, "You are about to ", "create a user.") out.Print(ctx, "You are about to ", "create a user.")
return nil return nil
},
}
} }
``` ```
@@ -127,29 +143,31 @@ You are about to create a user.
Use input options or arguments to pass information to the command: Use input options or arguments to pass information to the command:
```go ```go
func CreateUser(required bool) *console.Command { func CreateUser() command.Command {
return &console.Command{ return command.New(
Configure: func(ctx context.Context, cfg config.Definition) error { "app:user:create",
var opts []func(*input.Argument) "create user",
if required { Execute,
opts = append(opts, argument.Required) command.Configure(Configure),
} )
}
func Configure(ctx context.Context, cfg config.Definition) error {
cfg.Add( cfg.Add(
arg.String("username", "The username of the user.", arg.Required), arg.String("username", "The username of the user.", arg.Required),
arg.String("password", "User password", opts...), arg.String("password", "User password"),
) )
return nil return nil
}, }
Execute: func(ctx context.Context, in config.Provider, out output.Output) error {
func Execute(ctx context.Context, in config.Provider, out output.Output) error {
// outputs a message followed by a "\n" // outputs a message followed by a "\n"
username, _ := in.Value(ctx, "username") username, _ := in.Value(ctx, "username")
out.Println(ctx, "User Creator") out.Println(ctx, "User Creator")
out.Println(ctx, "Username: ", username.String()) out.Println(ctx, "Username: ", username.String())
return nil return nil
},
}
} }
``` ```
@@ -180,7 +198,7 @@ import (
func TestCreateUser(t *testing.T) { func TestCreateUser(t *testing.T) {
ctx := context.Background() ctx := context.Background()
in := memory.Map{} in := memory.Map{}
in.Set("andrey","username") in.SetOption("andrey","username")
buf := bytes.Buffer{} buf := bytes.Buffer{}
out := output.Buffer(&buf) out := output.Buffer(&buf)