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 (
|
||||
"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,32 +7,37 @@ 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 {
|
||||
def.Add(
|
||||
option.Bool("foo", "foo option", option.Short('f')),
|
||||
option.String("bar", "required bar option", option.Required, option.Short('b')),
|
||||
option.String("cat", "cat option", option.Short('c')),
|
||||
option.Time("time", "time example"),
|
||||
option.Time("hidden", "hidden time example", option.Hidden),
|
||||
)
|
||||
|
||||
return nil
|
||||
},
|
||||
Execute: func(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>")
|
||||
out.Println(ctx, "time: <info>", console.ReadValue(ctx, in, "time").Time().Format(time.RFC3339), "</info>")
|
||||
out.Println(ctx, "hidden: <info>", console.ReadValue(ctx, in, "hidden").Time().Format(time.RFC3339), "</info>")
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
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')),
|
||||
option.String("cat", "cat option", option.Short('c')),
|
||||
option.Time("time", "time example"),
|
||||
option.Time("hidden", "hidden time example", option.Hidden),
|
||||
)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
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>")
|
||||
out.Println(ctx, "time: <info>", console.ReadValue(ctx, in, "time").Time().Format(time.RFC3339), "</info>")
|
||||
out.Println(ctx, "hidden: <info>", console.ReadValue(ctx, in, "hidden").Time().Format(time.RFC3339), "</info>")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -5,37 +5,47 @@ 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
|
||||
if required {
|
||||
opts = append(opts, option.Required)
|
||||
}
|
||||
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
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
cfg.
|
||||
Add(
|
||||
argument.String("username", "The username of the user.", option.Required),
|
||||
argument.String("password", "User password", opts...),
|
||||
)
|
||||
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)
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
Execute: func(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())
|
||||
cfg.
|
||||
Add(
|
||||
argument.String("username", "The username of the user.", option.Required),
|
||||
argument.String("password", "User password", opts...),
|
||||
)
|
||||
|
||||
return nil
|
||||
},
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
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,33 +7,38 @@ 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 {
|
||||
name := console.ReadValue(ctx, in, "name").String()
|
||||
out.Println(ctx, "<error>Hello</error> <info>", name, "</info>")
|
||||
|
||||
out.Info(ctx, "same trace info\n")
|
||||
out.Debug(ctx, "have some question?\n")
|
||||
out.Trace(ctx, "this message shows with -vvv\n")
|
||||
|
||||
pass := console.ReadValue(ctx, in, "pass").String()
|
||||
out.Println(ctx, "hidden option pass <info>", pass, "</info>")
|
||||
|
||||
return nil
|
||||
},
|
||||
Configure: func(_ context.Context, def config.Definition) error {
|
||||
def.Add(
|
||||
arg.String("name", "Same name", arg.Default("World")),
|
||||
option.String("pass", "password", option.Hidden),
|
||||
)
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
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>")
|
||||
|
||||
out.Info(ctx, "same trace info\n")
|
||||
out.Debug(ctx, "have some question?\n")
|
||||
out.Trace(ctx, "this message shows with -vvv\n")
|
||||
|
||||
pass := console.ReadValue(ctx, in, "pass").String()
|
||||
out.Println(ctx, "hidden option pass <info>", pass, "</info>")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
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 {
|
||||
out.Println(ctx, "<info> call hidden command</info>")
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
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,45 +9,46 @@ 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 {
|
||||
timeout := console.ReadValue(ctx, in, "timeout").Duration()
|
||||
timer := time.NewTimer(timeout)
|
||||
func Long() command.Command {
|
||||
return command.New("fdevs:command:long", "long command description", LongExecute, command.Configure(LongConfigure))
|
||||
}
|
||||
|
||||
ticker := time.NewTicker(time.Second)
|
||||
defer ticker.Stop()
|
||||
func LongExecute(ctx context.Context, in config.Provider, out output.Output) error {
|
||||
timeout := console.ReadValue(ctx, in, "timeout").Duration()
|
||||
timer := time.NewTimer(timeout)
|
||||
|
||||
for {
|
||||
select {
|
||||
case t := <-ticker.C:
|
||||
out.Println(ctx, "ticker: <info>", t, "</info>")
|
||||
case <-timer.C:
|
||||
out.Println(ctx, "<error>stop timer</error>")
|
||||
ticker := time.NewTicker(time.Second)
|
||||
defer ticker.Stop()
|
||||
|
||||
return nil
|
||||
case <-ctx.Done():
|
||||
out.Println(ctx, "<info>cancel context</info>")
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
},
|
||||
Configure: func(_ context.Context, def config.Definition) error {
|
||||
def.Add(option.Duration("timeout", "set duration run command",
|
||||
option.Default(value.New(defaultTimeout)),
|
||||
option.Short('t'),
|
||||
validator.Valid(validator.NotBlank),
|
||||
))
|
||||
for {
|
||||
select {
|
||||
case t := <-ticker.C:
|
||||
out.Println(ctx, "ticker: <info>", t, "</info>")
|
||||
case <-timer.C:
|
||||
out.Println(ctx, "<error>stop timer</error>")
|
||||
|
||||
return nil
|
||||
},
|
||||
case <-ctx.Done():
|
||||
out.Println(ctx, "<info>cancel context</info>")
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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'),
|
||||
validator.Valid(validator.NotBlank),
|
||||
))
|
||||
|
||||
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 {
|
||||
out.Println(ctx, "example command in other namespace")
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
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