From 799e5896600d65da85200de2b36eb99d331a5179 Mon Sep 17 00:00:00 2001 From: Nicola Strappazzon C Date: Thu, 15 Feb 2024 13:49:20 +0100 Subject: [PATCH] refact - issue #8 - use cobra module --- command/help/main.go | 52 -------------------------------- command/main.go | 47 ----------------------------- command/version/main.go | 3 -- flags/main.go | 60 +++++-------------------------------- go.mod | 7 ++++- go.sum | 8 +++++ main.go | 66 +++++++++++++++++++++++++++++++++++++++-- version/main.go | 23 ++++++++++++++ 8 files changed, 108 insertions(+), 158 deletions(-) delete mode 100644 command/help/main.go delete mode 100644 command/main.go delete mode 100644 command/version/main.go create mode 100644 version/main.go diff --git a/command/help/main.go b/command/help/main.go deleted file mode 100644 index a60594a..0000000 --- a/command/help/main.go +++ /dev/null @@ -1,52 +0,0 @@ -package help - -import ( - "fmt" - "os" - - "skeely/command/version" -) - -const USAGE = `skeely %s Is a Schema Linter for MySQL, this tool help to identifying -some common and uncommon mistakes on data model. - -USAGE: - skeely [--help | [ --path | --files ] | --version] - -OPTIONS: - --comment Send summary as comment into GitHub. - --files List of files to lint, separated by space. - --git Auto identifying git changed files, require --path option. - --github-pull-request Pull request number. - --github-repository Repository path on github. - --github-token Token to auth in github. - --help Show this help. - --ignore List of codes to ignore for all tables, separated by comma. - --path Path of the directory to start to find *.sql to lint. - --version Print version numbers. - -EXAMPLES: - - # Lint directory - $ skeely --path=assets/examples/ - - # Lint specific file - $ skeely --files=assets/examples/case01.sql - - # Lint specific file and ignore codes - $ skeely --files=assets/examples/case01.sql --ignore=103,104,305,406 - - # Lint and push summary as comment into GitHub Pull Request. - $ skeely --path=assets/examples/case01.sql \ - --github-comment \ - --github-token=${{github.token}} \ - --github-repository=$GITHUB_REPOSITORY \ - --github-pull-request=${{github.event.pull_request.number}} - -For more help, plese visit: https://github.com/debeando/skeely -` - -func Show(rc int) { - fmt.Printf(USAGE, version.VERSION) - os.Exit(rc) -} diff --git a/command/main.go b/command/main.go deleted file mode 100644 index a121621..0000000 --- a/command/main.go +++ /dev/null @@ -1,47 +0,0 @@ -package command - -import ( - "fmt" - "os" - - "skeely/command/help" - "skeely/command/version" - "skeely/common/github" - "skeely/common/terminal" - "skeely/config" - "skeely/flags" - "skeely/linter" - - _ "skeely/plugins" -) - -func Run() { - gh := github.GitHub{} - c := config.GetInstance() - err := c.Load() - if err != nil { - fmt.Println(err) - os.Exit(2) - } - f := flags.GetInstance() - f.Load() - - switch { - case f.Version: - fmt.Println(version.VERSION) - os.Exit(0) - case f.Help: - help.Show(0) - case len(f.Path) > 0 && len(f.Files) > 0: - help.Show(1) - case len(f.Path) == 0 && len(f.Files) == 0: - help.Show(0) - } - - msgPlugins := linter.Run() - - gh.Comment(msgPlugins) - gh.Push() - - terminal.Print(msgPlugins) -} diff --git a/command/version/main.go b/command/version/main.go deleted file mode 100644 index 461a6a2..0000000 --- a/command/version/main.go +++ /dev/null @@ -1,3 +0,0 @@ -package version - -const VERSION string = "0.0.0-beta.2" diff --git a/flags/main.go b/flags/main.go index 0714762..8aba7e4 100644 --- a/flags/main.go +++ b/flags/main.go @@ -1,10 +1,8 @@ package flags import ( - "flag" "os" - "skeely/command/help" "skeely/common" ) @@ -14,10 +12,8 @@ type Flags struct { GitHubPullRequest int GitHubRepository string GitHubToken string - Help bool Ignore string Path string - Version bool } var instance *Flags @@ -30,53 +26,11 @@ func GetInstance() *Flags { } func (f *Flags) Load() { - fFiles := flag.String("files", "", "") - fGitHubComment := flag.Bool("github-comment", false, "") - fGitHubPullRequest := flag.Int("github-pull-request", 0, "") - fGitHubRepository := flag.String("github-repository", "", "") - fGitHubToken := flag.String("github-token", "", "") - fHelp := flag.Bool("help", false, "") - fIgnore := flag.String("ignore", "", "") - flag.Usage = func() { help.Show(1) } - fPath := flag.String("path", "", "") - fVersion := flag.Bool("version", false, "") - flag.Parse() - - f.Files = *fFiles - f.GitHubComment = *fGitHubComment - f.GitHubPullRequest = *fGitHubPullRequest - f.GitHubRepository = *fGitHubRepository - f.GitHubToken = *fGitHubToken - f.Help = *fHelp - f.Ignore = *fIgnore - f.Path = *fPath - f.Version = *fVersion - - if len(f.Files) == 0 && len(os.Getenv("INPUT_FILES")) > 0 { - f.Files = os.Getenv("INPUT_FILES") - } - - if len(f.Ignore) == 0 && len(os.Getenv("INPUT_IGNORE")) > 0 { - f.Ignore = os.Getenv("INPUT_IGNORE") - } - - if len(f.Path) == 0 && len(os.Getenv("INPUT_PATH")) > 0 { - f.Path = os.Getenv("") - } - - if f.GitHubComment == false && common.StringToBool(os.Getenv("INPUT_COMMENT")) == true { - f.GitHubComment = true - } - - if f.GitHubPullRequest == 0 && common.StringToInt(os.Getenv("INPUT_PULLREQUEST")) > 0 { - f.GitHubPullRequest = common.StringToInt(os.Getenv("INPUT_PULLREQUEST")) - } - - if len(f.GitHubRepository) == 0 && len(os.Getenv("INPUT_REPOSITORY")) > 0 { - f.GitHubRepository = os.Getenv("INPUT_REPOSITORY") - } - - if len(f.GitHubToken) == 0 && len(os.Getenv("INPUT_TOKEN")) > 0 { - f.GitHubToken = os.Getenv("INPUT_TOKEN") - } + f.Files = os.Getenv("INPUT_FILES") + f.GitHubComment = common.StringToBool(os.Getenv("INPUT_COMMENT")) + f.GitHubPullRequest = common.StringToInt(os.Getenv("INPUT_PULLREQUEST")) + f.GitHubRepository = os.Getenv("INPUT_REPOSITORY") + f.GitHubToken = os.Getenv("INPUT_TOKEN") + f.Ignore = os.Getenv("INPUT_IGNORE") + f.Path = os.Getenv("INPUT_PATH") } diff --git a/go.mod b/go.mod index 994a3d8..84295d8 100644 --- a/go.mod +++ b/go.mod @@ -4,4 +4,9 @@ go 1.21 require gopkg.in/yaml.v3 v3.0.1 -require gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect +require ( + github.com/inconshreveable/mousetrap v1.1.0 // indirect + github.com/spf13/cobra v1.8.0 // indirect + github.com/spf13/pflag v1.0.5 // indirect + gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect +) diff --git a/go.sum b/go.sum index 33d593e..d888b4d 100644 --- a/go.sum +++ b/go.sum @@ -1,8 +1,16 @@ +github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= +github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= +github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI= github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0= +github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= +github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= diff --git a/main.go b/main.go index 153e483..14d0796 100644 --- a/main.go +++ b/main.go @@ -1,9 +1,71 @@ package main import ( - "skeely/command" + "fmt" + "os" + + "skeely/common/github" + "skeely/common/terminal" + "skeely/config" + "skeely/flags" + "skeely/linter" + "skeely/version" + + "github.com/spf13/cobra" ) func main() { - command.Run() + var f = flags.GetInstance() + var rootCmd = &cobra.Command{ + Use: "skeely [COMMANDS] [OPTIONS]", + Long: `skeely is a schema linter for MySQL, this tool help to identifying +some common and uncommon mistakes on data model. + +For more help, plese visit: https://github.com/debeando/skeely`, + Example: ` + # Lint directory + $ skeely --path=assets/examples/ + + # Lint specific file + $ skeely --files=assets/examples/case01.sql + + # Lint specific file and ignore codes + $ skeely --files=assets/examples/case01.sql --ignore=103,104,305,406 + + # Lint and push summary as comment into GitHub Pull Request. + $ skeely --path=assets/examples/case01.sql \ + --github-comment \ + --github-token=${{github.token}} \ + --github-repository=$GITHUB_REPOSITORY \ + --github-pull-request=${{github.event.pull_request.number}} +`, + Run: func(cmd *cobra.Command, args []string) { + gh := github.GitHub{} + c := config.GetInstance() + + if err := c.Load(); err != nil { + fmt.Println(err) + os.Exit(2) + } + + msgPlugins := linter.Run() + + gh.Comment(msgPlugins) + gh.Push() + + terminal.Print(msgPlugins) + }, + } + + f.Load() + + rootCmd.Flags().BoolVarP(&f.GitHubComment, "github-comment", "", false, "Send summary as comment into GitHub.") + rootCmd.Flags().IntVar(&f.GitHubPullRequest, "github-pull-request", 0, "Pull request number.") + rootCmd.Flags().StringVar(&f.Files, "files", "", "List of files to lint, separated by space.") + rootCmd.Flags().StringVar(&f.GitHubRepository, "github-repository", "", "Repository path on github.") + rootCmd.Flags().StringVar(&f.GitHubToken, "github-token", "", "Token to auth in github.") + rootCmd.Flags().StringVar(&f.Ignore, "ignore", "", "List of error codes separated by comma to ignore.") + rootCmd.Flags().StringVar(&f.Path, "path", "", "Path of the directory to start to find *.sql to lint.") + rootCmd.AddCommand(version.NewCommand()) + rootCmd.Execute() } diff --git a/version/main.go b/version/main.go new file mode 100644 index 0000000..fad1359 --- /dev/null +++ b/version/main.go @@ -0,0 +1,23 @@ +package version + +import ( + "fmt" + + "github.com/spf13/cobra" +) + +// Version is a const to have the latest version number for this code. +const VERSION string = "0.0.0-beta.3" + +func NewCommand() *cobra.Command { + var cmd = &cobra.Command{ + Use: "version", + Short: "Print version numbers", + Long: `All software has versions. This is skale`, + Run: func(cmd *cobra.Command, args []string) { + fmt.Println(VERSION) + }, + } + + return cmd +}