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,32 +7,37 @@ 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,
|
||||||
def.Add(
|
command.Configure(ArgConfigure),
|
||||||
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"),
|
func ArgConfigure(_ context.Context, def config.Definition) error {
|
||||||
option.Time("hidden", "hidden time example", option.Hidden),
|
def.Add(
|
||||||
)
|
option.Bool("foo", "foo option", option.Short('f')),
|
||||||
|
option.String("bar", "required bar option", option.Required, option.Short('b')),
|
||||||
return nil
|
option.String("cat", "cat option", option.Short('c')),
|
||||||
},
|
option.Time("time", "time example"),
|
||||||
Execute: func(ctx context.Context, in config.Provider, out output.Output) error {
|
option.Time("hidden", "hidden time example", option.Hidden),
|
||||||
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>")
|
return nil
|
||||||
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>")
|
|
||||||
|
func ArgExecute(ctx context.Context, in config.Provider, out output.Output) error {
|
||||||
return nil
|
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"
|
||||||
"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) {
|
||||||
if required {
|
return "This command allows you to create a user...", nil
|
||||||
opts = append(opts, option.Required)
|
}),
|
||||||
}
|
)
|
||||||
|
}
|
||||||
|
|
||||||
cfg.
|
func UserConfigure(required bool) func(_ context.Context, cfg config.Definition) error {
|
||||||
Add(
|
return func(_ context.Context, cfg config.Definition) error {
|
||||||
argument.String("username", "The username of the user.", option.Required),
|
var opts []cparam.Option
|
||||||
argument.String("password", "User password", opts...),
|
if required {
|
||||||
)
|
opts = append(opts, option.Required)
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
cfg.
|
||||||
},
|
Add(
|
||||||
Execute: func(ctx context.Context, in config.Provider, out output.Output) error {
|
argument.String("username", "The username of the user.", option.Required),
|
||||||
// outputs a message followed by a "\n"
|
argument.String("password", "User password", opts...),
|
||||||
out.Println(ctx, "User Creator")
|
)
|
||||||
out.Println(ctx, "Username: ", console.ReadValue(ctx, in, "username").String())
|
|
||||||
|
|
||||||
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/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,
|
||||||
name := console.ReadValue(ctx, in, "name").String()
|
command.Configure(HelloConfigure),
|
||||||
out.Println(ctx, "<error>Hello</error> <info>", name, "</info>")
|
)
|
||||||
|
}
|
||||||
out.Info(ctx, "same trace info\n")
|
|
||||||
out.Debug(ctx, "have some question?\n")
|
func HelloExecute(ctx context.Context, in config.Provider, out output.Output) error {
|
||||||
out.Trace(ctx, "this message shows with -vvv\n")
|
name := console.ReadValue(ctx, in, "name").String()
|
||||||
|
out.Println(ctx, "<error>Hello</error> <info>", name, "</info>")
|
||||||
pass := console.ReadValue(ctx, in, "pass").String()
|
|
||||||
out.Println(ctx, "hidden option pass <info>", pass, "</info>")
|
out.Info(ctx, "same trace info\n")
|
||||||
|
out.Debug(ctx, "have some question?\n")
|
||||||
return nil
|
out.Trace(ctx, "this message shows with -vvv\n")
|
||||||
},
|
|
||||||
Configure: func(_ context.Context, def config.Definition) error {
|
pass := console.ReadValue(ctx, in, "pass").String()
|
||||||
def.Add(
|
out.Println(ctx, "hidden option pass <info>", pass, "</info>")
|
||||||
arg.String("name", "Same name", arg.Default("World")),
|
|
||||||
option.String("pass", "password", option.Hidden),
|
return nil
|
||||||
)
|
}
|
||||||
|
|
||||||
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"
|
"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,45 +9,46 @@ 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 {
|
|
||||||
timeout := console.ReadValue(ctx, in, "timeout").Duration()
|
|
||||||
timer := time.NewTimer(timeout)
|
|
||||||
|
|
||||||
ticker := time.NewTicker(time.Second)
|
func LongExecute(ctx context.Context, in config.Provider, out output.Output) error {
|
||||||
defer ticker.Stop()
|
timeout := console.ReadValue(ctx, in, "timeout").Duration()
|
||||||
|
timer := time.NewTimer(timeout)
|
||||||
|
|
||||||
for {
|
ticker := time.NewTicker(time.Second)
|
||||||
select {
|
defer ticker.Stop()
|
||||||
case t := <-ticker.C:
|
|
||||||
out.Println(ctx, "ticker: <info>", t, "</info>")
|
|
||||||
case <-timer.C:
|
|
||||||
out.Println(ctx, "<error>stop timer</error>")
|
|
||||||
|
|
||||||
return nil
|
for {
|
||||||
case <-ctx.Done():
|
select {
|
||||||
out.Println(ctx, "<info>cancel context</info>")
|
case t := <-ticker.C:
|
||||||
|
out.Println(ctx, "ticker: <info>", t, "</info>")
|
||||||
return nil
|
case <-timer.C:
|
||||||
}
|
out.Println(ctx, "<error>stop timer</error>")
|
||||||
}
|
|
||||||
},
|
|
||||||
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),
|
|
||||||
))
|
|
||||||
|
|
||||||
return nil
|
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"
|
"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