Merge pull request 'update example' (#14) from command into master
All checks were successful
Go Action / goaction (push) Successful in 36s
All checks were successful
Go Action / goaction (push) Successful in 36s
Reviewed-on: #14
This commit was merged in pull request #14.
This commit is contained in:
@@ -2,6 +2,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"os"
|
||||||
|
|
||||||
"gitoa.ru/go-4devs/config/provider/arg"
|
"gitoa.ru/go-4devs/config/provider/arg"
|
||||||
"gitoa.ru/go-4devs/config/provider/chain"
|
"gitoa.ru/go-4devs/config/provider/chain"
|
||||||
@@ -22,7 +23,7 @@ func main() {
|
|||||||
console.
|
console.
|
||||||
New(console.WithInput(
|
New(console.WithInput(
|
||||||
chain.New(
|
chain.New(
|
||||||
arg.New(arg.WithSkip(0)),
|
arg.New(arg.WithArgs(os.Args)),
|
||||||
env.New(Namespace, AppName),
|
env.New(Namespace, AppName),
|
||||||
&memory.Default{},
|
&memory.Default{},
|
||||||
),
|
),
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
module gitoa.ru/go-4devs/console/example
|
module gitoa.ru/go-4devs/console/example
|
||||||
|
|
||||||
go 1.23
|
go 1.24.0
|
||||||
|
|
||||||
toolchain go1.24.1
|
toolchain go1.24.1
|
||||||
|
|
||||||
require (
|
require (
|
||||||
gitoa.ru/go-4devs/config v0.0.7
|
gitoa.ru/go-4devs/config v0.0.8
|
||||||
gitoa.ru/go-4devs/console v0.2.0
|
gitoa.ru/go-4devs/console v0.2.1-0.20260105202444-e2c6fc0a35a4
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
gitoa.ru/go-4devs/config v0.0.7 h1:8q6axRNLgXE5dYQd8Jbh9j+STqevbibVyvwrtsuHpZk=
|
gitoa.ru/go-4devs/config v0.0.8 h1:o4p8I9jWJMfiFVVKr50IqCGj1fF+8kmSPDkH0deRvn4=
|
||||||
gitoa.ru/go-4devs/config v0.0.7/go.mod h1:UINWnObZA0nLiJro+TtavUBBvN0cSt17aRHOk20pP74=
|
gitoa.ru/go-4devs/config v0.0.8/go.mod h1:jHKqVafFVW400LC0M4i1ifPapiI9sqpX/QTh+VMadKw=
|
||||||
gitoa.ru/go-4devs/console v0.2.0 h1:6lsbArs99GA8vGdnwNDThZNKjFNctNtTlSCUjhgwIpU=
|
gitoa.ru/go-4devs/console v0.2.0 h1:6lsbArs99GA8vGdnwNDThZNKjFNctNtTlSCUjhgwIpU=
|
||||||
gitoa.ru/go-4devs/console v0.2.0/go.mod h1:xi4Svw7T+lylckAQiJQS/2qwDwF4YbIanlhcbQrBAiI=
|
gitoa.ru/go-4devs/console v0.2.0/go.mod h1:xi4Svw7T+lylckAQiJQS/2qwDwF4YbIanlhcbQrBAiI=
|
||||||
|
gitoa.ru/go-4devs/console v0.2.1-0.20260105202444-e2c6fc0a35a4 h1:zOk/59IvgUfCDL6ub6WX4hsqDK79FsZR0gf7s7t3fXM=
|
||||||
|
gitoa.ru/go-4devs/console v0.2.1-0.20260105202444-e2c6fc0a35a4/go.mod h1:PG/Zyj1dLh7eFlj9bgnV58+Ys6I/MTrS0q9W7oD7z4U=
|
||||||
|
|||||||
@@ -7,14 +7,20 @@ import (
|
|||||||
"gitoa.ru/go-4devs/config"
|
"gitoa.ru/go-4devs/config"
|
||||||
"gitoa.ru/go-4devs/config/definition/option"
|
"gitoa.ru/go-4devs/config/definition/option"
|
||||||
"gitoa.ru/go-4devs/console"
|
"gitoa.ru/go-4devs/console"
|
||||||
|
"gitoa.ru/go-4devs/console/command"
|
||||||
"gitoa.ru/go-4devs/console/output"
|
"gitoa.ru/go-4devs/console/output"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Args() *console.Command {
|
func Args() command.Command {
|
||||||
return &console.Command{
|
return command.New(
|
||||||
Name: "fdevs:console:arg",
|
"fdevs:console:arg",
|
||||||
Description: "Understanding how Console Arguments and Options Are Handled",
|
"Understanding how Console Arguments and Options Are Handled",
|
||||||
Configure: func(_ context.Context, def config.Definition) error {
|
ArgExecute,
|
||||||
|
command.Configure(ArgConfigure),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
func ArgConfigure(_ context.Context, def config.Definition) error {
|
||||||
def.Add(
|
def.Add(
|
||||||
option.Bool("foo", "foo option", option.Short('f')),
|
option.Bool("foo", "foo option", option.Short('f')),
|
||||||
option.String("bar", "required bar option", option.Required, option.Short('b')),
|
option.String("bar", "required bar option", option.Required, option.Short('b')),
|
||||||
@@ -24,8 +30,9 @@ func Args() *console.Command {
|
|||||||
)
|
)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
},
|
}
|
||||||
Execute: func(ctx context.Context, in config.Provider, out output.Output) error {
|
|
||||||
|
func ArgExecute(ctx context.Context, in config.Provider, out output.Output) error {
|
||||||
out.Println(ctx, "foo: <info>", console.ReadValue(ctx, in, "foo").Bool(), "</info>")
|
out.Println(ctx, "foo: <info>", console.ReadValue(ctx, in, "foo").Bool(), "</info>")
|
||||||
out.Println(ctx, "bar: <info>", console.ReadValue(ctx, in, "bar").String(), "</info>")
|
out.Println(ctx, "bar: <info>", console.ReadValue(ctx, in, "bar").String(), "</info>")
|
||||||
out.Println(ctx, "cat: <info>", console.ReadValue(ctx, in, "cat").String(), "</info>")
|
out.Println(ctx, "cat: <info>", console.ReadValue(ctx, in, "cat").String(), "</info>")
|
||||||
@@ -33,6 +40,4 @@ func Args() *console.Command {
|
|||||||
out.Println(ctx, "hidden: <info>", console.ReadValue(ctx, in, "hidden").Time().Format(time.RFC3339), "</info>")
|
out.Println(ctx, "hidden: <info>", console.ReadValue(ctx, in, "hidden").Time().Format(time.RFC3339), "</info>")
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,19 +5,29 @@ import (
|
|||||||
|
|
||||||
"gitoa.ru/go-4devs/config"
|
"gitoa.ru/go-4devs/config"
|
||||||
"gitoa.ru/go-4devs/config/definition/option"
|
"gitoa.ru/go-4devs/config/definition/option"
|
||||||
"gitoa.ru/go-4devs/config/param"
|
cparam "gitoa.ru/go-4devs/config/param"
|
||||||
argument "gitoa.ru/go-4devs/config/provider/arg"
|
argument "gitoa.ru/go-4devs/config/provider/arg"
|
||||||
"gitoa.ru/go-4devs/console"
|
"gitoa.ru/go-4devs/console"
|
||||||
|
"gitoa.ru/go-4devs/console/command"
|
||||||
"gitoa.ru/go-4devs/console/output"
|
"gitoa.ru/go-4devs/console/output"
|
||||||
|
"gitoa.ru/go-4devs/console/param"
|
||||||
)
|
)
|
||||||
|
|
||||||
func CreateUser(required bool) *console.Command {
|
func CreateUser(required bool) command.Command {
|
||||||
return &console.Command{
|
return command.New(
|
||||||
Name: "app:create-user",
|
"app:create-user",
|
||||||
Description: "Creates a new user.",
|
"Creates a new user.",
|
||||||
Help: "This command allows you to create a user...",
|
UserExecute,
|
||||||
Configure: func(_ context.Context, cfg config.Definition) error {
|
command.Configure(UserConfigure(required)),
|
||||||
var opts []param.Option
|
command.Help(func(param.HData) (string, error) {
|
||||||
|
return "This command allows you to create a user...", nil
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
func UserConfigure(required bool) func(_ context.Context, cfg config.Definition) error {
|
||||||
|
return func(_ context.Context, cfg config.Definition) error {
|
||||||
|
var opts []cparam.Option
|
||||||
if required {
|
if required {
|
||||||
opts = append(opts, option.Required)
|
opts = append(opts, option.Required)
|
||||||
}
|
}
|
||||||
@@ -29,13 +39,13 @@ func CreateUser(required bool) *console.Command {
|
|||||||
)
|
)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
},
|
}
|
||||||
Execute: func(ctx context.Context, in config.Provider, out output.Output) error {
|
}
|
||||||
|
|
||||||
|
func UserExecute(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, "Username: ", console.ReadValue(ctx, in, "username").String())
|
out.Println(ctx, "Username: ", console.ReadValue(ctx, in, "username").String())
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,14 +7,20 @@ import (
|
|||||||
"gitoa.ru/go-4devs/config/definition/option"
|
"gitoa.ru/go-4devs/config/definition/option"
|
||||||
"gitoa.ru/go-4devs/config/provider/arg"
|
"gitoa.ru/go-4devs/config/provider/arg"
|
||||||
"gitoa.ru/go-4devs/console"
|
"gitoa.ru/go-4devs/console"
|
||||||
|
"gitoa.ru/go-4devs/console/command"
|
||||||
"gitoa.ru/go-4devs/console/output"
|
"gitoa.ru/go-4devs/console/output"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Hello() *console.Command {
|
func Hello() command.Command {
|
||||||
return &console.Command{
|
return command.New(
|
||||||
Name: "fdevs:console:hello",
|
"fdevs:console:hello",
|
||||||
Description: "example hello command",
|
"example hello command",
|
||||||
Execute: func(ctx context.Context, in config.Provider, out output.Output) error {
|
HelloExecute,
|
||||||
|
command.Configure(HelloConfigure),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
func HelloExecute(ctx context.Context, in config.Provider, out output.Output) error {
|
||||||
name := console.ReadValue(ctx, in, "name").String()
|
name := console.ReadValue(ctx, in, "name").String()
|
||||||
out.Println(ctx, "<error>Hello</error> <info>", name, "</info>")
|
out.Println(ctx, "<error>Hello</error> <info>", name, "</info>")
|
||||||
|
|
||||||
@@ -26,14 +32,13 @@ func Hello() *console.Command {
|
|||||||
out.Println(ctx, "hidden option pass <info>", pass, "</info>")
|
out.Println(ctx, "hidden option pass <info>", pass, "</info>")
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
},
|
}
|
||||||
Configure: func(_ context.Context, def config.Definition) error {
|
|
||||||
|
func HelloConfigure(_ context.Context, def config.Definition) error {
|
||||||
def.Add(
|
def.Add(
|
||||||
arg.String("name", "Same name", arg.Default("World")),
|
arg.String("name", "Same name", arg.Default("World")),
|
||||||
option.String("pass", "password", option.Hidden),
|
option.String("pass", "password", option.Hidden),
|
||||||
)
|
)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,19 +4,16 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
|
|
||||||
"gitoa.ru/go-4devs/config"
|
"gitoa.ru/go-4devs/config"
|
||||||
"gitoa.ru/go-4devs/console"
|
"gitoa.ru/go-4devs/console/command"
|
||||||
"gitoa.ru/go-4devs/console/output"
|
"gitoa.ru/go-4devs/console/output"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Hidden() *console.Command {
|
func Hidden() command.Command {
|
||||||
return &console.Command{
|
return command.New("fdevs:console:hidden", "hidden command exmale", HiddenExecute, command.Hidden)
|
||||||
Name: "fdevs:console:hidden",
|
}
|
||||||
Description: "hidden command exmale",
|
|
||||||
Hidden: true,
|
func HiddenExecute(ctx context.Context, _ config.Provider, out output.Output) error {
|
||||||
Execute: func(ctx context.Context, _ config.Provider, out output.Output) error {
|
|
||||||
out.Println(ctx, "<info> call hidden command</info>")
|
out.Println(ctx, "<info> call hidden command</info>")
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,16 +9,18 @@ import (
|
|||||||
"gitoa.ru/go-4devs/config/validator"
|
"gitoa.ru/go-4devs/config/validator"
|
||||||
"gitoa.ru/go-4devs/config/value"
|
"gitoa.ru/go-4devs/config/value"
|
||||||
"gitoa.ru/go-4devs/console"
|
"gitoa.ru/go-4devs/console"
|
||||||
|
"gitoa.ru/go-4devs/console/command"
|
||||||
"gitoa.ru/go-4devs/console/output"
|
"gitoa.ru/go-4devs/console/output"
|
||||||
)
|
)
|
||||||
|
|
||||||
const defaultTimeout = time.Second * 30
|
const defaultTimeout = time.Second * 30
|
||||||
|
|
||||||
// Long example of a command that takes a long time to run.
|
// Long example of a command that takes a long time to run.
|
||||||
func Long() *console.Command {
|
func Long() command.Command {
|
||||||
return &console.Command{
|
return command.New("fdevs:command:long", "long command description", LongExecute, command.Configure(LongConfigure))
|
||||||
Name: "fdevs:command:long",
|
}
|
||||||
Execute: func(ctx context.Context, in config.Provider, out output.Output) error {
|
|
||||||
|
func LongExecute(ctx context.Context, in config.Provider, out output.Output) error {
|
||||||
timeout := console.ReadValue(ctx, in, "timeout").Duration()
|
timeout := console.ReadValue(ctx, in, "timeout").Duration()
|
||||||
timer := time.NewTimer(timeout)
|
timer := time.NewTimer(timeout)
|
||||||
|
|
||||||
@@ -39,8 +41,9 @@ func Long() *console.Command {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
Configure: func(_ context.Context, def config.Definition) error {
|
|
||||||
|
func LongConfigure(_ context.Context, def config.Definition) error {
|
||||||
def.Add(option.Duration("timeout", "set duration run command",
|
def.Add(option.Duration("timeout", "set duration run command",
|
||||||
option.Default(value.New(defaultTimeout)),
|
option.Default(value.New(defaultTimeout)),
|
||||||
option.Short('t'),
|
option.Short('t'),
|
||||||
@@ -48,6 +51,4 @@ func Long() *console.Command {
|
|||||||
))
|
))
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,18 +4,16 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
|
|
||||||
"gitoa.ru/go-4devs/config"
|
"gitoa.ru/go-4devs/config"
|
||||||
"gitoa.ru/go-4devs/console"
|
"gitoa.ru/go-4devs/console/command"
|
||||||
"gitoa.ru/go-4devs/console/output"
|
"gitoa.ru/go-4devs/console/output"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Namespace() *console.Command {
|
func Namespace() command.Command {
|
||||||
return &console.Command{
|
return command.New("app:start", "example command in other namespace", NSExecute)
|
||||||
Name: "app:start",
|
}
|
||||||
Description: "example command in other namespace",
|
|
||||||
Execute: func(ctx context.Context, _ config.Provider, out output.Output) error {
|
func NSExecute(ctx context.Context, _ config.Provider, out output.Output) error {
|
||||||
out.Println(ctx, "example command in other namespace")
|
out.Println(ctx, "example command in other namespace")
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user