-
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.
- Loading branch information
Showing
12 changed files
with
3,164 additions
and
7 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,3 @@ | ||
package cmd | ||
|
||
|
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,119 @@ | ||
package cmd | ||
|
||
import ( | ||
"errors" | ||
"html/template" | ||
"io/ioutil" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/go-openapi/spec" | ||
"github.com/go-openapi/swag" | ||
"github.com/gookit/gcli/v2" | ||
"github.com/gookit/goutil/dump" | ||
"github.com/gookit/goutil/fsutil" | ||
"github.com/gookit/ini/v2" | ||
) | ||
|
||
var swag2codeOpts = struct { | ||
tplDir string | ||
SwagFile string | ||
Template string | ||
OutDir string | ||
GroupSuffix string | ||
ActionSuffix string | ||
}{ | ||
tplDir: "resource/templates", | ||
} | ||
|
||
var tplSuffix = ".tpl" | ||
|
||
var tplFuncs = template.FuncMap{ | ||
"join": strings.Join, | ||
} | ||
|
||
var Swag2code = &gcli.Command{ | ||
Name: "swag2code", | ||
UseFor: "generate rux API service codes by swagger.yaml or swagger.json", | ||
Config: func(c *gcli.Command) { | ||
c.StrOpt(&swag2codeOpts.SwagFile, "swagger-file", "f", "./swagger.json", "the swagger doc filepath") | ||
c.StrOpt(&swag2codeOpts.OutDir, "output", "o", "./gocodes", `the output directory for generated codes | ||
if input 'stdout' will print codes on terminal | ||
`) | ||
c.StrVar(&swag2codeOpts.Template, gcli.FlagMeta{ | ||
Name: "template", | ||
Desc: "the template name for generate codes", | ||
Shorts: []string{"t"}, | ||
DefVal: "rux-controller", | ||
}) | ||
c.StrVar(&swag2codeOpts.GroupSuffix, gcli.FlagMeta{ | ||
Name: "group-suffix", | ||
Desc: "Add suffix for group name. eg: API, Controller", | ||
DefVal: "API", | ||
}) | ||
c.StrVar(&swag2codeOpts.ActionSuffix, gcli.FlagMeta{ | ||
Name: "action-suffix", | ||
Desc: "Add suffix for action name. eg: Action, Method", | ||
}) | ||
}, | ||
Func: func(c *gcli.Command, args []string) (err error) { | ||
swagFile := swag2codeOpts.SwagFile | ||
|
||
var bts []byte | ||
if swag.YAMLMatcher(swagFile) { | ||
bts, err = swag.YAMLDoc(swagFile) | ||
} else { // JSON file | ||
bts, err = ioutil.ReadFile(swagFile) | ||
} | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
doc := new(spec.Swagger) | ||
err = doc.UnmarshalJSON(bts) | ||
|
||
if len(doc.SwaggerProps.Paths.Paths) == 0 { | ||
return errors.New("API doc 'paths' is empty") | ||
} | ||
dump.Config(func(d *dump.Dumper) { | ||
d.MaxDepth = 8 | ||
}) | ||
|
||
tplDir := ini.String("swag2code.templateDir") | ||
if tplDir != "" { | ||
swag2codeOpts.tplDir = tplDir | ||
} | ||
|
||
generateByPathItem("/anything", doc.SwaggerProps.Paths.Paths["/anything"]) | ||
|
||
return | ||
}, | ||
} | ||
|
||
func generateByPathItem(path string, pathItem spec.PathItem) { | ||
tmpPath := strings.Trim(path, "/") | ||
group := tmpPath | ||
|
||
var substr string | ||
if strings.ContainsRune(tmpPath, '/') { | ||
nodes := strings.SplitN(tmpPath, "/", 2) | ||
group = nodes[0] | ||
substr = nodes[1] | ||
} | ||
|
||
dump.P(group, substr) | ||
dump.P(pathItem) | ||
|
||
tplFile := swag2codeOpts.Template | ||
if !strings.HasSuffix(tplFile, tplSuffix) { | ||
tplFile += tplSuffix | ||
} | ||
|
||
if !fsutil.IsFile(tplFile) { | ||
tplFile = filepath.Join(swag2codeOpts.tplDir, tplFile) | ||
} | ||
|
||
tpl := template.New("swag2code").Funcs(tplFuncs) | ||
template.Must(tpl.ParseFiles(tplFile)) | ||
} |
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,3 @@ | ||
package cmd | ||
|
||
|
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 |
---|---|---|
@@ -1,15 +1,36 @@ | ||
package main | ||
|
||
import "github.com/gookit/gcli/v2" | ||
import ( | ||
"github.com/gookit/color" | ||
"github.com/gookit/gcli/v2" | ||
"github.com/gookit/ini/v2" | ||
"github.com/gookit/ruxc/cmd" | ||
) | ||
|
||
var configFile string | ||
|
||
func init() { | ||
err := ini.LoadExists("ruxc.ini") | ||
if err != nil { | ||
color.Error.Println("load config error:", err) | ||
} | ||
} | ||
|
||
func main() { | ||
app := gcli.NewApp(func(a *gcli.App) { | ||
a.Name = "Ruxc Application" | ||
a.Description = "CLI tool application for rux" | ||
|
||
}) | ||
app.GOptsBinder = func(gf *gcli.Flags) { | ||
gf.StrOpt(&configFile, "config", "c", "ruxc.ini", "the INI config file for ruxc") | ||
} | ||
|
||
loadCommands(app) | ||
|
||
app.Run() | ||
} | ||
|
||
func loadCommands(app *gcli.App) { | ||
|
||
app.AddCommand(cmd.Swag2code) | ||
} |
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 @@ | ||
/** | ||
* auto generated by https://github.com/gookit/ruxc | ||
* | ||
* @author https://github.com/inhere | ||
*/ | ||
package api | ||
|
||
import ( | ||
"github.com/gookit/rux" | ||
) | ||
|
||
// {{ .GroupName }} {{ .GroupDesc }} | ||
type {{ .GroupName }} struct {} | ||
|
||
// AddRoutes register routes to the router | ||
func (grp *Anything) AddRoutes(r *rux.Router) { | ||
{{range _, $m := .Methods }} | ||
r.{{ $m.METHOD }}("{{ $m.Path }}", grp.{{ $m.MethodName }}) | ||
{{end}} | ||
} | ||
|
||
{{range _, $m := .Methods }} | ||
// {{ $m.MethodName }} {{ $m.MethodDesc }} | ||
func (*{{ .GroupName}}) {{ $m.MethodName }}(c *rux.Context) { | ||
c.Text(200, "hello") | ||
} | ||
{{end}} |
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,5 @@ | ||
|
||
func {{.MethodName}}(c *rux.Context) { | ||
c.Text(200, "hello") | ||
} | ||
|
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,7 @@ | ||
# config file for ruxc | ||
# homepage: https://github.com/gookit/ruxc | ||
# | ||
|
||
[swag2code] | ||
templateDir = resource/templates | ||
|
Oops, something went wrong.