-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from lazygophers/luoxin
生成入口文件
- Loading branch information
Showing
13 changed files
with
154 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package cli | ||
|
||
import ( | ||
"github.com/lazygophers/codegen/codegen" | ||
"github.com/lazygophers/log" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var genCmdCmd = &cobra.Command{ | ||
Use: "cmd", | ||
Short: "Generates cmd folder", | ||
RunE: runGenCmd, | ||
} | ||
|
||
func runGenCmd(cmd *cobra.Command, args []string) (err error) { | ||
err = codegen.GenerateCmd(pb) | ||
if err != nil { | ||
log.Errorf("err:%v", err) | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func init() { | ||
genCmd.AddCommand(genCmdCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ import ( | |
|
||
var GenStateHook = []GenHook{ | ||
ranGenConf, | ||
ranGenCache, | ||
runGenTable, | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package codegen | ||
|
||
import ( | ||
"github.com/lazygophers/codegen/state" | ||
"github.com/lazygophers/log" | ||
"github.com/lazygophers/utils/osx" | ||
"github.com/pterm/pterm" | ||
"io/fs" | ||
"os" | ||
) | ||
|
||
func initCmdDirectory(pb *PbPackage) error { | ||
if osx.IsDir(GetPath(PathTypeCmd, pb)) { | ||
return nil | ||
} | ||
|
||
err := os.MkdirAll(GetPath(PathTypeCmd, pb), fs.ModePerm) | ||
if err != nil { | ||
log.Errorf("err:%s", err) | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func GenerateCmd(pb *PbPackage) (err error) { | ||
err = initCmdDirectory(pb) | ||
if err != nil { | ||
log.Errorf("err:%v", err) | ||
return err | ||
} | ||
|
||
// cache 文件为覆盖生成单次生成,不会重复读取 | ||
if osx.IsFile(GetPath(PathTypeCmdMain, pb)) { | ||
if !state.Config.Overwrite { | ||
pterm.Warning.Printfln("main is already existing, skip generation") | ||
return nil | ||
} | ||
|
||
pterm.Warning.Printfln("main is already existing, will overwrite") | ||
} | ||
|
||
// table 文件为覆盖生成 | ||
args := map[string]interface{}{ | ||
"PB": pb, | ||
} | ||
|
||
tpl, err := GetTemplate(TemplateTypeCmd) | ||
if err != nil { | ||
log.Errorf("err:%v", err) | ||
return err | ||
} | ||
|
||
file, err := os.OpenFile(GetPath(PathTypeCmdMain, pb), os.O_CREATE|os.O_WRONLY|os.O_TRUNC, fs.FileMode(0666)) | ||
if err != nil { | ||
log.Errorf("err:%v", err) | ||
return err | ||
} | ||
defer file.Close() | ||
|
||
err = tpl.Execute(file, args) | ||
if err != nil { | ||
log.Errorf("err:%v", err) | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/lazygophers/{{ .PB.GoPackageName }}" | ||
"github.com/lazygophers/{{ .PB.GoPackageName }}/internal/state" | ||
"github.com/lazygophers/log" | ||
"github.com/lazygophers/lrpc" | ||
) | ||
|
||
func main() { | ||
err := state.Load() | ||
if err != nil { | ||
log.Errorf("err:%v", err) | ||
return | ||
} | ||
|
||
app := lrpc.NewApp(&lrpc.Config{ | ||
Name: state.State.Config.Name, | ||
}) | ||
|
||
app.AddRoutes({{ .PB.GoPackageName }}.Routes) | ||
|
||
err = app.ListenAndServe(state.State.Config.Port) | ||
if err != nil { | ||
log.Errorf("err:%v", err) | ||
return | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters