Skip to content

Commit 799e589

Browse files
author
Nicola Strappazzon C
committed
refact - issue #8 - use cobra module
1 parent bd3926b commit 799e589

File tree

8 files changed

+108
-158
lines changed

8 files changed

+108
-158
lines changed

command/help/main.go

-52
This file was deleted.

command/main.go

-47
This file was deleted.

command/version/main.go

-3
This file was deleted.

flags/main.go

+7-53
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
package flags
22

33
import (
4-
"flag"
54
"os"
65

7-
"skeely/command/help"
86
"skeely/common"
97
)
108

@@ -14,10 +12,8 @@ type Flags struct {
1412
GitHubPullRequest int
1513
GitHubRepository string
1614
GitHubToken string
17-
Help bool
1815
Ignore string
1916
Path string
20-
Version bool
2117
}
2218

2319
var instance *Flags
@@ -30,53 +26,11 @@ func GetInstance() *Flags {
3026
}
3127

3228
func (f *Flags) Load() {
33-
fFiles := flag.String("files", "", "")
34-
fGitHubComment := flag.Bool("github-comment", false, "")
35-
fGitHubPullRequest := flag.Int("github-pull-request", 0, "")
36-
fGitHubRepository := flag.String("github-repository", "", "")
37-
fGitHubToken := flag.String("github-token", "", "")
38-
fHelp := flag.Bool("help", false, "")
39-
fIgnore := flag.String("ignore", "", "")
40-
flag.Usage = func() { help.Show(1) }
41-
fPath := flag.String("path", "", "")
42-
fVersion := flag.Bool("version", false, "")
43-
flag.Parse()
44-
45-
f.Files = *fFiles
46-
f.GitHubComment = *fGitHubComment
47-
f.GitHubPullRequest = *fGitHubPullRequest
48-
f.GitHubRepository = *fGitHubRepository
49-
f.GitHubToken = *fGitHubToken
50-
f.Help = *fHelp
51-
f.Ignore = *fIgnore
52-
f.Path = *fPath
53-
f.Version = *fVersion
54-
55-
if len(f.Files) == 0 && len(os.Getenv("INPUT_FILES")) > 0 {
56-
f.Files = os.Getenv("INPUT_FILES")
57-
}
58-
59-
if len(f.Ignore) == 0 && len(os.Getenv("INPUT_IGNORE")) > 0 {
60-
f.Ignore = os.Getenv("INPUT_IGNORE")
61-
}
62-
63-
if len(f.Path) == 0 && len(os.Getenv("INPUT_PATH")) > 0 {
64-
f.Path = os.Getenv("")
65-
}
66-
67-
if f.GitHubComment == false && common.StringToBool(os.Getenv("INPUT_COMMENT")) == true {
68-
f.GitHubComment = true
69-
}
70-
71-
if f.GitHubPullRequest == 0 && common.StringToInt(os.Getenv("INPUT_PULLREQUEST")) > 0 {
72-
f.GitHubPullRequest = common.StringToInt(os.Getenv("INPUT_PULLREQUEST"))
73-
}
74-
75-
if len(f.GitHubRepository) == 0 && len(os.Getenv("INPUT_REPOSITORY")) > 0 {
76-
f.GitHubRepository = os.Getenv("INPUT_REPOSITORY")
77-
}
78-
79-
if len(f.GitHubToken) == 0 && len(os.Getenv("INPUT_TOKEN")) > 0 {
80-
f.GitHubToken = os.Getenv("INPUT_TOKEN")
81-
}
29+
f.Files = os.Getenv("INPUT_FILES")
30+
f.GitHubComment = common.StringToBool(os.Getenv("INPUT_COMMENT"))
31+
f.GitHubPullRequest = common.StringToInt(os.Getenv("INPUT_PULLREQUEST"))
32+
f.GitHubRepository = os.Getenv("INPUT_REPOSITORY")
33+
f.GitHubToken = os.Getenv("INPUT_TOKEN")
34+
f.Ignore = os.Getenv("INPUT_IGNORE")
35+
f.Path = os.Getenv("INPUT_PATH")
8236
}

go.mod

+6-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,9 @@ go 1.21
44

55
require gopkg.in/yaml.v3 v3.0.1
66

7-
require gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
7+
require (
8+
github.com/inconshreveable/mousetrap v1.1.0 // indirect
9+
github.com/spf13/cobra v1.8.0 // indirect
10+
github.com/spf13/pflag v1.0.5 // indirect
11+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
12+
)

go.sum

+8
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
1+
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
2+
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
3+
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
14
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
25
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
36
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
47
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
58
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
9+
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
10+
github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
11+
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
12+
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
13+
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
614
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
715
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
816
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=

main.go

+64-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,71 @@
11
package main
22

33
import (
4-
"skeely/command"
4+
"fmt"
5+
"os"
6+
7+
"skeely/common/github"
8+
"skeely/common/terminal"
9+
"skeely/config"
10+
"skeely/flags"
11+
"skeely/linter"
12+
"skeely/version"
13+
14+
"github.com/spf13/cobra"
515
)
616

717
func main() {
8-
command.Run()
18+
var f = flags.GetInstance()
19+
var rootCmd = &cobra.Command{
20+
Use: "skeely [COMMANDS] [OPTIONS]",
21+
Long: `skeely is a schema linter for MySQL, this tool help to identifying
22+
some common and uncommon mistakes on data model.
23+
24+
For more help, plese visit: https://github.com/debeando/skeely`,
25+
Example: `
26+
# Lint directory
27+
$ skeely --path=assets/examples/
28+
29+
# Lint specific file
30+
$ skeely --files=assets/examples/case01.sql
31+
32+
# Lint specific file and ignore codes
33+
$ skeely --files=assets/examples/case01.sql --ignore=103,104,305,406
34+
35+
# Lint and push summary as comment into GitHub Pull Request.
36+
$ skeely --path=assets/examples/case01.sql \
37+
--github-comment \
38+
--github-token=${{github.token}} \
39+
--github-repository=$GITHUB_REPOSITORY \
40+
--github-pull-request=${{github.event.pull_request.number}}
41+
`,
42+
Run: func(cmd *cobra.Command, args []string) {
43+
gh := github.GitHub{}
44+
c := config.GetInstance()
45+
46+
if err := c.Load(); err != nil {
47+
fmt.Println(err)
48+
os.Exit(2)
49+
}
50+
51+
msgPlugins := linter.Run()
52+
53+
gh.Comment(msgPlugins)
54+
gh.Push()
55+
56+
terminal.Print(msgPlugins)
57+
},
58+
}
59+
60+
f.Load()
61+
62+
rootCmd.Flags().BoolVarP(&f.GitHubComment, "github-comment", "", false, "Send summary as comment into GitHub.")
63+
rootCmd.Flags().IntVar(&f.GitHubPullRequest, "github-pull-request", 0, "Pull request number.")
64+
rootCmd.Flags().StringVar(&f.Files, "files", "", "List of files to lint, separated by space.")
65+
rootCmd.Flags().StringVar(&f.GitHubRepository, "github-repository", "", "Repository path on github.")
66+
rootCmd.Flags().StringVar(&f.GitHubToken, "github-token", "", "Token to auth in github.")
67+
rootCmd.Flags().StringVar(&f.Ignore, "ignore", "", "List of error codes separated by comma to ignore.")
68+
rootCmd.Flags().StringVar(&f.Path, "path", "", "Path of the directory to start to find *.sql to lint.")
69+
rootCmd.AddCommand(version.NewCommand())
70+
rootCmd.Execute()
971
}

version/main.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package version
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/spf13/cobra"
7+
)
8+
9+
// Version is a const to have the latest version number for this code.
10+
const VERSION string = "0.0.0-beta.3"
11+
12+
func NewCommand() *cobra.Command {
13+
var cmd = &cobra.Command{
14+
Use: "version",
15+
Short: "Print version numbers",
16+
Long: `All software has versions. This is skale`,
17+
Run: func(cmd *cobra.Command, args []string) {
18+
fmt.Println(VERSION)
19+
},
20+
}
21+
22+
return cmd
23+
}

0 commit comments

Comments
 (0)