Skip to content

Commit ef46d00

Browse files
authored
Merge pull request #4 from sliveryou/feat-go1.16
feat: upgrade go version to 1.16
2 parents 6e37f25 + 99922c6 commit ef46d00

26 files changed

+656
-421
lines changed

.gitignore

Lines changed: 163 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
# Created by https://www.toptal.com/developers/gitignore/api/go,jetbrains+all,macos,windows
2+
# Edit at https://www.toptal.com/developers/gitignore?templates=go,jetbrains+all,macos,windows
3+
4+
### Go ###
5+
# If you prefer the allow list template instead of the deny list, see community template:
6+
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
7+
#
18
# Binaries for programs and plugins
29
*.exe
310
*.exe~
@@ -13,5 +20,159 @@
1320

1421
# Dependency directories (remove the comment below to include it)
1522
# vendor/
16-
.idea/
17-
grom*
23+
24+
# Go workspace file
25+
go.work
26+
27+
### JetBrains+all ###
28+
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
29+
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
30+
31+
# User-specific stuff
32+
.idea/**/workspace.xml
33+
.idea/**/tasks.xml
34+
.idea/**/usage.statistics.xml
35+
.idea/**/dictionaries
36+
.idea/**/shelf
37+
38+
# AWS User-specific
39+
.idea/**/aws.xml
40+
41+
# Generated files
42+
.idea/**/contentModel.xml
43+
44+
# Sensitive or high-churn files
45+
.idea/**/dataSources/
46+
.idea/**/dataSources.ids
47+
.idea/**/dataSources.local.xml
48+
.idea/**/sqlDataSources.xml
49+
.idea/**/dynamic.xml
50+
.idea/**/uiDesigner.xml
51+
.idea/**/dbnavigator.xml
52+
53+
# Gradle
54+
.idea/**/gradle.xml
55+
.idea/**/libraries
56+
57+
# Gradle and Maven with auto-import
58+
# When using Gradle or Maven with auto-import, you should exclude module files,
59+
# since they will be recreated, and may cause churn. Uncomment if using
60+
# auto-import.
61+
# .idea/artifacts
62+
# .idea/compiler.xml
63+
# .idea/jarRepositories.xml
64+
# .idea/modules.xml
65+
# .idea/*.iml
66+
# .idea/modules
67+
# *.iml
68+
# *.ipr
69+
70+
# CMake
71+
cmake-build-*/
72+
73+
# Mongo Explorer plugin
74+
.idea/**/mongoSettings.xml
75+
76+
# File-based project format
77+
*.iws
78+
79+
# IntelliJ
80+
out/
81+
82+
# mpeltonen/sbt-idea plugin
83+
.idea_modules/
84+
85+
# JIRA plugin
86+
atlassian-ide-plugin.xml
87+
88+
# Cursive Clojure plugin
89+
.idea/replstate.xml
90+
91+
# SonarLint plugin
92+
.idea/sonarlint/
93+
94+
# Crashlytics plugin (for Android Studio and IntelliJ)
95+
com_crashlytics_export_strings.xml
96+
crashlytics.properties
97+
crashlytics-build.properties
98+
fabric.properties
99+
100+
# Editor-based Rest Client
101+
.idea/httpRequests
102+
103+
# Android studio 3.1+ serialized cache file
104+
.idea/caches/build_file_checksums.ser
105+
106+
### JetBrains+all Patch ###
107+
# Ignore everything but code style settings and run configurations
108+
# that are supposed to be shared within teams.
109+
110+
.idea/*
111+
112+
!.idea/codeStyles
113+
!.idea/runConfigurations
114+
115+
### macOS ###
116+
# General
117+
.DS_Store
118+
.AppleDouble
119+
.LSOverride
120+
121+
# Icon must end with two \r
122+
Icon
123+
124+
125+
# Thumbnails
126+
._*
127+
128+
# Files that might appear in the root of a volume
129+
.DocumentRevisions-V100
130+
.fseventsd
131+
.Spotlight-V100
132+
.TemporaryItems
133+
.Trashes
134+
.VolumeIcon.icns
135+
.com.apple.timemachine.donotpresent
136+
137+
# Directories potentially created on remote AFP share
138+
.AppleDB
139+
.AppleDesktop
140+
Network Trash Folder
141+
Temporary Items
142+
.apdisk
143+
144+
### macOS Patch ###
145+
# iCloud generated files
146+
*.icloud
147+
148+
### Windows ###
149+
# Windows thumbnail cache files
150+
Thumbs.db
151+
Thumbs.db:encryptable
152+
ehthumbs.db
153+
ehthumbs_vista.db
154+
155+
# Dump file
156+
*.stackdump
157+
158+
# Folder config file
159+
[Dd]esktop.ini
160+
161+
# Recycle Bin used on file shares
162+
$RECYCLE.BIN/
163+
164+
# Windows Installer files
165+
*.cab
166+
*.msi
167+
*.msix
168+
*.msm
169+
*.msp
170+
171+
# Windows shortcuts
172+
*.lnk
173+
174+
# End of https://www.toptal.com/developers/gitignore/api/go,jetbrains+all,macos,windows
175+
176+
### Project ###
177+
testdata/
178+
/grom*

Makefile

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
.PHONY: build install pkg clean fmt proxy
2+
13
build:
24
@sh scripts/build.sh
35

@@ -11,17 +13,10 @@ clean:
1113
@sh scripts/clean.sh
1214

1315
fmt:
14-
@find . -name '*.go' -not -path "./vendor/*" | xargs gofmt -s -w
15-
@find . -name '*.go' -not -path "./vendor/*" | xargs goimports -w
16-
@find . -name '*.sh' -not -path "./vendor/*" | xargs shfmt -w -s -i 4 -ci -bn
16+
@find . -name '*.go' -not -path "./vendor/*" | xargs gofumpt -w -extra
17+
@find . -name '*.go' -not -path "./vendor/*" | xargs -n 1 -t goimports-reviser -rm-unused -set-alias -company-prefixes "github.com/sliveryou" -project-name "github.com/sliveryou/grom"
18+
@find . -name '*.sh' -not -path "./vendor/*" | xargs shfmt -w -s -i 2 -ci -bn -sr
1719

1820
proxy:
1921
@go env -w GO111MODULE="on"
2022
@go env -w GOPROXY="https://goproxy.cn,direct"
21-
22-
dep:
23-
@go get github.com/golangci/golangci-lint/cmd/golangci-lint@v1.27.0
24-
@go get golang.org/x/tools/cmd/goimports
25-
@go get mvdan.cc/sh/v3/cmd/shfmt
26-
@go get mvdan.cc/sh/v3/cmd/gosh
27-
@git checkout -- go.mod go.sum

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,15 @@ Download package by using:
1818

1919
```shell script
2020
$ go get -u github.com/sliveryou/grom
21+
22+
# if the go version is before 1.16, use the following command to install:
23+
$ GO111MODULE=on go get -u github.com/sliveryou/grom@latest
24+
25+
# if the go version is 1.16 and later, use the following command to install:
26+
$ GO111MODULE=on go install github.com/sliveryou/grom@latest
2127
```
2228

23-
To build from source code, you need [Go environment](https://golang.org/dl/) (1.14 or newer) and use the following commands:
29+
To build from source code, you need [Go environment](https://golang.org/dl/) (1.16 or newer) and use the following commands:
2430

2531
```shell script
2632
$ git clone https://github.com/sliveryou/grom.git

README_zh-CN.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,14 @@ Grom 是一个可以将 mysql 的表字段转换为 golang 的模型结构的命
1616
使用如下命令下载并安装包:
1717

1818
```shell script
19-
$ go get -u github.com/sliveryou/grom
19+
# 如果 go 版本在 1.16 以前,使用如下命令安装:
20+
$ GO111MODULE=on go get -u github.com/sliveryou/grom@latest
21+
22+
# 如果 go 版本在 1.16 及以后,使用如下命令安装:
23+
$ GO111MODULE=on go install github.com/sliveryou/grom@latest
2024
```
2125

22-
如果要从源码开始构建的话,需要有 [Go](https://golang.org/dl/) 环境(1.14 及以上版本),并使用如下命令:
26+
如果要从源码开始构建的话,需要有 [Go](https://golang.org/dl/) 环境(1.16 及以上版本),并使用如下命令:
2327

2428
```shell script
2529
$ git clone https://github.com/sliveryou/grom.git

cmd/convert.go

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@ package cmd
22

33
import (
44
"encoding/json"
5-
"errors"
65
"fmt"
7-
"io/ioutil"
86
"os"
97
"strings"
108

9+
"github.com/pkg/errors"
1110
"github.com/spf13/cobra"
1211

1312
"github.com/sliveryou/grom/util"
@@ -47,7 +46,7 @@ var convertCmd = &cobra.Command{
4746
Long: "Convert mysql table fields to golang model structure by information_schema.columns and information_schema.statistics",
4847
Example: " grom convert -n ./grom.json\n" +
4948
" grom convert -H localhost -P 3306 -u user -p password -d database -t table -e INITIALISM,FIELD_COMMENT,JSON_TAG,GORM_V2_TAG --package PACKAGE_NAME --struct STRUCT_NAME",
50-
Run: convertFunc,
49+
RunE: convertFunc,
5150
}
5251

5352
func init() {
@@ -66,56 +65,49 @@ func init() {
6665
rootCmd.AddCommand(convertCmd)
6766
}
6867

69-
func convertFunc(cmd *cobra.Command, args []string) {
68+
func convertFunc(_ *cobra.Command, _ []string) error {
7069
config, err := getCmdConfig()
7170
if err != nil {
72-
return
71+
return errors.WithMessage(err, "getCmdConfig err")
7372
}
7473

75-
out, err := util.ConvertTable(config)
74+
out, err := util.ConvertTable(*config)
7675
if err != nil {
77-
return
76+
return errors.WithMessage(err, "util.ConvertTable err")
7877
}
7978

8079
if outputFilePath != "" {
81-
saveOutputToFile(out)
80+
return saveOutputToFile(out)
8281
} else {
8382
fmt.Println(out)
8483
}
84+
85+
return nil
8586
}
8687

87-
func saveOutputToFile(out string) {
88-
f, err := os.Create(outputFilePath)
88+
func saveOutputToFile(out string) error {
89+
err := os.WriteFile(outputFilePath, []byte(out), 0o666)
8990
if err != nil {
90-
fmt.Println(err)
91-
return
91+
return errors.WithMessage(err, "os.WriteFile err")
9292
}
93-
defer f.Close()
9493

95-
_, err = f.Write([]byte(out))
96-
if err != nil {
97-
fmt.Println(err)
98-
return
99-
}
94+
fmt.Println("write output in:", outputFilePath)
10095

101-
fmt.Println(out)
102-
fmt.Println("\nwrite output in:", outputFilePath)
96+
return nil
10397
}
10498

105-
func getCmdConfig() (util.CMDConfig, error) {
99+
func getCmdConfig() (*util.CMDConfig, error) {
106100
config := util.CMDConfig{}
107101

108102
if filePath != "" {
109-
content, err := ioutil.ReadFile(filePath)
103+
content, err := os.ReadFile(filePath)
110104
if err != nil {
111-
fmt.Println(err)
112-
return util.CMDConfig{}, err
105+
return nil, errors.WithMessage(err, "os.ReadFile err")
113106
}
114107

115108
err = json.Unmarshal(content, &config)
116109
if err != nil {
117-
fmt.Println(err)
118-
return util.CMDConfig{}, err
110+
return nil, errors.WithMessage(err, "json.Unmarshal err")
119111
}
120112
}
121113

@@ -148,9 +140,7 @@ func getCmdConfig() (util.CMDConfig, error) {
148140
for _, v := range enable {
149141
service := strings.ToUpper(v)
150142
if _, ok := validServices[service]; !ok {
151-
err := errors.New("enabled service is invalid, service: " + service)
152-
fmt.Println(err)
153-
return util.CMDConfig{}, err
143+
return nil, errors.New("enabled service is invalid, service: " + service)
154144
}
155145

156146
switch service {
@@ -180,5 +170,5 @@ func getCmdConfig() (util.CMDConfig, error) {
180170
}
181171
}
182172

183-
return config, nil
173+
return &config, nil
184174
}

cmd/generate.go

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"os"
77

8+
"github.com/pkg/errors"
89
"github.com/spf13/cobra"
910

1011
"github.com/sliveryou/grom/util"
@@ -20,30 +21,23 @@ var generateCmd = &cobra.Command{
2021
Short: "Generate grom configuration file",
2122
Long: fmt.Sprintf("Generate grom configuration file like this:\n%s", generateFileInfo()),
2223
Example: " grom generate -n ./grom.json",
23-
Run: generateFunc,
24+
RunE: generateFunc,
2425
}
2526

2627
func init() {
2728
generateCmd.Flags().StringVarP(&fileName, "name", "n", "grom.json", "the name of the generated grom configuration file")
2829
rootCmd.AddCommand(generateCmd)
2930
}
3031

31-
func generateFunc(cmd *cobra.Command, args []string) {
32-
f, err := os.Create(fileName)
32+
func generateFunc(_ *cobra.Command, _ []string) error {
33+
err := os.WriteFile(fileName, []byte(fileInfo), 0o666)
3334
if err != nil {
34-
fmt.Println(err)
35-
return
35+
return errors.WithMessage(err, "os.WriteFile err")
3636
}
37-
defer f.Close()
3837

39-
_, err = f.Write([]byte(fileInfo))
40-
if err != nil {
41-
fmt.Println(err)
42-
return
43-
}
38+
fmt.Println("write in:", fileName)
4439

45-
fmt.Println(fileInfo)
46-
fmt.Println("\nwrite in:", fileName)
40+
return nil
4741
}
4842

4943
func generateFileInfo() string {

0 commit comments

Comments
 (0)