add value vithh error (#1)
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing

Co-authored-by: andrey1s <andrey@4devs.pro>
Reviewed-on: #1
Co-authored-by: andrey <andrey@4devs.io>
Co-committed-by: andrey <andrey@4devs.io>
This commit was merged in pull request #1.
This commit is contained in:
2022-09-18 21:37:25 +03:00
parent 2657672288
commit 4fdeb73e8a
74 changed files with 2588 additions and 939 deletions

View File

@@ -11,7 +11,7 @@ import (
var ErrDescriptorNotFound = errors.New("descriptor not found")
//nolint: gochecknoglobals
//nolint:gochecknoglobals
var (
descriptors = map[string]Descriptor{
"txt": &txt{},

View File

@@ -10,8 +10,8 @@ import (
"time"
"gitoa.ru/go-4devs/console/input"
"gitoa.ru/go-4devs/console/input/flag"
"gitoa.ru/go-4devs/console/input/value"
"gitoa.ru/go-4devs/console/input/value/flag"
"gitoa.ru/go-4devs/console/output"
)
@@ -43,7 +43,7 @@ var (
{{- help . }}
`))
txtListTempkate = template.Must(template.New("txt_list").
txtListTemplate = template.Must(template.New("txt_list").
Funcs(txtFunc).
Parse(`<comment>Usage:</comment>
command [options] [arguments]
@@ -58,7 +58,7 @@ func (t *txt) Command(ctx context.Context, out output.Output, cmd Command) error
var tpl bytes.Buffer
if err := txtHelpTemplate.Execute(&tpl, cmd); err != nil {
return err
return fmt.Errorf("execute txt help tpl:%w", err)
}
out.Println(ctx, tpl.String())
@@ -69,8 +69,8 @@ func (t *txt) Command(ctx context.Context, out output.Output, cmd Command) error
func (t *txt) Commands(ctx context.Context, out output.Output, cmds Commands) error {
var buf bytes.Buffer
if err := txtListTempkate.Execute(&buf, cmds); err != nil {
return err
if err := txtListTemplate.Execute(&buf, cmds); err != nil {
return fmt.Errorf("execute txt list tpl:%w", err)
}
out.Println(ctx, buf.String())
@@ -78,36 +78,37 @@ func (t *txt) Commands(ctx context.Context, out output.Output, cmds Commands) er
return nil
}
func txtDefaultArray(v value.Value, f flag.Flag) string {
st := v.Strings()
//nolint:cyclop
func txtDefaultArray(val value.Value, fl flag.Flag) string {
st := val.Strings()
switch {
case f.IsInt():
for _, i := range v.Ints() {
case fl.IsInt():
for _, i := range val.Ints() {
st = append(st, strconv.Itoa(i))
}
case f.IsInt64():
for _, i := range v.Int64s() {
case fl.IsInt64():
for _, i := range val.Int64s() {
st = append(st, strconv.FormatInt(i, 10))
}
case f.IsUint():
for _, u := range v.Uints() {
case fl.IsUint():
for _, u := range val.Uints() {
st = append(st, strconv.FormatUint(uint64(u), 10))
}
case f.IsUint64():
for _, u := range v.Uint64s() {
case fl.IsUint64():
for _, u := range val.Uint64s() {
st = append(st, strconv.FormatUint(u, 10))
}
case f.IsFloat64():
for _, f := range v.Float64s() {
case fl.IsFloat64():
for _, f := range val.Float64s() {
st = append(st, strconv.FormatFloat(f, 'g', -1, 64))
}
case f.IsDuration():
for _, d := range v.Durations() {
case fl.IsDuration():
for _, d := range val.Durations() {
st = append(st, d.String())
}
case f.IsTime():
for _, d := range v.Times() {
case fl.IsTime():
for _, d := range val.Times() {
st = append(st, d.Format(time.RFC3339))
}
}
@@ -115,32 +116,33 @@ func txtDefaultArray(v value.Value, f flag.Flag) string {
return strings.Join(st, ",")
}
func txtDefault(v value.Value, f flag.Flag) []byte {
//nolint:cyclop
func txtDefault(val value.Value, fl flag.Flag) []byte {
var buf bytes.Buffer
buf.WriteString("<comment> [default: ")
switch {
case f.IsArray():
buf.WriteString(txtDefaultArray(v, f))
case f.IsInt():
buf.WriteString(strconv.Itoa(v.Int()))
case f.IsInt64():
buf.WriteString(strconv.FormatInt(v.Int64(), 10))
case f.IsUint():
buf.WriteString(strconv.FormatUint(uint64(v.Uint()), 10))
case f.IsUint64():
buf.WriteString(strconv.FormatUint(v.Uint64(), 10))
case f.IsFloat64():
buf.WriteString(strconv.FormatFloat(v.Float64(), 'g', -1, 64))
case f.IsDuration():
buf.WriteString(v.Duration().String())
case f.IsTime():
buf.WriteString(v.Time().Format(time.RFC3339))
case f.IsAny():
buf.WriteString(fmt.Sprint(v.Any()))
case fl.IsArray():
buf.WriteString(txtDefaultArray(val, fl))
case fl.IsInt():
buf.WriteString(strconv.Itoa(val.Int()))
case fl.IsInt64():
buf.WriteString(strconv.FormatInt(val.Int64(), 10))
case fl.IsUint():
buf.WriteString(strconv.FormatUint(uint64(val.Uint()), 10))
case fl.IsUint64():
buf.WriteString(strconv.FormatUint(val.Uint64(), 10))
case fl.IsFloat64():
buf.WriteString(strconv.FormatFloat(val.Float64(), 'g', -1, 64))
case fl.IsDuration():
buf.WriteString(val.Duration().String())
case fl.IsTime():
buf.WriteString(val.Time().Format(time.RFC3339))
case fl.IsAny():
buf.WriteString(fmt.Sprint(val.Any()))
default:
buf.WriteString(v.String())
buf.WriteString(val.String())
}
buf.WriteString("]</comment>")
@@ -214,7 +216,7 @@ func txtDefinitionOption(maxLen int, def *input.Definition) string {
if opt.HasShort() {
op.WriteString("-")
op.WriteString(opt.Short)
op.WriteString(opt.Alias)
op.WriteString(", ")
} else {
op.WriteString(" ")
@@ -354,18 +356,18 @@ func totalWidth(def *input.Definition) int {
for _, name := range def.Options() {
opt, _ := def.Option(name)
l := len(opt.Name) + 6
current := len(opt.Name) + 6
if !opt.IsBool() {
l = l*2 + 1
current = current*2 + 1
}
if opt.HasDefault() {
l += 2
current += 2
}
if l > max {
max = l
if current > max {
max = current
}
}

View File

@@ -8,7 +8,6 @@ import (
"gitoa.ru/go-4devs/console/output/style"
)
//nolint: gochecknoglobals
var re = regexp.MustCompile(`<(([a-z][^<>]+)|/([a-z][^<>]+)?)>`)
func WithStyle(styles func(string) (style.Style, error)) func(*Formatter) {
@@ -18,15 +17,15 @@ func WithStyle(styles func(string) (style.Style, error)) func(*Formatter) {
}
func New(opts ...func(*Formatter)) *Formatter {
f := &Formatter{
formatter := &Formatter{
styles: style.Find,
}
for _, opt := range opts {
opt(f)
opt(formatter)
}
return f
return formatter
}
type Formatter struct {

View File

@@ -8,6 +8,8 @@ import (
)
func TestFormatter(t *testing.T) {
t.Parallel()
ctx := context.Background()
formatter := formatter.New()

View File

@@ -8,6 +8,8 @@ import (
)
func TestNone(t *testing.T) {
t.Parallel()
ctx := context.Background()
none := formatter.None()

View File

@@ -63,15 +63,13 @@ func (o Output) Write(b []byte) (int, error) {
}
func (o Output) Writer(ctx context.Context, verb verbosity.Verbosity) io.Writer {
return verbosityWriter{ctx, o, verb}
return verbosityWriter(func(b []byte) (int, error) {
return o(ctx, verb, string(b))
})
}
type verbosityWriter struct {
ctx context.Context
out Output
verb verbosity.Verbosity
}
type verbosityWriter func(b []byte) (int, error)
func (w verbosityWriter) Write(b []byte) (int, error) {
return w.out(w.ctx, w.verb, string(b))
return w(b)
}

View File

@@ -28,13 +28,13 @@ const (
type Option string
func (o Option) Apply(action int) string {
v := string(o)
out := string(o)
switch action {
case ActionSet:
return v[0:1]
return out[0:1]
case ActionUnset:
return v[1:]
return out[1:]
}
return ""

View File

@@ -7,7 +7,7 @@ import (
"sync"
)
//nolint: gochecknoglobals
//nolint:gochecknoglobals
var (
styles = map[string]Style{
"error": {Foreground: White, Background: Red},

View File

@@ -41,6 +41,11 @@ func FormatString(_ verbosity.Verbosity, msg string, kv ...label.KeyValue) strin
func New(w io.Writer, format func(verb verbosity.Verbosity, msg string, kv ...label.KeyValue) string) Output {
return func(ctx context.Context, verb verbosity.Verbosity, msg string, kv ...label.KeyValue) (int, error) {
return fmt.Fprint(w, format(verb, msg, kv...))
out, err := fmt.Fprint(w, format(verb, msg, kv...))
if err != nil {
return 0, fmt.Errorf("writer fprint:%w", err)
}
return out, nil
}
}

View File

@@ -10,6 +10,8 @@ import (
)
func TestNew(t *testing.T) {
t.Parallel()
ctx := context.Background()
buf := bytes.Buffer{}
wr := output.New(&buf, output.FormatString)