This repository has been archived by the owner on Dec 29, 2024. It is now read-only.
-
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 #25 from osspkg/add-app-template
add app generator
- Loading branch information
Showing
22 changed files
with
397 additions
and
61 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
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,19 @@ | ||
module github.com/osspkg/devtool | ||
|
||
go 1.18 | ||
go 1.20 | ||
|
||
require ( | ||
go.osspkg.com/algorithms v1.3.0 | ||
go.osspkg.com/goppy v0.14.0 | ||
golang.org/x/mod v0.14.0 | ||
go.osspkg.com/algorithms v1.3.1 | ||
go.osspkg.com/goppy/console v0.3.1 | ||
go.osspkg.com/goppy/errors v0.3.0 | ||
go.osspkg.com/goppy/iofile v0.3.2 | ||
go.osspkg.com/goppy/shell v0.3.0 | ||
go.osspkg.com/goppy/syscall v0.3.0 | ||
golang.org/x/mod v0.16.0 | ||
gopkg.in/yaml.v3 v3.0.1 | ||
) | ||
|
||
require ( | ||
github.com/kr/text v0.2.0 // indirect | ||
github.com/rogpeppe/go-internal v1.11.0 // indirect | ||
go.osspkg.com/goppy/iosync v0.3.0 // indirect | ||
) |
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,169 @@ | ||
/* | ||
* Copyright (c) 2022-2024 Mikhail Knyazhev <markus621@yandex.ru>. All rights reserved. | ||
* Use of this source code is governed by a BSD 3-Clause license that can be found in the LICENSE file. | ||
*/ | ||
|
||
package appgoppy | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"context" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"text/template" | ||
|
||
"github.com/osspkg/devtool/internal/global" | ||
"go.osspkg.com/goppy/console" | ||
"go.osspkg.com/goppy/iofile" | ||
"go.osspkg.com/goppy/shell" | ||
) | ||
|
||
func Cmd() console.CommandGetter { | ||
return console.NewCommand(func(setter console.CommandSetter) { | ||
setter.Setup("goppy", "Generate new app with goppy sdk") | ||
setter.ExecFunc(func(_ []string) { | ||
currdir := iofile.CurrentDir() | ||
|
||
data := make(map[string]interface{}, 100) | ||
data["go_version"] = strings.TrimLeft(global.GoVersion(), "go") | ||
data["app_module"] = console.Input("Input project name", nil, "app") | ||
data["app_name"] = func() string { | ||
vv := strings.Split(data["app_module"].(string), "/") | ||
return vv[len(vv)-1] | ||
}() | ||
|
||
for _, blocks := range modules { | ||
for _, name := range blocks { | ||
data["mod_"+name] = false | ||
} | ||
} | ||
|
||
userInput("Add modules", modules, "q", func(s string) { | ||
data["mod_"+s] = true | ||
}) | ||
|
||
for _, folder := range folders { | ||
console.FatalIfErr(os.MkdirAll(currdir+"/"+folder, 0744), "Create folder") | ||
} | ||
|
||
for filename, tmpl := range templates { | ||
if strings.Contains(filename, "{{") { | ||
for key, value := range data { | ||
filename = strings.ReplaceAll( | ||
filename, | ||
"{{"+key+"}}", | ||
fmt.Sprintf("%+v", value), | ||
) | ||
} | ||
} | ||
writeFile(currdir+"/"+filename, tmpl, data) | ||
} | ||
|
||
sh := shell.New() | ||
sh.SetDir(currdir) | ||
sh.SetShell("bash") | ||
sh.SetWriter(os.Stdout) | ||
err := sh.CallPackageContext(context.TODO(), | ||
"gofmt -w .", | ||
"go mod tidy", | ||
"devtool setup-lib", | ||
"devtool setup-app", | ||
) | ||
console.FatalIfErr(err, "Call commands") | ||
}) | ||
}) | ||
} | ||
|
||
var modules = [][]string{ | ||
{ | ||
"metrics", | ||
"geoip", | ||
"oauth", | ||
"auth_jwt", | ||
}, | ||
{ | ||
"db_mysql", | ||
"db_sqlite", | ||
"db_postgre", | ||
}, | ||
{ | ||
"web_server", | ||
"web_client", | ||
}, | ||
{ | ||
"websocket_server", | ||
"websocket_client", | ||
}, | ||
{ | ||
"unixsocket_server", | ||
"unixsocket_client", | ||
}, | ||
{ | ||
"dns_server", | ||
"dns_client", | ||
}, | ||
} | ||
|
||
var folders = []string{ | ||
"app", | ||
"config", | ||
"cmd", | ||
} | ||
|
||
var templates = map[string]string{ | ||
".gitignore": tmplGitIgnore, | ||
"README.md": tmplReadMe, | ||
"go.mod": tmplGoMod, | ||
"docker-compose.yaml": tmplDockerFile, | ||
"cmd/{{app_name}}/main.go": tmplMainGO, | ||
"app/plugin.go": tmplAppGo, | ||
} | ||
|
||
func writeFile(filename, t string, data map[string]interface{}) { | ||
tmpl, err := template.New("bt").Parse(t) | ||
console.FatalIfErr(err, "Parse template") | ||
tmpl.Option("missingkey=error") | ||
|
||
var buf bytes.Buffer | ||
err = tmpl.Execute(&buf, data) | ||
console.FatalIfErr(err, "Build template") | ||
|
||
console.FatalIfErr(os.MkdirAll(filepath.Dir(filename), 0744), "Create folder") | ||
console.FatalIfErr(os.WriteFile(filename, buf.Bytes(), 0664), "Write %s", filename) | ||
} | ||
|
||
func userInput(msg string, mods [][]string, exit string, call func(s string)) { | ||
fmt.Printf("--- %s ---\n", msg) | ||
|
||
list := make(map[string]string, len(mods)*4) | ||
i := 0 | ||
for _, blocks := range mods { | ||
for _, name := range blocks { | ||
i++ | ||
fmt.Printf("(%d) %s, ", i, name) | ||
list[fmt.Sprintf("%d", i)] = name | ||
} | ||
fmt.Printf("\n") | ||
} | ||
fmt.Printf("and (%s) Done: \n", exit) | ||
|
||
scan := bufio.NewScanner(os.Stdin) | ||
for { | ||
if scan.Scan() { | ||
r := scan.Text() | ||
if r == exit { | ||
fmt.Printf("\u001B[1A\u001B[K--- Done ---\n\n") | ||
return | ||
} | ||
if name, ok := list[r]; ok { | ||
call(name) | ||
fmt.Printf("\033[1A\033[K + %s\n", name) | ||
continue | ||
} | ||
fmt.Printf("\u001B[1A\u001B[KBad answer! Try again: ") | ||
} | ||
} | ||
} |
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,122 @@ | ||
package appgoppy | ||
|
||
const tmplMainGO = `package main | ||
import ( | ||
app_{{.app_name}} "{{.app_module}}/app" | ||
"go.osspkg.com/goppy" | ||
{{if .mod_metrics}}"go.osspkg.com/goppy/metrics" | ||
{{end}}{{if .mod_geoip}}"go.osspkg.com/goppy/geoip" | ||
{{end}}{{if or .mod_oauth .mod_auth_jwt}}"go.osspkg.com/goppy/auth" | ||
{{end}}{{if .mod_db_mysql}}"go.osspkg.com/goppy/ormmysql" | ||
{{end}}{{if .mod_db_sqlite}}"go.osspkg.com/goppy/ormsqlite" | ||
{{end}}{{if .mod_db_postgre}}"go.osspkg.com/goppy/ormpgsql" | ||
{{end}}{{if or .mod_web_server .mod_web_client}}"go.osspkg.com/goppy/web" | ||
{{end}}{{if or .mod_websocket_server .mod_websocket_client}}"go.osspkg.com/goppy/ws" | ||
{{end}}{{if or .mod_unixsocket_server .mod_unixsocket_client}}"go.osspkg.com/goppy/unixsocket" | ||
{{end}}{{if or .mod_dns_server .mod_dns_client}}"go.osspkg.com/goppy/xdns" | ||
{{end}} | ||
) | ||
var Version = "v0.0.0-dev" | ||
func main() { | ||
app := goppy.New() | ||
app.AppName("{{.app_name}}") | ||
app.AppVersion(Version) | ||
app.Plugins( | ||
{{if .mod_metrics}}metrics.WithMetrics(),{{end}} | ||
{{if .mod_geoip}}geoip.WithMaxMindGeoIP(),{{end}} | ||
{{if .mod_oauth}}auth.WithOAuth(),{{end}} | ||
{{if .mod_auth_jwt}}auth.WithJWT(),{{end}} | ||
{{if .mod_db_mysql}}ormmysql.WithMySQL(),{{end}} | ||
{{if .mod_db_sqlite}}ormsqlite.WithSQLite(),{{end}} | ||
{{if .mod_db_postgre}}ormpgsql.WithPostgreSQL(),{{end}} | ||
{{if .mod_web_server}}web.WithHTTP(),{{end}} | ||
{{if .mod_web_client}}web.WithHTTPClient(),{{end}} | ||
{{if .mod_websocket_server}}ws.WithWebsocketServer(),{{end}} | ||
{{if .mod_websocket_client}}ws.WithWebsocketClient(),{{end}} | ||
{{if .mod_dns_server}}xdns.WithDNSServer(),{{end}} | ||
{{if .mod_dns_client}}xdns.WithDNSClient(),{{end}} | ||
{{if .mod_unixsocket_server}}unixsocket.WithServer(),{{end}} | ||
{{if .mod_unixsocket_client}}unixsocket.WithClient(),{{end}} | ||
) | ||
app.Plugins(app_{{.app_name}}.Plugins...) | ||
app.Run() | ||
} | ||
` | ||
|
||
const tmplAppGo = `package app | ||
import ( | ||
"go.osspkg.com/goppy/plugins" | ||
) | ||
var Plugins = plugins.Inject() | ||
` | ||
|
||
const tmplReadMe = `# {{.app_name}} | ||
` | ||
|
||
const tmplGitIgnore = ` | ||
*.exe | ||
*.exe~ | ||
*.dll | ||
*.so | ||
*.dylib | ||
*.test | ||
*.out | ||
*.dev.yaml | ||
vendor/ | ||
build/ | ||
.idea/ | ||
.vscode/ | ||
.tools/ | ||
` | ||
|
||
const tmplDockerFile = `version: '2.4' | ||
networks: | ||
database: | ||
name: {{.app_name}}-dev-net | ||
services: | ||
db: | ||
image: library/mysql:5.7.25 | ||
restart: on-failure | ||
environment: | ||
MYSQL_ROOT_PASSWORD: 'root' | ||
MYSQL_USER: 'test' | ||
MYSQL_PASSWORD: 'test' | ||
MYSQL_DATABASE: 'test_database' | ||
healthcheck: | ||
test: [ "CMD", "mysql", "--user=root", "--password=root", "-e", "SHOW DATABASES;" ] | ||
interval: 15s | ||
timeout: 30s | ||
retries: 30 | ||
ports: | ||
- "127.0.0.1:3306:3306" | ||
networks: | ||
- database | ||
adminer: | ||
image: adminer:latest | ||
restart: on-failure | ||
links: | ||
- db | ||
ports: | ||
- "127.0.0.1:8000:8080" | ||
networks: | ||
- database | ||
` | ||
|
||
const tmplGoMod = `module {{.app_module}} | ||
go {{.go_version}} | ||
` |
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
Oops, something went wrong.