Skip to content

Commit

Permalink
get my files
Browse files Browse the repository at this point in the history
  • Loading branch information
liujianping committed Jan 15, 2018
1 parent 748a08f commit 12cb977
Show file tree
Hide file tree
Showing 120 changed files with 14,651 additions and 7 deletions.
27 changes: 27 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
all:

init:
go get -u github.com/jteeuwen/go-bindata/...

debugTpl:
go-bindata -nometadata -o tpl/bindata.go -ignore bindata.go -pkg tpl -debug tpl

buildTpl:
go-bindata -nometadata -o tpl/bindata.go -ignore bindata.go -pkg tpl tpl

build:
go build

install:
go install

sql:
go install
db-orm sql -i ./example/yaml/ -o ./example/script/

test:
go install
db-orm code -i ./example/yaml/ -o ./example/model/
# go test -v ./... ???
#go test -v ./orm/sqlbuilder
go test -v ./example/model/...
41 changes: 41 additions & 0 deletions cmd/code.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright © 2016 NAME HERE <EMAIL ADDRESS>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// codeCmd represents the code command
var codeCmd = &cobra.Command{
Use: "code",
Short: "generate code files from yaml files",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
GenerateCode()
},
}

func init() {
RootCmd.AddCommand(codeCmd)

codeCmd.PersistentFlags().StringP("package", "p", "", "code file (.go) package name")
codeCmd.PersistentFlags().StringP("input", "i", ".", "directory of yaml files")
codeCmd.PersistentFlags().StringP("model", "m", "", "model need to generate code")
viper.BindPFlag("package", codeCmd.PersistentFlags().Lookup("package"))
viper.BindPFlag("code_input", codeCmd.PersistentFlags().Lookup("input"))
viper.BindPFlag("code_model", codeCmd.PersistentFlags().Lookup("model"))
}
86 changes: 86 additions & 0 deletions cmd/fn.code.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package cmd

import (
"fmt"
"os"
"path"
"path/filepath"
"strings"

"github.com/auto-program/db-orm/fs"
"github.com/auto-program/db-orm/parser"
"github.com/spf13/viper"
)

func GenerateCode() {
packageName := viper.GetString("package")

inputDir, err := filepath.Abs(viper.GetString("code_input"))
if err != nil {
fmt.Println(err)
os.Exit(1)
}

outputDir, err := filepath.Abs(viper.GetString("output"))
if err != nil {
fmt.Println(err)
os.Exit(1)
}
if packageName == "" {
_, packageName = path.Split(outputDir)
}

yamls, err := fs.GetDirectoryFilesBySuffix(inputDir, ".yaml")
if err != nil {
fmt.Println(err)
os.Exit(1)
}

model := viper.GetString("code_model")
metaObjs := map[string]*parser.MetaObject{}
confTpls := map[string]bool{
"orm": true,
}
i := 0
for _, yaml := range yamls {
objs, err := parser.ReadYaml(packageName, yaml)
if err != nil {
fmt.Println("failed: ", err)
os.Exit(1)
}

i = i + 1
if model != "" {
for _, obj := range objs {
if obj.Tag == "" {
obj.Tag = fmt.Sprint(i)
}
if strings.ToLower(obj.Name) == strings.ToLower(model) {
metaObjs[obj.Name] = obj
for _, db := range obj.Dbs {
confTpls[db] = true
}
goto GeneratePoint
}
}
} else {
for _, obj := range objs {
obj.Tag = fmt.Sprint(i)
metaObjs[obj.Name] = obj
for _, db := range obj.Dbs {
confTpls[db] = true
}
}
}
}

GeneratePoint:
for _, metaObj := range metaObjs {
fs.ExecuteMetaObjectCodeTemplate(outputDir, metaObj)
}

for conf := range confTpls {
fs.ExecuteConfigTemplate(outputDir, conf, packageName)
}

}
79 changes: 79 additions & 0 deletions cmd/fn.sql.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package cmd

import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/auto-program/db-orm/fs"
"github.com/auto-program/db-orm/parser"
"github.com/spf13/viper"
)

func GenerateSQL() {
inputDir, err := filepath.Abs(viper.GetString("sql_input"))
if err != nil {
fmt.Println(err)
os.Exit(1)
}

outputDir, err := filepath.Abs(viper.GetString("output"))
if err != nil {
fmt.Println(err)
os.Exit(1)
}

model := viper.GetString("sql_model")
driver := viper.GetString("sql_driver")

yamls, err := fs.GetDirectoryFilesBySuffix(inputDir, ".yaml")
if err != nil {
fmt.Println(err)
os.Exit(1)
}

metaObjs := map[string]*parser.MetaObject{}
confTpls := map[string]bool{
"orm": true,
}
i := 0
for _, yaml := range yamls {
objs, err := parser.ReadYaml("script", yaml)
if err != nil {
fmt.Println("failed: ", err)
os.Exit(1)
}

i = i + 1
if model != "" {
for _, obj := range objs {
if obj.Tag == "" {
obj.Tag = fmt.Sprint(i)
}
if strings.ToLower(obj.Name) == strings.ToLower(model) {
metaObjs[obj.Name] = obj
for _, db := range obj.Dbs {
confTpls[db] = true
}
goto GeneratePoint
}
}
} else {
for _, obj := range objs {
obj.Tag = fmt.Sprint(i)
metaObjs[obj.Name] = obj
for _, db := range obj.Dbs {
confTpls[db] = true
}
}
}
}

GeneratePoint:
for _, metaObj := range metaObjs {
if metaObj.DbSource() != "" {
fs.ExecuteMetaObjectScriptTemplate(outputDir, driver, metaObj)
}
}
}
18 changes: 18 additions & 0 deletions cmd/fn.yaml.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package cmd

import (
"fmt"
"strings"

"github.com/spf13/viper"
)

func GenerateYaml() {
model := viper.GetString("yaml_model")

fmt.Println("generate yaml => ", model)

s := make([]string, 6)
fmt.Println("s =>", strings.Join(s, ","))

}
47 changes: 47 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright © 2016 NAME HERE <EMAIL ADDRESS>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
"fmt"
"os"

"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var cfgFile string

// RootCmd represents the base command when called without any subcommands
var RootCmd = &cobra.Command{
Use: "orm",
Short: "orm fly your program up",
}

// Execute adds all child commands to the root command sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
if err := RootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(-1)
}
}

func init() {
RootCmd.PersistentFlags().StringP("output", "o", ".", "directory of output")
RootCmd.PersistentFlags().BoolP("verbose", "v", false, "verbose message flag")
viper.BindPFlag("output", RootCmd.PersistentFlags().Lookup("output"))
viper.BindPFlag("verbose", RootCmd.PersistentFlags().Lookup("verbose"))
}
40 changes: 40 additions & 0 deletions cmd/sql.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright © 2016 NAME HERE <EMAIL ADDRESS>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// yamlCmd represents the yaml command
var sqlCmd = &cobra.Command{
Use: "sql",
Short: "generate sql script from yaml file",
Run: func(cmd *cobra.Command, args []string) {
GenerateSQL()
},
}

func init() {
RootCmd.AddCommand(sqlCmd)

sqlCmd.PersistentFlags().StringP("input", "i", ".", "directory of yaml files")
sqlCmd.PersistentFlags().StringP("driver", "d", "mysql", "database type")
sqlCmd.PersistentFlags().StringP("model", "m", "", "model need to generate sql script")
viper.BindPFlag("sql_input", sqlCmd.PersistentFlags().Lookup("input"))
viper.BindPFlag("sql_model", sqlCmd.PersistentFlags().Lookup("model"))
viper.BindPFlag("sql_driver", sqlCmd.PersistentFlags().Lookup("driver"))
}
49 changes: 49 additions & 0 deletions cmd/yaml.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright © 2016 NAME HERE <EMAIL ADDRESS>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// yamlCmd represents the yaml command
var yamlCmd = &cobra.Command{
Use: "yaml",
Short: "generate yaml files from database or just sample yaml file",
Run: func(cmd *cobra.Command, args []string) {
GenerateYaml()
},
}

func init() {
RootCmd.AddCommand(yamlCmd)

yamlCmd.PersistentFlags().StringP("model", "m", "", "sample yaml file's model name")
yamlCmd.PersistentFlags().StringP("driver", "D", "mysql", "database driver name, like: mysql, mssql etc")
yamlCmd.PersistentFlags().StringP("database", "d", "", "database name")
yamlCmd.PersistentFlags().StringP("host", "H", "localhost", "database host")
yamlCmd.PersistentFlags().IntP("port", "P", 3306, "database port")
yamlCmd.PersistentFlags().StringP("username", "u", "root", "database username")
yamlCmd.PersistentFlags().StringP("password", "p", "", "database password")

viper.BindPFlag("yaml_model", yamlCmd.PersistentFlags().Lookup("model"))
viper.BindPFlag("yaml_driver", yamlCmd.PersistentFlags().Lookup("driver"))
viper.BindPFlag("yaml_host", yamlCmd.PersistentFlags().Lookup("host"))
viper.BindPFlag("yaml_port", yamlCmd.PersistentFlags().Lookup("port"))
viper.BindPFlag("yaml_database", yamlCmd.PersistentFlags().Lookup("database"))
viper.BindPFlag("yaml_username", yamlCmd.PersistentFlags().Lookup("username"))
viper.BindPFlag("yaml_password", yamlCmd.PersistentFlags().Lookup("password"))
}
Loading

0 comments on commit 12cb977

Please sign in to comment.