You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
1.9 KiB

package command
import (
"context"
"fmt"
"gitoa.ru/go-4devs/console"
"gitoa.ru/go-4devs/console/input"
"gitoa.ru/go-4devs/console/input/argument"
"gitoa.ru/go-4devs/console/input/option"
"gitoa.ru/go-4devs/console/output"
"gitoa.ru/go-4devs/mime/scripts/mime"
)
const (
ArgFile = "file"
OptExtTpl = "ext-tpl"
OptExtPackage = "ext-package"
OptExtResult = "ext-result"
OptMimeTpl = "mime-tpl"
OptMimePackage = "mime-package"
OptMimeResult = "mime-result"
)
func Mime() *console.Command {
return &console.Command{
Name: "mime",
Description: "generate mime from file",
Configure: func(ctx context.Context, cfg *input.Definition) error {
cfg.SetArgument(ArgFile, "yaml file", argument.Required)
cfg.SetOptions(
option.String(OptExtTpl, "extension template", option.Default("mime/tpl/extension.text.tmpl")),
option.String(OptExtPackage, "extension package", option.Default("mime")),
option.String(OptExtResult, "extension result", option.Default("../extension.go")),
option.String(OptMimeTpl, "mime template", option.Default("mime/tpl/mime.text.tmpl")),
option.String(OptMimePackage, "mime package", option.Default("mime")),
option.String(OptMimeResult, "mime result", option.Default("../mime.go")),
)
return nil
},
Execute: func(ctx context.Context, input input.Input, output output.Output) error {
file := input.Argument(ctx, ArgFile).String()
opts := []mime.Option{
mime.WithExtTpl(input.Option(ctx, OptExtTpl).String()),
mime.WithExtPacakge(input.Option(ctx, OptExtPackage).String()),
mime.WithExtResult(input.Option(ctx, OptExtResult).String()),
mime.WithMimeTpl(input.Option(ctx, OptMimeTpl).String()),
mime.WithMimePackage(input.Option(ctx, OptMimePackage).String()),
mime.WithMimeResult(input.Option(ctx, OptMimeResult).String()),
}
if err := mime.Generate(file, opts...); err != nil {
return fmt.Errorf("generate:%w", err)
}
return nil
},
}
}