gencmd

package
v0.0.0-...-9c55465 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 10, 2024 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var CodeGenCmd = &gcli.Command{
	Name:    "gen",
	Desc:    "quickly code or project generate",
	Aliases: []string{"cgen", "generate"},
	Subs: []*gcli.Command{
		ParseTemplateCmd,
		NewProjectCmd(),
	},
}

CodeGenCmd instance

View Source
var ParseTemplateCmd = &gcli.Command{
	Name: "parse",
	Desc: "parse template for generate code",
	Func: func(c *gcli.Command, args []string) error {
		if !fsutil.IsFile(genOpts.tpl) {
			return c.NewErr("template file not exists")
		}

		c.Infoln("will generate code by parse:", genOpts.tpl)

		buf := new(bytes.Buffer)
		bts := fsutil.MustReadFile(genOpts.tpl)

		nodes := strings.SplitN(string(bts), "\n###\n", 2)
		if len(nodes) != 2 {
			return c.NewErr("template content is invalid")
		}

		varCode, tplCode := nodes[0], nodes[1]

		c.Infoln("- parse template vars")
		err := ini.LoadStrings(varCode)
		if err != nil {
			return err
		}

		data := ini.StringMap("")

		varMap := make(map[string]interface{}, len(data))
		for k, v := range data {

			if v[0] == '[' {
				varMap[k] = strutil.Split(strings.Trim(v, "[]"), ",")
			} else {
				varMap[k] = v
			}
		}

		if gcli.IsDebugMode() {
			d := dump.NewWithOptions(func(opts *dump.Options) {
				opts.ShowFlag = dump.Fnopos
			})

			d.Println(varMap)
		}

		c.Infoln("- render template contents")
		t := template.New("parseTpl")
		template.Must(t.Parse(tplCode))

		err = t.Execute(buf, varMap)
		if err != nil {
			return err
		}

		fmt.Print(buf.String())
		return nil
	},

	Config: func(c *gcli.Command) {
		c.StrOpt(&genOpts.tpl, "tpl", "t", "", "the template file path")
	},
}

ParseTemplateCmd instance

Functions

func NewJsonToCodeCmd

func NewJsonToCodeCmd() *gcli.Command

NewJsonToCodeCmd instance

func NewProjectCmd

func NewProjectCmd() *gcli.Command

NewProjectCmd create a new project command

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL