andrey1s
4 years ago
43 changed files with 510 additions and 493 deletions
@ -1,26 +1,26 @@ |
|||
package argument |
|||
|
|||
import ( |
|||
"gitoa.ru/go-4devs/console/input" |
|||
"gitoa.ru/go-4devs/console/input/flag" |
|||
"gitoa.ru/go-4devs/console/input/value" |
|||
) |
|||
|
|||
func Required(a *input.Argument) { |
|||
a.Flag |= input.ValueRequired |
|||
func Required(a *Argument) { |
|||
a.Flag |= flag.Required |
|||
} |
|||
|
|||
func Default(v interface{}) func(*input.Argument) { |
|||
return func(a *input.Argument) { |
|||
func Default(v interface{}) func(*Argument) { |
|||
return func(a *Argument) { |
|||
a.Default = value.New(v) |
|||
} |
|||
} |
|||
|
|||
func Flag(flag input.Flag) func(*input.Argument) { |
|||
return func(a *input.Argument) { |
|||
func Flag(flag flag.Flag) func(*Argument) { |
|||
return func(a *Argument) { |
|||
a.Flag = flag |
|||
} |
|||
} |
|||
|
|||
func Array(a *input.Argument) { |
|||
a.Flag |= input.ValueArray |
|||
func Array(a *Argument) { |
|||
a.Flag |= flag.Array |
|||
} |
|||
|
@ -1,76 +0,0 @@ |
|||
package input |
|||
|
|||
//go:generate stringer -type=Flag -linecomment
|
|||
|
|||
type Flag int |
|||
|
|||
const ( |
|||
ValueString Flag = 0 // string
|
|||
ValueRequired Flag = 1 << iota // required
|
|||
ValueArray // array
|
|||
ValueInt // int
|
|||
ValueInt64 // int64
|
|||
ValueUint // uint
|
|||
ValueUint64 // uint64
|
|||
ValueFloat64 // float64
|
|||
ValueBool // bool
|
|||
ValueDuration // duration
|
|||
ValueTime // time
|
|||
ValueAny // any
|
|||
) |
|||
|
|||
func (f Flag) Type() Flag { |
|||
return Type(f) |
|||
} |
|||
|
|||
func (f Flag) With(v Flag) Flag { |
|||
return f | v |
|||
} |
|||
|
|||
func (f Flag) IsString() bool { |
|||
return f|ValueRequired|ValueArray^ValueRequired^ValueArray == 0 |
|||
} |
|||
|
|||
func (f Flag) IsRequired() bool { |
|||
return f&ValueRequired > 0 |
|||
} |
|||
|
|||
func (f Flag) IsArray() bool { |
|||
return f&ValueArray > 0 |
|||
} |
|||
|
|||
func (f Flag) IsInt() bool { |
|||
return f&ValueInt > 0 |
|||
} |
|||
|
|||
func (f Flag) IsInt64() bool { |
|||
return f&ValueInt64 > 0 |
|||
} |
|||
|
|||
func (f Flag) IsUint() bool { |
|||
return f&ValueUint > 0 |
|||
} |
|||
|
|||
func (f Flag) IsUint64() bool { |
|||
return f&ValueUint64 > 0 |
|||
} |
|||
|
|||
func (f Flag) IsFloat64() bool { |
|||
return f&ValueFloat64 > 0 |
|||
} |
|||
|
|||
func (f Flag) IsBool() bool { |
|||
return f&ValueBool > 0 |
|||
} |
|||
|
|||
func (f Flag) IsDuration() bool { |
|||
return f&ValueDuration > 0 |
|||
} |
|||
|
|||
func (f Flag) IsTime() bool { |
|||
return f&ValueTime > 0 |
|||
} |
|||
|
|||
func (f Flag) IsAny() bool { |
|||
return f&ValueAny > 0 |
|||
} |
@ -0,0 +1,97 @@ |
|||
package flag |
|||
|
|||
//go:generate stringer -type=Flag -linecomment
|
|||
|
|||
type Flag int |
|||
|
|||
const ( |
|||
String Flag = 0 // string
|
|||
Required Flag = 1 << iota // required
|
|||
Array // array
|
|||
Int // int
|
|||
Int64 // int64
|
|||
Uint // uint
|
|||
Uint64 // uint64
|
|||
Float64 // float64
|
|||
Bool // bool
|
|||
Duration // duration
|
|||
Time // time
|
|||
Any // any
|
|||
) |
|||
|
|||
func (f Flag) With(v Flag) Flag { |
|||
return f | v |
|||
} |
|||
|
|||
func (f Flag) IsString() bool { |
|||
return f|Required|Array^Required^Array == 0 |
|||
} |
|||
|
|||
func (f Flag) IsRequired() bool { |
|||
return f&Required > 0 |
|||
} |
|||
|
|||
func (f Flag) IsArray() bool { |
|||
return f&Array > 0 |
|||
} |
|||
|
|||
func (f Flag) IsInt() bool { |
|||
return f&Int > 0 |
|||
} |
|||
|
|||
func (f Flag) IsInt64() bool { |
|||
return f&Int64 > 0 |
|||
} |
|||
|
|||
func (f Flag) IsUint() bool { |
|||
return f&Uint > 0 |
|||
} |
|||
|
|||
func (f Flag) IsUint64() bool { |
|||
return f&Uint64 > 0 |
|||
} |
|||
|
|||
func (f Flag) IsFloat64() bool { |
|||
return f&Float64 > 0 |
|||
} |
|||
|
|||
func (f Flag) IsBool() bool { |
|||
return f&Bool > 0 |
|||
} |
|||
|
|||
func (f Flag) IsDuration() bool { |
|||
return f&Duration > 0 |
|||
} |
|||
|
|||
func (f Flag) IsTime() bool { |
|||
return f&Time > 0 |
|||
} |
|||
|
|||
func (f Flag) IsAny() bool { |
|||
return f&Any > 0 |
|||
} |
|||
|
|||
func (f Flag) Type() Flag { |
|||
switch { |
|||
case f.IsInt(): |
|||
return Int |
|||
case f.IsInt64(): |
|||
return Int64 |
|||
case f.IsUint(): |
|||
return Uint |
|||
case f.IsUint64(): |
|||
return Uint64 |
|||
case f.IsFloat64(): |
|||
return Float64 |
|||
case f.IsBool(): |
|||
return Bool |
|||
case f.IsDuration(): |
|||
return Duration |
|||
case f.IsTime(): |
|||
return Time |
|||
case f.IsAny(): |
|||
return Any |
|||
default: |
|||
return String |
|||
} |
|||
} |
@ -1,35 +1,37 @@ |
|||
package option |
|||
|
|||
import "gitoa.ru/go-4devs/console/input" |
|||
import ( |
|||
"gitoa.ru/go-4devs/console/input/flag" |
|||
) |
|||
|
|||
func Bool(name, description string, opts ...func(*input.Option)) input.Option { |
|||
return input.NewOption(name, description, append(opts, Value(input.ValueBool))...) |
|||
func Bool(name, description string, opts ...func(*Option)) Option { |
|||
return New(name, description, append(opts, Value(flag.Bool))...) |
|||
} |
|||
|
|||
func Duration(name, description string, opts ...func(*input.Option)) input.Option { |
|||
return input.NewOption(name, description, append(opts, Value(input.ValueDuration))...) |
|||
func Duration(name, description string, opts ...func(*Option)) Option { |
|||
return New(name, description, append(opts, Value(flag.Duration))...) |
|||
} |
|||
|
|||
func Float64(name, description string, opts ...func(*input.Option)) input.Option { |
|||
return input.NewOption(name, description, append(opts, Value(input.ValueFloat64))...) |
|||
func Float64(name, description string, opts ...func(*Option)) Option { |
|||
return New(name, description, append(opts, Value(flag.Float64))...) |
|||
} |
|||
|
|||
func Int(name, description string, opts ...func(*input.Option)) input.Option { |
|||
return input.NewOption(name, description, append(opts, Value(input.ValueInt))...) |
|||
func Int(name, description string, opts ...func(*Option)) Option { |
|||
return New(name, description, append(opts, Value(flag.Int))...) |
|||
} |
|||
|
|||
func Int64(name, description string, opts ...func(*input.Option)) input.Option { |
|||
return input.NewOption(name, description, append(opts, Value(input.ValueInt64))...) |
|||
func Int64(name, description string, opts ...func(*Option)) Option { |
|||
return New(name, description, append(opts, Value(flag.Int64))...) |
|||
} |
|||
|
|||
func Time(name, description string, opts ...func(*input.Option)) input.Option { |
|||
return input.NewOption(name, description, append(opts, Value(input.ValueTime))...) |
|||
func Time(name, description string, opts ...func(*Option)) Option { |
|||
return New(name, description, append(opts, Value(flag.Time))...) |
|||
} |
|||
|
|||
func Uint(name, description string, opts ...func(*input.Option)) input.Option { |
|||
return input.NewOption(name, description, append(opts, Value(input.ValueUint))...) |
|||
func Uint(name, description string, opts ...func(*Option)) Option { |
|||
return New(name, description, append(opts, Value(flag.Uint))...) |
|||
} |
|||
|
|||
func Uint64(name, descriontion string, opts ...func(*input.Option)) input.Option { |
|||
return input.NewOption(name, descriontion, append(opts, Value(input.ValueUint64))...) |
|||
func Uint64(name, descriontion string, opts ...func(*Option)) Option { |
|||
return New(name, descriontion, append(opts, Value(flag.Uint64))...) |
|||
} |
|||
|
@ -1,44 +1,100 @@ |
|||
package option |
|||
|
|||
import ( |
|||
"gitoa.ru/go-4devs/console/input" |
|||
"gitoa.ru/go-4devs/console/input/flag" |
|||
"gitoa.ru/go-4devs/console/input/value" |
|||
) |
|||
|
|||
func Required(o *input.Option) { |
|||
o.Flag |= input.ValueRequired |
|||
func Required(o *Option) { |
|||
o.Flag |= flag.Required |
|||
} |
|||
|
|||
func Default(in interface{}) func(*input.Option) { |
|||
return func(o *input.Option) { |
|||
func Default(in interface{}) func(*Option) { |
|||
return func(o *Option) { |
|||
o.Default = value.New(in) |
|||
} |
|||
} |
|||
|
|||
func Short(s string) func(*input.Option) { |
|||
return func(o *input.Option) { |
|||
func Short(s string) func(*Option) { |
|||
return func(o *Option) { |
|||
o.Short = s |
|||
} |
|||
} |
|||
|
|||
func Array(o *input.Option) { |
|||
o.Flag |= input.ValueArray |
|||
func Array(o *Option) { |
|||
o.Flag |= flag.Array |
|||
} |
|||
|
|||
func Value(flag input.Flag) func(*input.Option) { |
|||
return func(o *input.Option) { |
|||
func Value(flag flag.Flag) func(*Option) { |
|||
return func(o *Option) { |
|||
o.Flag |= flag |
|||
} |
|||
} |
|||
|
|||
func Flag(in input.Flag) func(*input.Option) { |
|||
return func(o *input.Option) { |
|||
func Flag(in flag.Flag) func(*Option) { |
|||
return func(o *Option) { |
|||
o.Flag = in |
|||
} |
|||
} |
|||
|
|||
func Valid(f ...func(input.Value) error) func(*input.Option) { |
|||
return func(o *input.Option) { |
|||
func Valid(f ...func(value.Value) error) func(*Option) { |
|||
return func(o *Option) { |
|||
o.Valid = f |
|||
} |
|||
} |
|||
|
|||
func New(name, description string, opts ...func(*Option)) Option { |
|||
o := Option{ |
|||
Name: name, |
|||
Description: description, |
|||
} |
|||
|
|||
for _, opt := range opts { |
|||
opt(&o) |
|||
} |
|||
|
|||
return o |
|||
} |
|||
|
|||
type Option struct { |
|||
Name string |
|||
Description string |
|||
Short string |
|||
Flag flag.Flag |
|||
Default value.Value |
|||
Valid []func(value.Value) error |
|||
} |
|||
|
|||
func (o Option) HasShort() bool { |
|||
return len(o.Short) == 1 |
|||
} |
|||
|
|||
func (o Option) HasDefault() bool { |
|||
return o.Default != nil |
|||
} |
|||
|
|||
func (o Option) IsBool() bool { |
|||
return o.Flag.IsBool() |
|||
} |
|||
|
|||
func (o Option) IsArray() bool { |
|||
return o.Flag.IsArray() |
|||
} |
|||
|
|||
func (o Option) IsRequired() bool { |
|||
return o.Flag.IsRequired() |
|||
} |
|||
|
|||
func (o Option) Validate(v value.Value) error { |
|||
for _, valid := range o.Valid { |
|||
if err := valid(v); err != nil { |
|||
return Error(o.Name, err) |
|||
} |
|||
} |
|||
|
|||
return nil |
|||
} |
|||
|
|||
func Error(name string, err error) error { |
|||
return err |
|||
} |
|||
|
@ -1,9 +1,9 @@ |
|||
package validator |
|||
|
|||
import "gitoa.ru/go-4devs/console/input" |
|||
import "gitoa.ru/go-4devs/console/input/value" |
|||
|
|||
func Enum(enum ...string) func(input.Value) error { |
|||
return func(in input.Value) error { |
|||
func Enum(enum ...string) func(value.Value) error { |
|||
return func(in value.Value) error { |
|||
v := in.String() |
|||
for _, e := range enum { |
|||
if e == v { |
@ -0,0 +1,15 @@ |
|||
package validator |
|||
|
|||
import "gitoa.ru/go-4devs/console/input/value" |
|||
|
|||
func Valid(v ...func(value.Value) error) func(value.Value) error { |
|||
return func(in value.Value) error { |
|||
for _, valid := range v { |
|||
if err := valid(in); err != nil { |
|||
return err |
|||
} |
|||
} |
|||
|
|||
return nil |
|||
} |
|||
} |
@ -1,58 +0,0 @@ |
|||
package input |
|||
|
|||
import ( |
|||
"time" |
|||
) |
|||
|
|||
type Value interface { |
|||
String() string |
|||
Int() int |
|||
Int64() int64 |
|||
Uint() uint |
|||
Uint64() uint64 |
|||
Float64() float64 |
|||
Bool() bool |
|||
Duration() time.Duration |
|||
Time() time.Time |
|||
Any() interface{} |
|||
|
|||
Strings() []string |
|||
Ints() []int |
|||
Int64s() []int64 |
|||
Uints() []uint |
|||
Uint64s() []uint64 |
|||
Float64s() []float64 |
|||
Bools() []bool |
|||
Durations() []time.Duration |
|||
Times() []time.Time |
|||
} |
|||
|
|||
type AppendValue interface { |
|||
Value |
|||
Append(string) error |
|||
} |
|||
|
|||
func Type(flag Flag) Flag { |
|||
switch { |
|||
case (flag & ValueInt) > 0: |
|||
return ValueInt |
|||
case (flag & ValueInt64) > 0: |
|||
return ValueInt64 |
|||
case (flag & ValueUint) > 0: |
|||
return ValueUint |
|||
case (flag & ValueUint64) > 0: |
|||
return ValueUint64 |
|||
case (flag & ValueFloat64) > 0: |
|||
return ValueFloat64 |
|||
case (flag & ValueBool) > 0: |
|||
return ValueBool |
|||
case (flag & ValueDuration) > 0: |
|||
return ValueDuration |
|||
case (flag & ValueTime) > 0: |
|||
return ValueTime |
|||
case (flag & ValueAny) > 0: |
|||
return ValueAny |
|||
default: |
|||
return ValueString |
|||
} |
|||
} |
@ -1,17 +1,20 @@ |
|||
package value |
|||
|
|||
import ( |
|||
"fmt" |
|||
|
|||
"gitoa.ru/go-4devs/console/input" |
|||
"errors" |
|||
) |
|||
|
|||
var _ input.AppendValue = (*Read)(nil) |
|||
var _ AppendValue = (*Read)(nil) |
|||
|
|||
var ( |
|||
ErrAppendRead = errors.New("invalid append data to read value") |
|||
ErrAppendEmpty = errors.New("invalid apped data to empty value") |
|||
) |
|||
|
|||
type Read struct { |
|||
input.Value |
|||
Value |
|||
} |
|||
|
|||
func (r *Read) Append(string) error { |
|||
return fmt.Errorf("%w: read value", input.ErrInvalidName) |
|||
return ErrAppendRead |
|||
} |
|||
|
@ -1,15 +0,0 @@ |
|||
package validator |
|||
|
|||
import "gitoa.ru/go-4devs/console/input" |
|||
|
|||
func Valid(v ...func(input.Value) error) func(input.Value) error { |
|||
return func(in input.Value) error { |
|||
for _, valid := range v { |
|||
if err := valid(in); err != nil { |
|||
return err |
|||
} |
|||
} |
|||
|
|||
return nil |
|||
} |
|||
} |
Loading…
Reference in new issue