gencmd

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: May 26, 2026 License: MIT Imports: 10 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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