github.com/kotovmak/go-admin@v1.1.1/adm/cli.go (about)

     1  // Copyright 2019 GoAdmin Core Team. All rights reserved.
     2  // Use of this source code is governed by a Apache-2.0 style
     3  // license that can be found in the LICENSE file.
     4  
     5  package main
     6  
     7  import (
     8  	"fmt"
     9  	_ "github.com/kotovmak/go-admin/modules/db/drivers/mysql"
    10  	_ "github.com/kotovmak/go-admin/modules/db/drivers/oceanbase"
    11  	_ "github.com/kotovmak/go-admin/modules/db/drivers/postgres"
    12  	_ "github.com/kotovmak/go-admin/modules/db/drivers/sqlite"
    13  	"os"
    14  	"runtime"
    15  	"runtime/debug"
    16  
    17  	cli "github.com/jawher/mow.cli"
    18  	"github.com/mgutz/ansi"
    19  )
    20  
    21  func main() {
    22  
    23  	var verbose *bool
    24  
    25  	defer func() {
    26  		if err := recover(); err != nil {
    27  			if errs, ok := err.(error); ok {
    28  				fmt.Println()
    29  				if runtime.GOOS == "windows" && errs.Error() == "Incorrect function." {
    30  					fmt.Println(ansi.Color(getWord("GoAdmin CLI error: CLI has not supported MINGW64 for now, "+
    31  						"please use cmd terminal instead."), "red"))
    32  					fmt.Println(getWord("Know more here: http://discuss.go-admin.com/t/goadmin-cli-adm-does-not-support-git-bash-mingw64-for-now/77"))
    33  				} else {
    34  					fmt.Println(ansi.Color("GoAdmin CLI error: "+errs.Error(), "red"))
    35  
    36  					if *verbose {
    37  						fmt.Println(string(debug.Stack()))
    38  					}
    39  				}
    40  				fmt.Println()
    41  			}
    42  		}
    43  	}()
    44  
    45  	app := cli.App("adm", "GoAdmin CLI tool for developing and generating")
    46  
    47  	app.Spec = "[-v]"
    48  
    49  	verbose = app.BoolOpt("v verbose", false, "debug info output")
    50  	// quiet
    51  
    52  	app.Command("-V version", "display this application version", func(cmd *cli.Cmd) {
    53  		cmd.Action = func() {
    54  			cliInfo()
    55  		}
    56  	})
    57  
    58  	app.Command("combine", "combine assets", func(cmd *cli.Cmd) {
    59  		cmd.Command("css", "combine css assets", func(cmd *cli.Cmd) {
    60  			var (
    61  				rootPath   = cmd.StringOpt("s src", "./resource/assets/src/css/combine/", "css src path")
    62  				outputPath = cmd.StringOpt("d dist", "./resource/assets/dist/css/all.min.css", "css output path")
    63  				hash       = cmd.BoolOpt("h hash", false, "add hash tag to file name")
    64  			)
    65  
    66  			cmd.Action = func() {
    67  				cssMinifier(*rootPath, *outputPath, *hash)
    68  			}
    69  		})
    70  
    71  		cmd.Command("js", "combine js assets", func(cmd *cli.Cmd) {
    72  			var (
    73  				rootPath   = cmd.StringOpt("s src", "./resource/assets/src/js/combine/", "js src path")
    74  				outputPath = cmd.StringOpt("d dist", "./resource/assets/dist/js/all.min.js", "js output path")
    75  				hash       = cmd.BoolOpt("h hash", false, "add hash tag to file name")
    76  			)
    77  
    78  			cmd.Action = func() {
    79  				jsMinifier(*rootPath, *outputPath, *hash)
    80  			}
    81  		})
    82  	})
    83  
    84  	app.Command("compile", "compile template files or assets to one go file", func(cmd *cli.Cmd) {
    85  		cmd.Command("tpl", "compile template files", func(cmd *cli.Cmd) {
    86  			var (
    87  				rootPath    = cmd.StringOpt("s src", "./resource/pages/", "template files src path")
    88  				outputPath  = cmd.StringOpt("d dist", "./template.go", "compile file output path")
    89  				packageName = cmd.StringOpt("p package", "newTmplTheme", "the package name")
    90  				varName     = cmd.StringOpt("v var", "TemplateList", "the variable name")
    91  			)
    92  
    93  			cmd.Action = func() {
    94  				compileTmpl(*rootPath, *outputPath, *packageName, *varName)
    95  			}
    96  		})
    97  
    98  		cmd.Command("asset", "compile assets", func(cmd *cli.Cmd) {
    99  			var (
   100  				rootPath    = cmd.StringOpt("s src", "./resource/assets/dist/", "assets root path")
   101  				outputPath  = cmd.StringOpt("d dist", "./resource/", "compile file output path")
   102  				packageName = cmd.StringOpt("p package", "resource", "package name of the output golang file")
   103  			)
   104  
   105  			cmd.Action = func() {
   106  				compileAsset(*rootPath, *outputPath, *packageName)
   107  			}
   108  		})
   109  	})
   110  
   111  	app.Command("develop", "commands for developing", func(cmd *cli.Cmd) {
   112  		cmd.Command("tpl", "generate a theme project from a remote template", func(cmd *cli.Cmd) {
   113  			var (
   114  				moduleName = cmd.StringOpt("m module", "", "the module path of your theme")
   115  				themeName  = cmd.StringOpt("n name", "newTmplTheme", "the name of your theme")
   116  			)
   117  
   118  			cmd.Action = func() {
   119  				getThemeTemplate(*moduleName, *themeName)
   120  			}
   121  		})
   122  
   123  		cmd.Command("plug", "initialize a plugin project", func(cmd *cli.Cmd) {
   124  			var (
   125  				moduleName = cmd.StringOpt("m module", "", "the module path of your plugin")
   126  				themeName  = cmd.StringOpt("n name", "", "the name of your plugin")
   127  			)
   128  
   129  			cmd.Action = func() {
   130  				getPluginTemplate(*moduleName, *themeName)
   131  			}
   132  		})
   133  	})
   134  
   135  	app.Command("generate", "generate table model files", func(cmd *cli.Cmd) {
   136  
   137  		var (
   138  			config = cmd.StringOpt("c config", "", "config ini path")
   139  			lang   = cmd.StringOpt("l language", "en", "language")
   140  			conn   = cmd.StringOpt("conn connection", "", "connection")
   141  		)
   142  
   143  		cmd.Action = func() {
   144  			setDefaultLangSet(*lang)
   145  			generating(*config, *conn)
   146  		}
   147  	})
   148  
   149  	app.Command("init", "generate a template project", func(cmd *cli.Cmd) {
   150  
   151  		var (
   152  			config = cmd.StringOpt("c config", "", "config ini path")
   153  			lang   = cmd.StringOpt("l language", "en", "language")
   154  		)
   155  
   156  		cmd.Action = func() {
   157  			setDefaultLangSet(*lang)
   158  			buildProject(*config)
   159  		}
   160  
   161  		cmd.Command("web", "generate a template project", func(cmd *cli.Cmd) {
   162  			var (
   163  				lang = cmd.StringOpt("l language", "en", "language")
   164  				port = cmd.StringOpt("p port", "6633", "port")
   165  			)
   166  
   167  			cmd.Action = func() {
   168  				setDefaultLangSet(*lang)
   169  				buildProjectWeb(*port)
   170  			}
   171  		})
   172  	})
   173  
   174  	app.Command("add", "generate user/permission/roles", func(cmd *cli.Cmd) {
   175  
   176  		cmd.Command("user", "generate users", func(cmd *cli.Cmd) {
   177  			var (
   178  				config = cmd.StringOpt("c config", "", "config ini path")
   179  				lang   = cmd.StringOpt("l language", "en", "language")
   180  			)
   181  
   182  			cmd.Action = func() {
   183  				setDefaultLangSet(*lang)
   184  				addUser(*config)
   185  			}
   186  		})
   187  
   188  		cmd.Command("permission", "generate permissions of table", func(cmd *cli.Cmd) {
   189  			var (
   190  				config = cmd.StringOpt("c config", "", "config ini path")
   191  				lang   = cmd.StringOpt("l language", "en", "language")
   192  			)
   193  
   194  			cmd.Action = func() {
   195  				setDefaultLangSet(*lang)
   196  				addPermission(*config)
   197  			}
   198  		})
   199  	})
   200  
   201  	_ = app.Run(os.Args)
   202  }