cmd

package
v1.9.5 Latest Latest
Warning

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

Go to latest
Published: Sep 5, 2023 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (

	//Dsn .
	Dsn = "root:123123@tcp(127.0.0.1:3306)/xxx?charset=utf8"
	//JSONFile .
	JSONFile = ""
	//OutObj .
	OutObj = "./domain/po"
	//OutFunc .
	OutFunc = "./adapter/repository"
	//Prefix .
	Prefix = ""
	//NewCRUDCmd .
	NewCRUDCmd = &cobra.Command{
		Use:   "new-po",
		Short: "Create the model code for the CRUD.",
		Long:  `Create the model code for the CRUD, You can view subcommands and customize builds`,
		RunE: func(cmd *cobra.Command, args []string) (err error) {
			os.MkdirAll(OutObj, os.ModePerm)
			os.MkdirAll(OutFunc, os.ModePerm)
			tl := crud.PoDefContent()
			funTempl := crud.FunTemplate()
			list, err := GetStruct()
			if err != nil {
				return err
			}
			generateBuffer := new(bytes.Buffer)
			generateTmpl, err := template.New("").Parse(crud.FunTemplatePackage())
			if err != nil {
				return err
			}

			sysPath, err := os.Getwd()
			generateBuild := false

			for index := 0; index < len(list); index++ {
				pdata := map[string]interface{}{
					"Name":        list[index].Name,
					"Content":     list[index].Content,
					"Time":        false,
					"SetMethods":  list[index].SetMethods,
					"AddMethods":  list[index].AddMethods,
					"PrimaryType": list[index].PrimaryType,
					"PrimaryName": list[index].PrimaryName,
					"PrimaryParm": lowerCamelCase(list[index].PrimaryName),
					"Import":      "",
				}
				pdata["Import"] = "import (\n"
				if strings.Contains(list[index].Content, "time.Time") {
					pdata["Import"] = pdata["Import"].(string) + `"time"` + "\n"
				}

				if strings.Contains(list[index].Content, "datatypes.JSON") {
					pdata["Import"] = pdata["Import"].(string) + `"gorm.io/datatypes"` + "\n"
				}

				if len(list[index].AddMethods) > 0 {
					pdata["Import"] = pdata["Import"].(string) + `"gorm.io/gorm"` + "\n"
				}
				pdata["Import"] = pdata["Import"].(string) + ")"

				var pf *os.File
				pf, err = os.Create(OutObj + "/" + list[index].TableRealName + ".go")
				if err != nil {
					return err
				}
				tmpl, err := template.New("").Parse(tl)
				if err != nil {
					return err
				}
				if err = tmpl.Execute(pf, pdata); err != nil {
					return err
				}
				fmt.Println(successString("Success [" + OutObj + "/" + list[index].TableRealName + ".go]"))

				if !generateBuild {
					generatePdata := map[string]interface{}{
						"PackagePath": path.Base(sysPath) + "/domain/po",
					}
					if err = generateTmpl.Execute(generateBuffer, generatePdata); err != nil {
						return err
					}
					generateBuild = true
				}

				tmpl, err = template.New("").Parse(funTempl)
				if err != nil {
					return err
				}
				if err = tmpl.Execute(generateBuffer, pdata); err != nil {
					return err
				}
			}
			ioutil.WriteFile(OutFunc+"/"+"generate.go", generateBuffer.Bytes(), 0755)
			exec.Command("gofmt", "-w", OutObj).Output()
			exec.Command("gofmt", "-w", OutFunc).Output()
			fmt.Println(successString("Success [" + OutFunc + "/" + "generate.go]"))
			return nil
		}}
)
View Source
var (
	// NewProjectCmd .
	NewProjectCmd = &cobra.Command{
		Use:   "new-project [project_name]",
		Short: "New a microservice project based on freedom",
		Long:  `New project from freedom project template. `,
		RunE: func(cmd *cobra.Command, args []string) (err error) {
			if len(args) < 1 || args[0] == "" {
				return errors.New("[project_name] empty")
			}
			sysPath, err := filepath.Abs(args[0])
			if err != nil {
				return
			}
			mkdirAll(sysPath)

			projectName := args[0]
			pdata := map[string]interface{}{
				"PackagePath": projectName,
				"PackageName": projectName,
				"VersionNum":  versionNum,
			}

			m := project.FileContent()
			for filepath, content := range m {
				var pf *os.File
				pf, err = os.Create(sysPath + filepath)
				if err != nil {
					return err
				}
				tmpl, err := template.New(projectName).Parse(content)
				if err = tmpl.Execute(pf, pdata); err != nil {
					return err
				}
			}
			exec.Command("gofmt", "-w", sysPath).Output()
			return nil
		},
	}
)
View Source
var (
	// VersionCmd .
	VersionCmd = &cobra.Command{
		Use:   "version",
		Short: "Output current version number",
		Long:  `Output current version number`,
		RunE: func(cmd *cobra.Command, args []string) (err error) {
			fmt.Println("freedom " + versionNum)
			return
		},
	}
)

Functions

func AddCommand

func AddCommand(cmd *cobra.Command)

AddCommand add sub command to root

func Commands

func Commands() []*cobra.Command

Commands returns all sub command

func Execute

func Execute()

Execute adds all child commands to the root command and sets flags appropriately. This is called by main.main(). It only needs to happen once to the rootCmd.

func GetStruct added in v1.8.3

func GetStruct() (list []crud.ObjectContent, e error)

GetStruct .

Types

This section is empty.

Jump to

Keyboard shortcuts

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