This commit is contained in:
@@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
|
||||
"gitoa.ru/go-4devs/config/provider/arg"
|
||||
"gitoa.ru/go-4devs/config/provider/chain"
|
||||
@@ -22,7 +23,7 @@ func main() {
|
||||
console.
|
||||
New(console.WithInput(
|
||||
chain.New(
|
||||
arg.New(arg.WithSkip(0)),
|
||||
arg.New(arg.WithArgs(os.Args)),
|
||||
env.New(Namespace, AppName),
|
||||
&memory.Default{},
|
||||
),
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
module gitoa.ru/go-4devs/console/example
|
||||
|
||||
go 1.23
|
||||
go 1.24.0
|
||||
|
||||
toolchain go1.24.1
|
||||
|
||||
require (
|
||||
gitoa.ru/go-4devs/config v0.0.7
|
||||
gitoa.ru/go-4devs/console v0.2.0
|
||||
gitoa.ru/go-4devs/config v0.0.8
|
||||
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.7/go.mod h1:UINWnObZA0nLiJro+TtavUBBvN0cSt17aRHOk20pP74=
|
||||
gitoa.ru/go-4devs/config v0.0.8 h1:o4p8I9jWJMfiFVVKr50IqCGj1fF+8kmSPDkH0deRvn4=
|
||||
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/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/definition/option"
|
||||
"gitoa.ru/go-4devs/console"
|
||||
"gitoa.ru/go-4devs/console/command"
|
||||
"gitoa.ru/go-4devs/console/output"
|
||||
)
|
||||
|
||||
func Args() *console.Command {
|
||||
return &console.Command{
|
||||
Name: "fdevs:console:arg",
|
||||
Description: "Understanding how Console Arguments and Options Are Handled",
|
||||
Configure: func(_ context.Context, def config.Definition) error {
|
||||
func Args() command.Command {
|
||||
return command.New(
|
||||
"fdevs:console:arg",
|
||||
"Understanding how Console Arguments and Options Are Handled",
|
||||
ArgExecute,
|
||||
command.Configure(ArgConfigure),
|
||||
)
|
||||
}
|
||||
|
||||
func ArgConfigure(_ context.Context, def config.Definition) error {
|
||||
def.Add(
|
||||
option.Bool("foo", "foo option", option.Short('f')),
|
||||
option.String("bar", "required bar option", option.Required, option.Short('b')),
|
||||
@@ -24,8 +30,9 @@ func Args() *console.Command {
|
||||
)
|
||||
|
||||
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, "bar: <info>", console.ReadValue(ctx, in, "bar").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>")
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,19 +5,29 @@ import (
|
||||
|
||||
"gitoa.ru/go-4devs/config"
|
||||
"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"
|
||||
"gitoa.ru/go-4devs/console"
|
||||
"gitoa.ru/go-4devs/console/command"
|
||||
"gitoa.ru/go-4devs/console/output"
|
||||
"gitoa.ru/go-4devs/console/param"
|
||||
)
|
||||
|
||||
func CreateUser(required bool) *console.Command {
|
||||
return &console.Command{
|
||||
Name: "app:create-user",
|
||||
Description: "Creates a new user.",
|
||||
Help: "This command allows you to create a user...",
|
||||
Configure: func(_ context.Context, cfg config.Definition) error {
|
||||
var opts []param.Option
|
||||
func CreateUser(required bool) command.Command {
|
||||
return command.New(
|
||||
"app:create-user",
|
||||
"Creates a new user.",
|
||||
UserExecute,
|
||||
command.Configure(UserConfigure(required)),
|
||||
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 {
|
||||
opts = append(opts, option.Required)
|
||||
}
|
||||
@@ -29,13 +39,13 @@ func CreateUser(required bool) *console.Command {
|
||||
)
|
||||
|
||||
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"
|
||||
out.Println(ctx, "User Creator")
|
||||
out.Println(ctx, "Username: ", console.ReadValue(ctx, in, "username").String())
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,14 +7,20 @@ import (
|
||||
"gitoa.ru/go-4devs/config/definition/option"
|
||||
"gitoa.ru/go-4devs/config/provider/arg"
|
||||
"gitoa.ru/go-4devs/console"
|
||||
"gitoa.ru/go-4devs/console/command"
|
||||
"gitoa.ru/go-4devs/console/output"
|
||||
)
|
||||
|
||||
func Hello() *console.Command {
|
||||
return &console.Command{
|
||||
Name: "fdevs:console:hello",
|
||||
Description: "example hello command",
|
||||
Execute: func(ctx context.Context, in config.Provider, out output.Output) error {
|
||||
func Hello() command.Command {
|
||||
return command.New(
|
||||
"fdevs:console:hello",
|
||||
"example hello command",
|
||||
HelloExecute,
|
||||
command.Configure(HelloConfigure),
|
||||
)
|
||||
}
|
||||
|
||||
func HelloExecute(ctx context.Context, in config.Provider, out output.Output) error {
|
||||
name := console.ReadValue(ctx, in, "name").String()
|
||||
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>")
|
||||
|
||||
return nil
|
||||
},
|
||||
Configure: func(_ context.Context, def config.Definition) error {
|
||||
}
|
||||
|
||||
func HelloConfigure(_ context.Context, def config.Definition) error {
|
||||
def.Add(
|
||||
arg.String("name", "Same name", arg.Default("World")),
|
||||
option.String("pass", "password", option.Hidden),
|
||||
)
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,19 +4,16 @@ import (
|
||||
"context"
|
||||
|
||||
"gitoa.ru/go-4devs/config"
|
||||
"gitoa.ru/go-4devs/console"
|
||||
"gitoa.ru/go-4devs/console/command"
|
||||
"gitoa.ru/go-4devs/console/output"
|
||||
)
|
||||
|
||||
func Hidden() *console.Command {
|
||||
return &console.Command{
|
||||
Name: "fdevs:console:hidden",
|
||||
Description: "hidden command exmale",
|
||||
Hidden: true,
|
||||
Execute: func(ctx context.Context, _ config.Provider, out output.Output) error {
|
||||
func Hidden() command.Command {
|
||||
return command.New("fdevs:console:hidden", "hidden command exmale", HiddenExecute, command.Hidden)
|
||||
}
|
||||
|
||||
func HiddenExecute(ctx context.Context, _ config.Provider, out output.Output) error {
|
||||
out.Println(ctx, "<info> call hidden command</info>")
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,16 +9,18 @@ import (
|
||||
"gitoa.ru/go-4devs/config/validator"
|
||||
"gitoa.ru/go-4devs/config/value"
|
||||
"gitoa.ru/go-4devs/console"
|
||||
"gitoa.ru/go-4devs/console/command"
|
||||
"gitoa.ru/go-4devs/console/output"
|
||||
)
|
||||
|
||||
const defaultTimeout = time.Second * 30
|
||||
|
||||
// Long example of a command that takes a long time to run.
|
||||
func Long() *console.Command {
|
||||
return &console.Command{
|
||||
Name: "fdevs:command:long",
|
||||
Execute: func(ctx context.Context, in config.Provider, out output.Output) error {
|
||||
func Long() command.Command {
|
||||
return command.New("fdevs:command:long", "long command description", LongExecute, command.Configure(LongConfigure))
|
||||
}
|
||||
|
||||
func LongExecute(ctx context.Context, in config.Provider, out output.Output) error {
|
||||
timeout := console.ReadValue(ctx, in, "timeout").Duration()
|
||||
timer := time.NewTimer(timeout)
|
||||
|
||||
@@ -39,8 +41,9 @@ func Long() *console.Command {
|
||||
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",
|
||||
option.Default(value.New(defaultTimeout)),
|
||||
option.Short('t'),
|
||||
@@ -48,6 +51,4 @@ func Long() *console.Command {
|
||||
))
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,18 +4,16 @@ import (
|
||||
"context"
|
||||
|
||||
"gitoa.ru/go-4devs/config"
|
||||
"gitoa.ru/go-4devs/console"
|
||||
"gitoa.ru/go-4devs/console/command"
|
||||
"gitoa.ru/go-4devs/console/output"
|
||||
)
|
||||
|
||||
func Namespace() *console.Command {
|
||||
return &console.Command{
|
||||
Name: "app:start",
|
||||
Description: "example command in other namespace",
|
||||
Execute: func(ctx context.Context, _ config.Provider, out output.Output) error {
|
||||
func Namespace() command.Command {
|
||||
return command.New("app:start", "example command in other namespace", NSExecute)
|
||||
}
|
||||
|
||||
func NSExecute(ctx context.Context, _ config.Provider, out output.Output) error {
|
||||
out.Println(ctx, "example command in other namespace")
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user