Skip to content

Commit

Permalink
Fsync (#8)
Browse files Browse the repository at this point in the history
- 新增目录文件同步子命令fsync
- 简化README.md描述内容,详细使用说明,迁移到wiki中
- 命令提示中增加Example提示,交互更友好
- 优化原有功能的实现方式,如#6;使用format.Source格式化go代码;
  • Loading branch information
yesAnd92 authored Sep 9, 2023
1 parent 5b1edaa commit e91f7e4
Show file tree
Hide file tree
Showing 19 changed files with 504 additions and 351 deletions.
341 changes: 97 additions & 244 deletions README.md

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions cmd/es.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ const (
var (
fmtPretty bool
esCmd = &cobra.Command{
Use: "es",
Short: "Translate SQL to elasticsearch's DSL",
Long: `Translate SQL to elasticsearch's DSL`,
Args: cobra.MatchAll(cobra.ExactArgs(1)),
Use: `es`,
Short: `Translate SQL to elasticsearch's DSL`,
Long: `Translate SQL to elasticsearch's DSL`,
Example: `lwe es 'select * from user where age >18' [-p=true]`,
Args: cobra.MatchAll(cobra.ExactArgs(1)),
Run: func(cmd *cobra.Command, args []string) {
sql := args[0]
//使用sqlparse对SQL进行解析
Expand Down
21 changes: 11 additions & 10 deletions cmd/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ var (
target string
author string
sqlCmd = &cobra.Command{
Use: "fmt",
Short: "Generate the specified file based on SQL",
Long: `Generate the specified file based on SQL. Such as Java Entity,Go struct and so on`,
Args: cobra.MatchAll(cobra.ExactArgs(1)),
Use: `fmt`,
Short: `Generate the specified file based on SQL`,
Long: `Generate the specified file based on SQL. Such as Java Entity,Go struct and so on`,
Example: `lwe fmt sql-file-path [-t=java|go|json] [-a=yesAnd]`,
Args: cobra.MatchAll(cobra.ExactArgs(1)),
Run: func(cmd *cobra.Command, args []string) {
sqlFilePath := args[0]
sqlCtxList, err := DataCleansing(sqlFilePath)
Expand All @@ -48,12 +49,6 @@ var (
}
)

func init() {

sqlCmd.PersistentFlags().StringVarP(&target, "target", "t", "java", "The type[java|json|go] of generate the sql")
sqlCmd.PersistentFlags().StringVarP(&author, "author", "a", "", "Comment for author information will be added to the generated file")
}

func GetParser(target string) (sql.IParseDDL, error) {
var handle sql.IParseDDL
switch target {
Expand Down Expand Up @@ -105,3 +100,9 @@ func DataCleansing(sqlFilePath string) (ctxList []string, err error) {
}
return sqlCtxArr, nil
}

func init() {

sqlCmd.PersistentFlags().StringVarP(&target, "target", "t", "java", "The type[java|json|go] of generate the sql")
sqlCmd.PersistentFlags().StringVarP(&author, "author", "a", "", "Comment for author information will be added to the generated file")
}
40 changes: 40 additions & 0 deletions cmd/fsync.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package cmd

import (
"github.com/spf13/cobra"
"github.com/yesAnd92/lwe/handler/sync"
)

var (
dryrRun bool

fsyncCmd = &cobra.Command{
Use: `fsync`,
Short: `Sync file from source dir to target dir`,
Long: `Sync file from source dir to target dir,and it will skip existing files`,
Example: `lwe fsync sourceDir targetDir [-d=true]`,
Args: cobra.MatchAll(cobra.ExactArgs(2)),
Run: func(cmd *cobra.Command, args []string) {
sourceDir := args[0]
targetDir := args[1]

var thenDo sync.CompareThenDoIfa = &sync.CopyCompareThenDo{}
if dryrRun {
thenDo = &sync.DisplayCompareThenDo{}
}
fsync := sync.InitFsync(sourceDir, targetDir)

//compare source and target dir diff
fsync.DiffDir()

fsync.Sync(thenDo)
},
}
)

func init() {

//dry-run
fsyncCmd.PersistentFlags().BoolVarP(&dryrRun, "dry-run", "d", false, "Because fsync can make some significant changes, you might prefer to add --dry-run=true option"+
" to the command line to preview what fsync plans to do")
}
36 changes: 20 additions & 16 deletions cmd/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ var (
token string //克隆所需要的token

glogCmd = &cobra.Command{
Use: "glog",
Short: "Get all git repository commit log under the given dir ",
Long: `Get all git repository commit log under the given dir ,and specify author,date etc. supported!`,
Args: cobra.MatchAll(),
Use: `glog`,
Short: `Get all git repository commit log under the given dir `,
Long: `Get all git repository commit log under the given dir ,and specify author,date etc. supported!`,
Example: `lwe glog [git repo dir] [-a=yesAnd] [-n=50] [-s=2023-08-04] [-e=2023-08-04]`,
Args: cobra.MatchAll(),
Run: func(cmd *cobra.Command, args []string) {

var dir = "."
Expand All @@ -50,10 +51,11 @@ var (
}

glCmd = &cobra.Command{
Use: "gl",
Short: "Update all git repository under the given dir ",
Long: `Update all git repository under the given dir ,the repository that has modified files will not be updated!`,
Args: cobra.MatchAll(cobra.MinimumNArgs(0)),
Use: `gl`,
Short: `Update all git repository under the given dir `,
Long: `Update all git repository under the given dir ,the repository that has modified files will not be updated!`,
Example: `lwe gl [git repo dir]`,
Args: cobra.MatchAll(cobra.MinimumNArgs(0)),
Run: func(cmd *cobra.Command, args []string) {

var dir = "."
Expand All @@ -67,10 +69,11 @@ var (
}

gclCmd = &cobra.Command{
Use: "gcl",
Short: "Update all git repository under the given dir ",
Long: `Update all git repository under the given dir ,the repository that has modified files will not be updated!`,
Args: cobra.MatchAll(cobra.MinimumNArgs(1)),
Use: `gcl`,
Short: `Git clone all git repository under the given git group `,
Long: `Git clone all git repository under the given git group `,
Example: `lwe gcl gitGroupUrl [dir for this git group] -t=yourToken`,
Args: cobra.MatchAll(cobra.MinimumNArgs(1)),
Run: func(cmd *cobra.Command, args []string) {
if len(token) == 0 {
cobra.CheckErr("please confirm token is not empty!")
Expand All @@ -86,10 +89,11 @@ var (
}

gstCmd = &cobra.Command{
Use: "gst",
Short: "Get all git repository status under the given dir ",
Long: `Get all git repository status under the given dir `,
Args: cobra.MatchAll(cobra.MinimumNArgs(0)),
Use: `gst`,
Short: `Get all git repository status under the given dir `,
Long: `Get all git repository status under the given dir `,
Example: `lwe gst [your git repo dir]`,
Args: cobra.MatchAll(cobra.MinimumNArgs(0)),
Run: func(cmd *cobra.Command, args []string) {

var dir = "."
Expand Down
9 changes: 5 additions & 4 deletions cmd/md5.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import (

var (
md5Cmd = &cobra.Command{
Use: "md5",
Short: "Get a md5 for the given value or a random md5 value",
Long: `Get a md5 for the given value. If not specify value ,it will give a random md5 value`,
Args: cobra.MatchAll(cobra.ExactArgs(1)),
Use: `md5`,
Short: `Get a md5 for the given value or a random md5 value`,
Long: `Get a md5 for the given value. If not specify value ,it will give a random md5 value`,
Example: `lwe md5 yourValue`,
Args: cobra.MatchAll(cobra.ExactArgs(1)),
Run: func(cmd *cobra.Command, args []string) {
specifyValue := args[0]
sum := md5.Sum([]byte(specifyValue))
Expand Down
9 changes: 5 additions & 4 deletions cmd/navicat.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ import (
*/
var (
navicatCmd = &cobra.Command{
Use: "ncx",
Short: "Decrypt password of connection in .ncx file",
Long: `The config exported from Navicat is encrypted,ncx command can decrypt it`,
Args: cobra.MatchAll(cobra.ExactArgs(1)),
Use: `ncx`,
Short: `Decrypt password of connection in .ncx file`,
Long: `The config exported from Navicat is encrypted,ncx command can decrypt it`,
Example: `lwe ncx ncx-file-path `,
Args: cobra.MatchAll(cobra.ExactArgs(1)),
Run: func(cmd *cobra.Command, args []string) {
ncxFilePath := args[0]
data, dataErr := getNcxData(ncxFilePath)
Expand Down
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@ func init() {
rootCmd.AddCommand(glCmd)
rootCmd.AddCommand(gclCmd)
rootCmd.AddCommand(gstCmd)
rootCmd.AddCommand(fsyncCmd)

}
9 changes: 5 additions & 4 deletions cmd/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import "github.com/spf13/cobra"
import "github.com/yesAnd92/lwe/handler/url"

var urlCmd = &cobra.Command{
Use: "url",
Short: "format request url to increase readability",
Long: `format request url to increase readability`,
Args: cobra.MatchAll(cobra.ExactArgs(1)),
Use: `url`,
Short: `Format request url to increase readability`,
Long: `Format request url to increase readability`,
Example: `lwe url yourUrl`,
Args: cobra.MatchAll(cobra.ExactArgs(1)),
Run: func(cmd *cobra.Command, args []string) {
url.HandleUrlPathParams(args[0])
},
Expand Down
2 changes: 1 addition & 1 deletion cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ var versionCmd = &cobra.Command{
Short: "Print the version number of lwe",
Long: `All software has versions. This is lwe's`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("v.0.0.3.beta")
fmt.Println("v.1.0.0.beta")
},
}
13 changes: 6 additions & 7 deletions handler/sql/render_gostruct.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package sql
import (
"bytes"
"fmt"
golang "github.com/yesAnd92/lwe/templates/golang"
"github.com/yesAnd92/lwe/templates"
"github.com/yesAnd92/lwe/utils"
"go/format"
"log"
"os"
"os/exec"
"path/filepath"
"text/template"
)
Expand All @@ -19,7 +19,7 @@ type GoStructRenderData struct {

func NewGoStructRenderData() *GoStructRenderData {
//加载实体对应的模板
goStructTpl := golang.InitGoStructTpl()
goStructTpl := templates.InitGoStructTpl()
return &GoStructRenderData{
goStructTpl: goStructTpl,
}
Expand Down Expand Up @@ -66,9 +66,8 @@ func (g *GoStructRenderData) RenderData(objInfos []*ObjInfo) {
log.Println("Create go file err", err)
return
}
f.Write(bf.Bytes())
//按照go的风格进行格式化
fmtBfBytes, e := format.Source(bf.Bytes())

//使用go提供的fmt命令对生成的文件进行格式化
cmd := exec.Command("go", "fmt", fileName)
cmd.Run()
f.Write(fmtBfBytes)
}
4 changes: 2 additions & 2 deletions handler/sql/render_javabean.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package sql

import (
"fmt"
"github.com/yesAnd92/lwe/templates/java"
"github.com/yesAnd92/lwe/templates"
"github.com/yesAnd92/lwe/utils"
"log"
"os"
Expand All @@ -17,7 +17,7 @@ type JavaRenderData struct {

func NewJavaRenderData() *JavaRenderData {
//加载实体对应的模板
javaTpl := java.InitJavaTpl()
javaTpl := templates.InitJavaTpl()
return &JavaRenderData{
JavaTpl: javaTpl,
}
Expand Down
Loading

0 comments on commit e91f7e4

Please sign in to comment.